演示如何使用ConfigServer提供统一的参数配置服务
###################################################################
一、概念和定义
1、
2、
3、
###################################################################
二、开发案例-服务端
#*******************************************************
1、引入pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
#*******************************************************
2、引入应用注解
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApp
{
public static void main(String[] args)
{
SpringApplication.run(ConfigServerApp.class, args);
}
}
#*******************************************************
3、编写配置文件,上传到git
ConfigClient-dev.properties
#-------------------------------------------------
UserKey=ConfigClient.dev.key
UserVal=ConfigClient.dev.val
driverClassName=com.mysql.jdbc.Driver
user=root
password=ConfigClient_dev_123
url=jdbc:mysql:///db_user_ConfigClient_dev
ConfigClient-tst.properties
#-------------------------------------------------
UserKey=ConfigClient.tst.key
UserVal=ConfigClient.tst.val
driverClassName=com.mysql.jdbc.Driver
user=root
password=ConfigClient_tst_123
url=jdbc:mysql:///db_user_ConfigClient_tst
#*******************************************************
4、启动服务端测试
http://localhost:8000/ConfigClient/dev/master
http://localhost:8000/ConfigClient/tst/master
http://localhost:8000/ConfigClient2/dev/master
http://localhost:8000/ConfigClient2/tst/master
http://localhost:8000/jdbc/dev/master
http://localhost:8000/jdbc/tst/master
###################################################################
三、开发案例-客户端
#*******************************************************
1、引入pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
#*******************************************************
2、bootstrap.properties
spring.application.name=ConfigClient2
server.port=8822
spring.cloud.config.name=ConfigClient2
spring.cloud.config.profile=pro
spring.cloud.config.uri=http://localhost:8000
spring.cloud.config.label=master
#*******************************************************
3、编写获取配置代码
@RestController
public class ConfigController {
@Value("${driverClassName}")
private String driverClassName;
@Value("${user}")
private String user;
@Value("${password}")
private String password;
@Value("${url}")
private String url;
@PostConstruct
@RequestMapping(value = "/showJdbc")
public String show() {
System.out.println("driverClassName=" + driverClassName);
System.out.println("user=" + user);
System.out.println("password=" + password);
System.out.println("url=" + url);
return "driverClassName="+driverClassName+",<br/>user="+user+",<br/>password="+password+",<br/>url="+url+"<br/>";
}
}
#*******************************************************
4、启动客户端测试
http://localhost:8811/showJdbc
http://localhost:8822/showJdbc
###################################################################
四、代码下载
https://github.com/lexiaofei/workspace_SpringConfig.git