SpringCloud-配置中心

SpringCloud文章系列

  1. SpringCloud
  2. SpringCloud-注册中心
  3. 【当前文章】SpringCloud-配置中心
  4. SpringCloud-链路跟踪
  5. SpringCloud-消息总线
  6. SpringCloud-API网关
  7. SpringCloud-异步消息
  8. SpringCloud-同步调用
  9. SpringCloud-断路降级
  10. SpringCloud-监控管理
  11. SpringCloud-番外篇-临时任务
  12. SpringCloud-番外篇-文档生成
  13. SpringCloud-番外篇-源码解析

接入说明

  1. 目前采用最新的1.xRelease版本:1.5.15.RELEASE
  2. SpringCloud项目可以在start.spring.io下载,不过更方便的应该是通过idea新建项目,建立Spring Initializr项目
  3. 我一般习惯将具体实现服务用具体的服务名,而公共组件用service-xxx来命名,当然还有一些比较固定名字的公共组件

Config

1. 新建项目

新建artifactId为service-config的服务

2. 导入依赖

依赖说明

  1. actuator用于暴露监控的接口
  2. config-server是Config服务器的依赖
  3. eureka是Eureka客户端,这里配置中心依赖于注册中心
1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3. 编写启动类

打开ServiceConfigApplication,在class上加入@EnableConfigServer注解
并且加上@EnableEurekaClient,注册到注册中心

4. 修改配置

建立git仓库

可以是本地仓库,也可以是远程仓库,我们用本地仓库来模拟

  1. 找到一个目录E:/config
  2. git init 初始化仓库
  3. 新建文件application.properties
  4. git add .
  5. git commit -m ‘init’
  6. git push origin master
1
2
3
4
5
6
7
8
9
server.port=8888
spring.application.name=service-config
spring.cloud.config.server.git.uri=E:/config

# public
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
management.security.enabled=false

配置中心默认端口8888

config.server.git.uri设置为git的本地地址

  1. 注意spring.cloud.config.server.git.uri必须配置在应用本地,否则不知道远程配置的地址,而其他服务的配置都可以放到配置中心,这个是特别的

discovery.enabled与discovery.service-id其实是客户端的配置,配置中心自己是服务端也是客户端,作为客户端与其他服务一样,都需要通过配置中心去发现配置服务启动,读取公共配置

  1. discovery.enabled设置为通过注册中心暴露服务
  2. discovery.service-id是配置中心对在注册中心外暴露的id一致,与server端配置的spring.application.name一致

defaultZone注册中心地址
management.security.enabled用于关闭一些校验权限

抽出公共配置–其他微服务共享

application.properties文件添加以下配置,并去掉service-config配置中的配置

1
2
3
4
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
management.security.enabled=false
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config

5. 启动main

6. 验证

访问 http://localhost:8761/ 看到service-config已经注册了一台
访问 http://localhost:8888/service-config/default 就可以访问到配置中心加载的配置了,中间改其他服务名,default是指配置的profile

Config集群

1. 修改配置文件

配置2个server,server1使用端口8888,server2使用端口8889

server1

1
2
3
4
5
6
7
8
9
server.port=8888
spring.application.name=service-config
spring.cloud.config.server.git.uri=E:/IdeaProjects/springcloud-test/config

# public
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
management.security.enabled=false

server2

1
2
3
4
5
6
7
8
9
server.port=8889
spring.application.name=service-config
spring.cloud.config.server.git.uri=E:/IdeaProjects/springcloud-test/config

# public
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
management.security.enabled=false

除了端口号不一样,其他都一样,本质上配置中心只要保证读取的git地址一样就可以了,客户端只是过来拉配置,git相当于数据库

2. 启动

启动server1
启动server2

3. 验证

访问 http://localhost:8761/ 看到service-config已经注册了2台
访问 http://localhost:8888/service-config/default 就可以访问到配置中心加载的配置了
访问 http://localhost:8889/service-config/default 就可以访问到配置中心加载的配置了

客户端使用

1. 添加依赖

客户端要通过注册中心查找注册中心服务,所以需要依赖注册中心

1
2
3
4
5
6
7
8
<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>

2. 开启注解

开启注册中心的配置即可@EnableDiscoveryClient,配置中心不需要额外的注解

3. 修改配置

1
2
3
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config

config.discovery.enabled与config.discovery.service-id是依赖配置中心启动的核心配置,指定配置中心暴露的名称

4. 编写代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RefreshScope
@RestController
class MessageController {

private final String msg;

@Autowired
public MessageController(@Value("${message}") String msg){
this.msg=msg;
}

@RequestMapping("/msg")
public String read() {
return msg;
}

}

@RefreshScope 注意这里如果想通过/refresh接口能实时刷新配置的话,需要加这个注解,没有加的只是启动时加载的数据不会更新
注意这里进来保持好的使用习惯,使用final来保存数据

疑问

1. 为什么一定通过注册中心暴露配置中心服务?

如果不通过注册中心,客户端的配置如下:

1
spring.cloud.config.uri=http://localhost:8888

如果修改修改了域名或者端口,增加减少机器都需要去修改此启动配置,则通过注册中心就可以用其各种好处,减少依赖

2. 配置中心为什么依赖注册中心?怎么我看有的是注册中心依赖配置中心?

其实目前2种配置都有

  1. 注册中心依赖配置中心的解释:就是注册中心也是一个服务,启动所需要的配置从配置中心拉取,同时可以动态变更,启动顺序:先配置中心,再注册中心,配置中心不能依赖注册中心,否则无法启动。
  2. 配置中心依赖注册中心的解释:配置中心如果注册为集群模式那必然让客户端通过服务发现找到从注册中心找到配置中心,所以配置中心一定要注册到注册中心启动服务发现。启动顺序:注册中心,再配置中心,这样注册中心的配置是不经过配置中心的,而注册中心的配置一般也是固定不变的
    针对第一种还有一种解:先启动配置中心,开启注册失败仍然可以启动的配置,只是第一次注册失败依然可以启动,然后再启动注册中心,可以两全
    总体来说第二种更加合理一点,因为一般注册中心的defaultZone与其他应用不一样,注册中心的要排除掉自己的地址,而其他服务是全量,这样我在做公共配置时比较统一,不用开启注册中心的本地配置优先
    不过其他解决方案中注册中心与配置中心一般都是一体的,所以也就没有这种相互依赖的尴尬,不过领域上确实是2个东西

3. 为什么配置中心读取的配置变量声明为final的,但是调用/refresh后带@RefreshScope的对象里的final值还会发生变化?

首先final是不会变的,所以spring是把整个@RefreshScope标注的bean重新创建了一遍
final的好处是

  1. 控制只有配置中心才可以更改变量,在程序中是无法赋值替换的
  2. 配置中心的数据都是很少去变化的,应该当做常量处理,final虚拟机有优化
  3. 可以保证线程安全,无状态类与不可变类多线程安全

其他注册中心

因为consul与zookeeper本身具备配置存储的能力,所以这2个注册中心的配置中心一般用他们定制的,而非用SpringCloudConfig

快速接入

暂略…

------ 本文结束 ------

版权声明

dawell's Notes by Dawell is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Dawell创作并维护的dawell's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于dawell's Notes 博客( http://dawell.cc ),版权所有,侵权必究。

坚持原创技术分享,您的支持将鼓励我继续创作!