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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM