本文接以前的《5--SpringCloud搭建分布式配置中心》,继续来讲说Spring Cloud Config的使用。html
先来回顾一下,在前文中咱们完成了什么:git
在本文中,咱们继续来看看Spring Cloud Config的一些其余能力。web
Config Server与服务注册中心同样,咱们也须要将其扩展为高可用的集群。spring
因此,简单的方法就是把config-server也注册为服务,这样全部客户端就能以服务的方式进行访问。经过这种方法,只须要启动多个指向同一Git仓库位置的config-server就能实现高可用了。bootstrap
首先搭建一个服务注册中心:若是不知道怎么搭建服务注册中心请阅读:《1--SpringCloud的服务注册与发现Eureka》app
在pom.xml
的dependencies节点中引入以下依赖,相比以前的config-server就,加入了spring-cloud-starter-eureka
,用来注册服务分布式
<dependencies>
<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-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置application.properties新增# 配置服务注册中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/spring-boot
spring.application.name=config-server server.port=7001 # 配置服务注册中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ # git配置 #所在项目根目录 spring.cloud.config.server.git.uri=https://gitee.com/cengjiang/springcloud_learning/ #所在地址目录 spring.cloud.config.server.git.searchPaths=5--SpringCloud--Config #若是是公开项目则不用写用户名密码 spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password
在应用主类中,新增@EnableDiscoveryClient
注解,用来将config-server注册到上面配置的服务注册中心上去。工具
@EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
启动该应用,并访问http://localhost:1111/
,能够在Eureka Server的信息面板中看到config-server已经被注册了。post
同config-server同样,在pom.xml
的dependencies节点中新增spring-cloud-starter-eureka
依赖,用来注册服务:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
在bootstrap.properties
中,按以下配置:
server.port=7002 #对应前配置文件中的{application}部分 spring.application.name=ghghspace eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ #参数设置为true,开启经过服务来访问Config Server的功能 spring.cloud.config.discovery.enabled=true #对应前配置文件中的{profile}部分 spring.cloud.config.profile=dev #对应前配置文件的git分支 spring.cloud.config.label=master #配置中心的访问地址 spring.cloud.config.uri=http://localhost:7001/
经过eureka.client.serviceUrl.defaultZone
参数指定服务注册中心,用于服务的注册与发现,再将spring.cloud.config.discovery.enabled
参数设置为true,开启经过服务来访问Config Server的功能
在应用主类中,增长@EnableDiscoveryClient
注解,用来发现config-server服务,利用其来加载应用配置
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
沿用以前咱们建立的Controller来加载Git中的配置信息
@RefreshScope @RestController public class TestController { @Value("${from}") private String from; @RequestMapping("/from") public String from() { return this.from; } }
完成了上述配置以后,咱们启动该客户端应用。若启动成功,访问http://localhost:1111/
,能够在Eureka Server的信息面板中看到该应用已经被注册成功了。
访问客户端应用提供的服务:http://localhost:7002/from
会返回信息
有时候,咱们须要对配置内容作一些实时更新的场景,那么Spring Cloud Config是否能够实现呢?答案显然是能够的。下面,咱们看看如何进行改造来实现配置内容的实时更新。
在改造程序以前,咱们先将config-server和config-client都启动起来,并访问客户端提供的REST APIhttp://localhost:7002/from
来获取配置信息,能够得到返回内容为:git-dev-1.0
。
接着,咱们能够尝试使用Git工具修改当前配置的内容,好比,将5--SpringCloud--Config/didispace-dev.properties
中的from的值从from=git-dev-1.0
修改成from=git-dev-2.0
,再访问http://localhost:7002/from
,能够看到其返回内容仍是git-dev-1.0
。
下面,咱们将在config-client端增长一些内容和操做以实现配置的刷新:
在config-clinet的pom.xml
中新增spring-boot-starter-actuator
监控模块,其中包含了/refresh
刷新API。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
从新启动config-clinet,访问一次http://localhost:7002/from
,能够看到当前的配置值
5--SpringCloud--Config
/didispace-dev.properties
文件中from
的值http://localhost:7002/from
,能够看到配置值没有改变http://localhost:7002/refresh
,咱们能够看到返回内容以下,表明from
参数的配置内容被更新了(请求须要为POST请求,get请求为405)[ "from" ]
再次访问一次http://localhost:7002/from
,能够看到配置值已是更新后的值了经过上面的介绍,你们不难想到,当有Git提交变化时,就给对应的配置主机发送/refresh
请求来实现配置信息的实时更新。