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的原本配置信息储存在数据库中,可以实现动态加载的方式,可以在后面数据库中在添加需要配置。
添加配置截图: