SpringCloud Config配置中心實現數據庫持久化

Spring Cloud Config從Edgware版本開始新增了一種配置的方法,可以把配置信息放到數據庫中,在SpringCloud項目啓動的時候配置服務器從數據庫中讀取配置信息,

分爲配置服務器和客戶端:

配置服務器如下:配置服務器從數據庫讀取配置信息

這裏寫圖片描述

pom.xml

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring-cloud.version>Edgware.SR2</spring-cloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>-->

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!--<dependency>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-core</artifactId>
      <version>5.0.3</version>
    </dependency>-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.21</version>
    </dependency>
  </dependencies>




  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

啓動類:


@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
     public static void main(String[] args){

         SpringApplication.run(ConfigServerApplication.class,args);

     }
}

application.properties

#應用名稱
spring.application.name=config-server-db
server.port=8080
#由於需要訪問數據庫,所以需要加載jdbc的依賴
spring.profiles.active=jdbc
#
spring.cloud.config.server.jdbc.sql=SELECT `aKey`, `avalue` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?

#數據源
spring.datasource.url=jdbc:mysql://localhost:3306/test2
spring.datasource.username=root
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

數據庫中的測試數據;

CREATE TABLE `properties` (
  `id` INT(11) NOT NULL,
  `akey` VARCHAR(50) NOT NULL,
  `avalue` VARCHAR(500) NOT NULL,
  `application` VARCHAR(50) NOT NULL COMMENT '應用名稱',
  `profile` VARCHAR(50) NOT NULL COMMENT '應用模塊',
  `label` VARCHAR(50) NOT NULL COMMENT '應用環境',
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO properties VALUES(1, 'com.didispace.message', 'test-stage-master', 'config-client', 'stage', 'master');
INSERT INTO properties VALUES(2, 'com.didispace.message', 'test-online-master', 'config-client', 'online', 'master');
INSERT INTO properties VALUES(3, 'com.didispace.message', 'test-online-develop', 'config-client', 'online', 'develop');
INSERT INTO properties VALUES(4, 'com.didispace.message', 'hello-online-master', 'hello-service', 'online', 'master');
INSERT INTO properties VALUES(5, 'com.didispace.message', 'hello-online-develop', 'hello-service', 'online', 'develop');

INSERT INTO properties VALUES(9, 'logging.path', '/varttttt/myerror', 'test', 'r', 'r');

COMMIT;
SELECT * FROM properties

客戶端:從配置中心回去配置信息(配置文件是bootstrap.properties)

pom.xml:


  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring-cloud.version>Edgware.SR2</spring-cloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>


  </dependencies>



  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

配置文件:bootstrap.properties:

spring.application.name=test
server.port=8081

spring.cloud.config.uri=http://localhost:8080/
spring.cloud.config.profile=r
spring.cloud.config.label=r

management.security.enabled=false

啓動類:


@SpringBootApplication
public class ConfigCenterApplication {
     public static void main(String[] args){
         SpringApplication.run(ConfigCenterApplication.class,args);
     }



}

測試:

@RefreshScope
@RestController
public class TestController {


    @Value("${logging.path}")
    private String message;

    @GetMapping("/test")
    public String test() {
        return message;
    }


}

分別啓動配置服務器和客戶端:
在項目的同路徑下會生成對應的文件,瀏覽器返回/varttttt/myerror字符串

通過數據庫持久化可以將自定義配置和SpringCloud的原本配置信息儲存在數據庫中,可以實現動態加載的方式,可以在後面數據庫中在添加需要配置。

添加配置截圖:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章