SpringCloud之配置中心Config
Spring Cloud Config可以把一些配置信息配置在远程服务器上,集中化管理集群配置。可以使用Spring Cloud Config来获取远程服务器上的配置信息。
可以分为两个部分:
服务端: 配置服务端,服务管理配置信息
客户端:客户端调用server端暴露接口获取配置信息
先来看看我github的目录和要读取的配置:里面定义四个配置文件读取。
现在读取第一个配置文件中的值:读取的方式和配置名称可以有关
读取项目可以分为两个部分:
服务端: 配置服务端,服务管理配置信息
客户端:客户端调用server端暴露接口获取配置信息
先来看服务器:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
@SpringBootApplication
@EnableConfigServer
public class Applicationn {
public static void main(String[] args){
SpringApplication.run(Applicationn.class,args);
}
}
server.port=8081
#spring.application.name=microservice-foo
spring.application.name=ok
# 远程仓库地址,具体到项目
spring.cloud.config.server.git.uri=https://github.com/meihuiziaaaaaa/firstTest
# 项目下的具体路径可以配置多个
spring.cloud.config.server.git.searchPaths=config/config
#spring.cloud.config.label=master
# 用户名
#spring.cloud.config.server.git.username=
# 密码
#spring.cloud.config.server.git.password=
服务器代码就此结束:读取方式如下:
启动服务端之后,我们可以通过url的方式对配置文件进行访问,访问关系如下:
url:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不同的分支,默认为master。
例如:访问microservice-foo-dev.properties文件的方式
在例如访问microservice-foo-dev.properties和microservice-foo.properties
先开启服务端,在启动客户端
现在客户端的读取方式:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
bootstrap.properties
需要配置在bootstrap中才能生效,而不是在application配置文件中,bootstrap配置文件会比application更加早的加载。
spring.application.name=microservice
spring.cloud.config.profile=foo-dev
spring.cloud.config.uri=http://localhost:8081/
#spring.cloud.config.label=master
@PropertySource(value = "classpath:bootstrap.properties")
@RestController
public class Test1 {
@Value("${profile}")
private String profile;
@RequestMapping("/profile")
public String profile(){
System.out.println(this.profile+"....");
return this.profile;
}
}
配置刷新
在上面的配置中,客户端是如何获得配置中心的配置信息的呢?是在bootstrap配置文件中配置,在程序启动时加载获取的。显然,在加载完成之后,如果配置中心中的信息发生变化的话,客户端的信息是不会跟着变化的,这是实际开发过程中所不允许的。我们应该怎么做呢?
我们需要增加一个监控模块:spring-boot-starter-actuator
在客户端中增加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
并且在Controller中增加@RefreshScope注解。
最后,actuator给我们提供了一个/refresh接口,修改完git仓库信息之后,向这个接口发送一个POST信息,就会更新配置信息了。不需要自己写/refresh接口
修改github配置文件后 (bootstrap.properties: management.security.enabled=false)访问http://localhost:8080/refresh,再一次http://localhost:8080/profile取得的是最新的数据
但是服务端和客户端最终是要成为微服务的,所以还得向eureka服务注册
在服务端和客户端都增加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
启动类上加
@EnableDiscoveryClient
客户端
eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.168.15.190:8888/eureka/
服务端
#服务注册
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=configServier
eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.xxx.15.190:8888/eureka/
注意:
在客户端上面实现了两种方式读取配置一种是url,另一种是通过注册的服务名称读取
第一种: spring.cloud.config.uri=http://localhost:8081/
第二种: spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=configServier
最终的代码:
服务端:
pom.xml
------- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <scope>test</scope>--> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- Eureka 客户端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class Applicationn {
public static void main(String[] args){
SpringApplication.run(Applicationn.class,args);
}
}
application.properties
server.port=8081
#spring.application.name=microservice-foo
spring.application.name=configServier
# 远程仓库地址,具体到项目
spring.cloud.config.server.git.uri=https://github.com/meihuiziaaaaaa/firstTest
# 项目下的具体路径可以配置多个
spring.cloud.config.server.git.searchPaths=config/config
#spring.cloud.config.label=master
# 用户名
#spring.cloud.config.server.git.username=
# 密码
#spring.cloud.config.server.git.password=
eureka.client.serviceUrl.defaultZone=http://192.168.15.188:8888/eureka/,http://192.168.15.189:8888/eureka/,http://192.168.15.190:8888/eureka/
客户端:
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</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-config</artifactId> </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Eureka 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
bootstrap.properties
server.port=8080
spring.application.name=microservice
spring.cloud.config.profile=foo-dev
#spring.cloud.config.uri=http://localhost:8081/
#spring.cloud.config.label=master
management.security.enabled=false
#服务注册
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=configServier
eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.xxx.15.190:8888/eureka/
测试
@PropertySource(value = "classpath:bootstrap.properties")
@RestController
@RefreshScope
public class Test1 {
@Value("${profile}")
private String profile;
@RequestMapping("/profile")
public String profile(){
System.out.println(this.profile+"....");
return this.profile;
}
}
还没有评论,来说两句吧...