这是系列搭建springcloud基础框架的文章,内容包括集成shiro、Mysql主从、seata、activiti、drools、hadoop大数据常用组件、keepalive+nginx https配置等;
docker pull mysql:5.7
redis 参考:docker+docker-compose 安装 redis.note
主库 :
[root@localhost mysql]# docker run -d --privileged=true -p 3306:3306 \
> --name mysql57 \
> -v ~/workspace/dockerspace/mysql/datadir:/var/lib/mysql \
> -v ~/workspace/dockerspace/mysql/conf.d:/etc/mysql/conf.d \
> -e MYSQL_ROOT_PASSWORD=163123 mysql:5.7
执行命令 :
docker exec -it mysql57 /bin/bash
# 安装 vi
>apt-get update
.... 更新过程
>apt-get install vim --fix-missing
.... 安装过程
>vi /etc/mysql/mysql.conf.d/mysqld.cnf
# 配置开始
.... 原始内容
log-bin=mysql-bin
binlog_format=Row
server-id=1
binlog-do-db=chen #同步的数据库
# 如果是vmware centos,直接 yum install -y vim
但是没有 vi,只有 vim;
# 注 2023-5-11
在 vware centos 安装的 mysql:5.7,进入容器后,在 /etc/mysql/mysql.conf.d/mysqld.cnf 没有找到这个文件;
参考 https://blog.csdn.net/lihuarongaini/article/details/101297899
找到 可能的 mysql 启动配置文件:
> mysql --help|grep 'my.cnf' 或 mysqld --verbose --help |grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
mysql会按顺序匹配,顺序排前的优先;
再采用 whereis my.cnf 看一下位置
>whereis my.cnf
/etc/my.cnf
=======================
但是在 applce docker desktop安装的 mysql, 他的配置文件是 mydqld.cnf
使用 whereis mysqld.cnf 找不到;
查看 my.cnf 具体位置: MySQL查看当前使用的配置文件my.cnf的方法.note
保存并退出容器,执行重启容器;
docker restart mysql57
获取 mysql57(master) 容器的ip,重要;
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql57
# 172.17.0.2
创建 slave 连接 mysql57(master库)的用户 ;
>create user 'master06'@'%' identified by 'xxxxxx';
>grant replication slave on *.* to 'master06'@'%' identified by 'xxxxxx';
>flush privileges;
#其他命令 (以下不需要)
>show grants for 'master06';
>alter user 'master06'@'%' IDENTIFIED with mysql_native_password by 'xxxxxx';
=======================================
第二步配置 slave
[root@localhost mysql]# docker run -d --privileged=true -p 3307:3306 \
> --name mysql3307 \
> -v ~/workspace/dockerspace/mysql/datadir:/var/lib/mysql \
> -v ~/workspace/dockerspace/mysql/conf.d:/etc/mysql/conf.d \
> -e MYSQL_ROOT_PASSWORD=163123 mysql:5.7
同步docker run进入mysql3307容器; apt-get upate install,再配置 mysqld.cnf
(以上命令在 MAC 没有问题,即datadir, conf.d 可以一样,但是在windows的vmware 上运行,报进程ID被使用,可以修改为 datadir2 处理)
log-bin=mysql-bin
binlog_format=Row
server-id=2 # 两个 server-id 不能一样;
保存退出容器重启 mysql3307容器 ;
查看当前 mysql57(master) 的 master 状态;
>show master status;
#----
File |Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|
----------------+--------+------------+----------------+-----------------+
mysql-bin.000002| 4142|chen | | |
--
使用 show variables like 'log_%'; 查看是否修改成功,重启后,重启容器;
配置slave 连接 master 并开启slave;
change master to master_host='172.17.0.2',master_port=3306,master_user='master06',master_password='163123',
master_log_file='mysql-bin.000002',master_log_pos=4142;
start slave;
show slave status;
stop slave; #首次不需要,后续修改,这句先执行,再 change master .. 最后再start slave;
# 如果 Slave failed to initialize relay log info structure from the repository
则 reset slave;
再 change master to ....
再 start slave;
master_host这里不能用 127.0.0.1,也不能 mysql57;
注:docker-compose: https://www.coder.work/article/4512640
mysql容器的配置文件位置 : https://blog.csdn.net/northernice/article/details/129151581
=========================================
springboot+shardingsphere:
参考: https://github.com/yudiandemingzi/spring-boot-sharding-sphere
1)添加依赖 — chen-common/pom.xml
# 更改
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId><!-- druid -->
<version>1.1.16</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>javax.servlet</groupId>-->
<!-- <artifactId>servlet-api</artifactId>-->
<!-- <version>2.5</version>-->
<!-- </dependency>-->
<!-- servlet-api 2.5 会与该版本springboota集成的 tomcat9 冲突 -->
<!-- HttpServletResponse部分代码要调整 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- shardingsphere 相关 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!--sharding seata begin-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.shardingsphere</groupId>-->
<!-- <artifactId>sharding-transaction-base-seata-at</artifactId>-->
<!-- <version>4.1.1</version>-->
<!-- </dependency>-->
<!--aop begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
</dependency>
2)chen-manager 相关调整
a. ManagerApplication类注解调整,由于使用shardingsphere,就按druid配置,因此在去掉数据源的自动配置 ;
exclude=DruidDataSourceAutoConfigure.class
https://blog.csdn.net/jd_gogogo/article/details/114029048
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
@EnableFeignClients(value = "com.chen.frame.api")
@ComponentScan(basePackages = {"com.chen.frame.common", "com.chen.frame.api", "com.chen.frame.shiro", "com.chen.frame.manager"})
public class ManagerApplication {...}
nacos数据源配置 :chen-druid-dev.yml
spring:
shardingsphere:
datasource:
names: master,slave0
master:
driver-class-name: com.mysql.jdbc.Driver
password: xxxxxx
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/chen?characterEncoding=utf-8
username: root
slave0:
driver-class-name: com.mysql.jdbc.Driver
password: xxxxxx
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3307/chen?characterEncoding=utf-8
username: root
masterslave:
load-balance-algorithm-type: round_robin
master-data-source-name: master
name: ms
slave-data-source-names: slave0
props:
sql:
show: true
测试controller添加 ShireController
@RestController
@RequestMapping(value = "/shiro")
public class ShiroController {
@Autowired
private TestService testService;
@PostMapping(value = "/insertTest")
public ResultData insertTest(@RequestBody Test test) {
testService.insert(test);
return new ResultData(ResultStatus.SUCCESS, "ok");
}
@GetMapping(value = "/queryTest")
public ResultData queryTest() {
List<Test> list = testService.queryTest(null);
return new ResultData(ResultStatus.SUCCESS, "ok", list);
}
}
Test.java 数据实体bean:
@Entity
@Table(name = "T_TEST")
@Data
public class Test {
@Id
@Column(name = "ID", length = 11, columnDefinition = "主健")
private Integer id;
@Column(name = "USER_NAME", length = 100, columnDefinition = "用户名")
private String userName;
}
TestService.java 接口:
public interface TestService {
int insert(Test test);
List<Test> queryTest(Test test);
}
TestServiceImpl.java 实现类
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public int insert(Test test) {
testMapper.insert(test);
return 1;
}
@Override
public List<Test> queryTest(Test test) {
return testMapper.selectList(test);
}
}
TestMapper.java (到这里没有创建 resources/mapper/TestMapper.xml)
@Mapper
public interface TestMapper extends IMysqlBaseMapper<Test, Integer> {
List<Test> queryTest(Test test);
}
最后,manager/bootstrap.yml底部添加 ;
# shardingsphere 数据库安全检测
# https://github.com/apache/shardingsphere/issues/7564
management:
health:
db:
enabled: false
运行 http://localhost:8033/shiro/queryTest 正常显示 ;
... ShardingSphere-SQL : Rule Type: master-slave
... ShardingSphere-SQL : SQL: SELECT ID,USER_NAME FROM T_TEST ::: DataSources: slave0
补充redis安装:docker+docker-compose 安装 redis.note
=======================================================
springboot+druid+shardingsphere 分库分表教程及其他;
https://github.com/yudiandemingzi/spring-boot-sharding-sphere
=======================================================
错误相关 :
1)servlet-api 相关
引入的 servlet-api 2.5 会与 springboot自身的 tomcat9 冲突,提示类不存在 ;
servlet-api-xxx 与 javax.servlet-api 区别
3.1 之前的 Servlet API 构件叫做 servlet-api-xxx.jar
3.1 及之后的Servlet API 构件改名为 javax.servlet-api-xxx.jar
当去掉 servlet-api 2.5 的 maven 依赖时,会自动加 2.3的版本;
当依赖的 2.5 版本添加provided时,2.3与2.5都会idea的依赖库存在(mvn -U idea:idea也无法去掉 2.3)
使用 javax.servlet-api 3.1x后,servlet-api 2.3版本依旧存在;(运行 chen-manager 不会再报与 tomcat冲突)
错误相关内容 :https://blog.csdn.net/syg2109170085syg/article/details/106082814
2)shardingsphere Caused by: java.sql.SQLFeatureNotSupportedException: isValid
# shardingsphere 数据库安全检测
management:
health:
db:
enabled: false
3)java.sql.SQLFeatureNotSupportedException问题原因及解决方法
参考:https://blog.csdn.net/qq_37713266/article/details/115859542
原本 shardingsphere 的版本为 4.1.1
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!--sharding seata begin-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.0.0-RC1</version>
</dependency>
druid原为 1.1.22
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId><!-- druid -->
<version>1.1.16</version>
</dependency>
===2023-5-12 补充====================================================
apple docker-desktop 与 vmware docker 创建 mysql 的地址 (不一样)
1)日志不一样:
apple : /etc/mysql/mysql.conf.d/mysql.cnf
vmware: /etc/my.cnf
2)logbin-do-db 与 binlog_do_db
logbin-do-db 在 apple 可识别;
binlog_do_db vmware 才可识别;
3)创建 mysql slave时,docker run 的 -v 目录映射,要与 master 不一样;
报错:由于 my.cnf 配置错误导致容器无法重启;
解决:
1)docker logs 容顺Id 查看错误信息,查看哪个文件报的错误,一般是上次修改的哪个容器文件;
2)拷贝容器文件到本地:docker cp 容器id:/etc/my.cnf /usr/local/dockers/mysql/errors
3) 将 文件修改后,再拷贝回容器内:
/usr/local/dockers/mysql/errors 下
docker cp my.cnf 容器id:/etc/my.cnf
重启后正常
4)配置开启binlog 相关,需要在 [mysqld] 标记下,不然无法识别;
===================
vmware 上创建 redis ;
安装 docker-compose: Docker compose 编排服务好文及安装.note
报错: 启动redis报Can’t open the log file: Permission denied错误 (会一直重试,不必关闭)
需要将映射的日志,数所目录赋权 :
cd /usr/local/dockers/redis/6379
chmod 777 data/ logs/
后,redis 启动成功;