关于集中的配置管理已经在 10.天气预报系统-集中化配置 中详细介绍了。本文为了方便引出问题,所以重新介绍一下它的基本使用。

服务端

第一步:新建一个工程,引入相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<dependencies>
<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>

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

<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

</dependencies>

第二步:添加注解

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringCloudConfigServerApplication {

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

第三步:配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server:
port: 8085
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/sunweiguo/spring-cloud-config-center.git
username: sunweiguo
password: *********
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka

第四步:在这个仓库里新建两个文件cloud-config-dev.propertiescloud-config-test.properties,里面有一些简单内容:name=sunweiguo-devname=sunweiguo-test

第四步:启动配置的服务端

浏览器输入:http://localhost:8085/cloud-config-dev.properties,如果可以看到里面配置的内容,那么就配置成功了。

客户端

第一步:新建一个sb项目,引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<dependencies>
<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>

<!--Eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency>
</dependencies>

第二步:添加注解使其注册到eureka上,不再赘述

第三步:配置文件

注意,这里必须是bootstrap.yml,他的优先级别比application.yml要高。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server:
port: 8086
# 这个cloud-config要与git上的文件名一致,文件的命名规则在开头的链接中已经详细介绍了
spring:
application:
name: cloud-config
cloud:
config:
profile: dev
discovery:
service-id: config-server # 致命服务端
enabled: true
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
management:
endpoints:
web:
exposure:
include: "*" # 这是一个2.x的坑,需要配置这个暴露所有端点,否则refresh报404

第四步:写一个简单的controller来显示

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
@RefreshScope
public class TestController {

@Value("${name}")
private String name;

@RequestMapping("/test")
public String test(){
return "hello."+this.name;
}

}

第五步:在浏览器中输入对应的url看是否显示正确,下面就是在git上修改一下对应的文件内容。我们会发现,服务端上面已经更新了,但是客户端没有更新。那么就需要我们手动去触发他更新

第六步:打开postman工具。输入Url:localhost:8086/actuator/refresh

如果返回:

1
2
3
4
[
"config.client.version",
"name"
]

那么表示更新成功。此时再刷新之前的页面,会发现客户端的内容已经同步过来了。

总结

我们实现了将配置文件放在git上集中管理,但是在修改中心仓库中的配置后,客户端是没有更新的,后来我们用更新包手动去更新,虽然不用重启服务,但是还是比较麻烦。那么,如何避免,对每个服务发送post请求去更新呢?

其实很简单,就是利用发布订阅的原理,利用消息来通知从而触发更新,是比较好的方式。下一节介绍消息总线。