1 分库分表与读写分离
1 修改数据库配置:
找到如下文件:
examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-readwrite-splitting.properties
改成自己的用户名和密码(需要改的地方有不少行,注意别漏改)
可以在该文件下方加入如下代码,打开打印日志的开关:
spring.shardingsphere.props.sql-show=true
2 运行代码
找到如下类中的main方法。:
org.apache.shardingsphere.example.sharding.spring.boot.mybatis.ShardingSpringBootMybatisExample
这里有两处需要注意:
注意点1:
如果在该方法上没有出现
"运行"的绿色按钮,则需要将该模块的pom文件加入到maven中,如下图所示:
注意点2:
如果运行时出现"找不到或无法加载主类"的问题:参照下方链接中的第4点解决问题
如下图所示,运行成功。
2 数据加密
找到如下文件((此时换了一个新模块)):
examples/shardingsphere-jdbc-example/ governance-example/governance-spring-boot-mybatis-example/src/main/resources/application.properties
将下列代码的注释打开
考虑到运行
实例代码会清理数据,故先将相关代码注释掉,方便查看到运行后产生的数据
examples/example-core/example-api/src/main/java/org/apache/shardingsphere/example/core/api/ExampleExecuteTemplate.java
examples/example-core/example-spring-mybatis/src/main/java/org/apache/shardingsphere/example/core/mybatis/service/OrderServiceImpl.java
找到下列类中的main方法,运行
org.apache.shardingsphere.example.governance.spring.boot.mybatis.GovernanceSpringBootMybatisExample
此时我遇到了,资源无法注入的问题
java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:779) ~[na:na]
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:342) ~[zookeeper-3.6.0.jar:3.6.0]
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1262) ~[zookeeper-3.6.0.jar:3.6.0]
2021-08-24 05:47:28.963 INFO 52188 --- [ main] org.apache.zookeeper.ZooKeeper : Session: 0x0 closed
2021-08-24 05:47:28.963 INFO 52188 --- [ain-EventThread] org.apache.zookeeper.ClientCnxn : EventThread shut down for session: 0x0
2021-08-24 05:47:28.980 WARN 52188 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mybatisOrderRepository' defined in file [D:\AppData\career\ShardingSphere\shardingsphere\examples\example-core\example-spring-mybatis\target\classes\org\apache\shardingsphere\example\core\mybatis\repository\MybatisOrderRepository.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localShardingSphereDataSource' defined in class path resource [org/apache/shardingsphere/spring/boot/governance/ShardingSphereGovernanceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'localShardingSphereDataSource' threw exception; nested exception is org.apache.shardingsphere.governance.repository.api.exception.GovernanceException: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
2021-08-24 05:47:28.990 INFO 52188 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-24 05:47:29.023 ERROR 52188 --- [ main] o.s.boot.SpringApplication : Application run failed
localShardingSphereDataSource