Springcloud学习笔记26--JeecgBoot 新建一个微服务模块,同时配置动态数据源(使用两个数据库)
1.创建一个新的module
创建完成后,项目结构为:
2.创建文件目录、启动类
(1)在pom文件中添加依赖和打包插件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>jeecg-cloud-module</artifactId> <groupId>org.jeecgframework.boot</groupId> <version>2.4.6</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-cloud-test</artifactId> <dependencies> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-base-core</artifactId> </dependency> <!--引入微服务启动依赖 starter--> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-starter-cloud</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(2)添加配置文件application.yml
server:
port: 7004
spring:
application:
name: jeecg-cloud-test
(3)添加启动类
package org.jeecg; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication(scanBasePackages = "org.jeecg") @EnableFeignClients(basePackages = "org.jeecg") public class JeecgCloudTestApplication { public static void main(String[] args) { SpringApplication.run(JeecgCloudTestApplication.class, args); } }
启动JeecgCloudTestApplication类,可以看到项目启动成功。
注意:@EnableFeignClients(basePackages = "org.jeecg")这个注解一定要加上,否则会报‘org.jeecg.common.api.CommonAPI' not be found 错误。
模块的启动顺序为(1)JeecgNacosApplication nacos应用 (2)JeecgGatewayApplication 网关应用 (3)JeecgCloudTestApplication 测试应用
此时,查看http://192.168.141.128:8848/nacos/index.html可见
3.在jeecg-cloud-gateway中配置路由信息
打开jeecg-cloud-gateway模块中的application.yml文件
添加如下内容
- id: jeecg-cloud-test uri: lb://jeecg-cloud-test predicates: - Path=/test/**
4.创建测试类与postman测试
(1)首先,创建目录和TestController
@Slf4j @Api(tags = "test") @RestController @RequestMapping("/test") public class TestController { @GetMapping(value = "/demo") @ApiOperation(value = "测试方法", notes = "测试方法") public Result methodTest() { return Result.OK("这是测试方法TestController"); } }
(2)修改gateway网关模块中的路由配置
- id: jeecg-cloud-test uri: lb://jeecg-cloud-test predicates: - Path=/test/**
(3)绕开网关,直接访问http://127.0.0.1:7006/test/demo
5.利用jeecg-boot代码生成器自动生成代码
具体使用见:https://www.cnblogs.com/luckyplj/p/15393648.html
注意:代码生成器生成的代码,务必按照包名规则 org.jeecg.modules.*
进行初始化,其他请了解jeecgboot mybatis
的包扫描规则,不然bean扫描不到!
如果未按包名规则正确配置,会报如下错误。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pmSubsysHostsController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pmSubsysHostsServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.jeecg.flep.mapper.PmSubsysHostsMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pmSubsysHostsServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.jeecg.flep.mapper.PmSubsysHostsMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
... 20 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.jeecg.flep.mapper.PmSubsysHostsMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
... 33 common frames omitted
正常的模块结构应该如下所示:
若要自定义包结构,可参考https://www.cnblogs.com/luckyplj/p/15397610.html
6 配置动态数据源
(1)动态数据源配置
datasource: master: url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root password: plj824 driver-class-name: com.mysql.cj.jdbc.Driver # 多数据源配置 multi-datasource1: url: jdbc:mysql://localhost:3306/flep?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root password: plj824 driver-class-name: com.mysql.cj.jdbc.Driver
master 为主数据源,系统默认数据源
multi-datasource1 :自定义的第三方数据源,multi-datasource1名称随便定义
(2)使用 @DS 切换数据源。
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
注解在service实现或mapper接口方法上,但强烈不建议同时在service和mapper注解。 (可能会有问题)
注解 | 结果 |
---|---|
没有@DS | 默认数据源 |
@DS("dsName") | dsName可以为组名也可以为具体某个库的名称 |
代码示例:
@Service @DS("multi-datasource1") public class BsClientSubsysServiceImpl extends ServiceImpl<BsClientSubsysMapper, BsClientSubsys> implements IBsClientSubsysService { }
此时,利用postman测试。
参考文献:
jeecg-boot:注解扫描范围问题 https://blog.csdn.net/gwcgwcjava/article/details/95967349
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2019-09-30 038 商品详情02-----页面静态化
2019-09-30 037 商品详情01