您当前的位置: 首页 >  微服务

微服务技术系列教程(24) - SpringCloud- 分布式配置中心

杨林伟 发布时间:2019-12-03 17:18:58 ,浏览量:2

引言

代码已上传至Github:有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringCloud-DynamicConfig-Demo

本文目录结构: l____引言 l____ 1. 常用的分布式配置中心 l____2. SpringCloud Config 原理 l____3. SpringCloud Config 集成 l________3.1 Github创建项目 l________3.2 ConfigServer配置服务器 l________3.3 ConfigClient客户端 l________3.4 测试 l____4. 动态刷新数据 l________4.1 actuator端点刷新数据 l________4.2 SpringCloud Bus实时刷新配置文件 l____总结

1. 常用的分布式配置中心

分布式配置中心的文章,之前也有写过,有兴趣的同学可以参考:《分布式配置中心Apollo安装与详解》

当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,使用分布式配置中心框架可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。常用的分布式配置中心框架有:

  • Disconf(依赖于zookpeer,百度研发)
  • Zookpeer(保证配置文件信息实时更新 -事件通知)
  • diamond(阿里巴巴研发)
  • Apollo阿波罗(携程研发)
  • Redis
  • xxl-conf

本文主要讲解SpringCloud Config分布式配置中心。

2. SpringCloud Config 原理

先来看看SpringCloud的流程图: 在这里插入图片描述 上面主要有几个对象:

  • 用户(提交配置文件信息)
  • Git/SVN(存放持久化配置文件信息)
  • Config Server(获取gti环境上的配置文件信息、Config Client 从它这里获取配置文件信息)
  • Config Client(Client服务,从ConfigServer获取配置文件信息)

用户会提交配置文件到Git/SVN,ConfigServer会定时或者手动去Git/SVN获取最新的持久化配置文件信息,Client会去ConfigServer里获取配置文件信息。

也许大家会问,为什么要这样设计,Client为什么不能直接去Git/SVN获取,还要增加多一个Config Server?

如果Client每次都从远程的Git仓库去取配置信息,那岂不是很耗时间,而且网络也不稳定。所以新增了Config Server,由ConfigServer去获取内容并临时存放着。这样就不存在这些问题了。

现在开始来讲解下SpringCloud Config集成的流程。

3. SpringCloud Config 集成 3.1 Github创建项目

务必记住,因为后面的配置文件命名用到,注意git环境分为几种:

  • dev 开发环境
  • sit 测试环境
  • pre 预发布环境
  • prd 准生产环境

1.登录Github,创建仓库,新建config目录

  • 具体的创建步骤百度或谷歌下。我创建Git的地址是:https://github.com/ylw-github/SpringCloud-DynamicConfig-Demo 在这里插入图片描述

2.创建配置文件并上传

  • 注意配置文件的命名格式为:{项目名}-{环境}.properties
  • 文件命名如果错误了,将会读取不到,我的文件命名如下,新建了3个环境,分别是dev(开发环境)、sit(测试环境)、prd(准生产环境): 在这里插入图片描述
3.2 ConfigServer配置服务器

1.新增ConfigServer项目

2.添加Maven依赖


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.M7
                pom
                import
            
        
    
    
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

    
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/libs-milestone
            
                false
            
        
    

3.application.yml配置

####端口号
server:
  port: 8888

###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
spring:
  application:
    ####注册中心应用名称
    name: config-server
  cloud:
    config:
      server:
        git:
          ###git环境地址
          uri: https://github.com/ylw-github/SpringCloud-DynamicConfig-Demo.git
          ####搜索目录
          search-paths:
            - config
      ####读取分支
      label: master

4. 启动类添加注解@EnableConfigServer

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

5.验证 浏览器访问:http://localhost:8888/config-client-dev.properties,可以看到config-client-dev.properties的内容: 在这里插入图片描述 访问:http://localhost:8888/config-client-sit.properties 在这里插入图片描述 成功,说明ConfigServer能从远程仓库获取配置文件信息。

3.3 ConfigClient客户端

1.新增ConfigServer项目

2.添加Maven依赖


	org.springframework.boot
	spring-boot-starter-parent
	2.0.1.RELEASE



	
		
			org.springframework.cloud
			spring-cloud-dependencies
			Finchley.M7
			pom
			import
		

	


	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
		org.springframework.cloud
		spring-cloud-config-client
	
	
	
		org.springframework.cloud
		spring-cloud-starter-netflix-eureka-client
	



	
		spring-milestones
		Spring Milestones
		https://repo.spring.io/libs-milestone
		
			false
		
	

3.bootstrap.yml

server:
  port: 8882
spring:
  application:
    ####注册中心应用名称
    name:  config-client
  cloud:
    config:
      ####读取后缀
      profile: dev
      ####读取config-server注册地址
      discovery:
        service-id: config-server
        enabled: true
#####    eureka服务注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka

4.启动类

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class App {

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

5.Controller

@RestController
public class IndexController {
    @Value("${user:defalut}")
    private String user;

    @Value("${sex:defalut}")
    private String sex;

    @Value("${blog:defalut}")
    private String blog;

    @RequestMapping("/getUserInfo")
    private String name() {
        return "userName -> " + user + "  sex -> " + sex + "  blog -> " + blog;
    }

}
3.4 测试

1.启动Eureka注册中心项目、ConfigServer项目、ConigClient项目 在这里插入图片描述 2.请求ConfigServer:http://localhost:8882/getUserInfo,获取数据成功。 在这里插入图片描述

4. 动态刷新数据

在SpringCloud中有手动刷新配置文件和实时刷新配置文件两种方式。

  • 手动方式采用actuator端点刷新数据
  • 实时刷新采用SpringCloud Bus消息总线
4.1 actuator端点刷新数据

1.添加Maven依赖:



    org.springframework.boot
    spring-boot-starter-actuator

1.Bootstrap.xml新增开启监控断点:

management:
  endpoints:
    web:
      exposure:
        include: "*"

2.添加@RefreshScope注解,当配置更改时,标有@RefreshScope的Bean将得到特殊处理来生效配置。

@RestController
@RefreshScope
public class IndexController {
    @Value("${user:defalut}")
    private String user;

    @Value("${sex:defalut}")
    private String sex;

    @Value("${blog:defalut}")
    private String blog;

    @RequestMapping("/getUserInfo")
    private String name() {
        return "userName -> " + user + "  sex -> " + sex + "  blog -> " + blog;
    }

}

3.使用PostMan ,Post请求手动刷新:http://127.0.0.1:8882/actuator/refresh 启动刷新器 从Config Server读取。返回的是[],可以看出文件没有修改。 在这里插入图片描述 4.修改config-client-dev.properties文件 在这里插入图片描述 5.使用Postman请求:http://127.0.0.1:8882/actuator/refresh 启动刷新器 从Config Server读取。可以看出三个字段都改变了,返回内容如下: 在这里插入图片描述 6.浏览器输入:http://localhost:8888/config-client-dev.properties,可以看到ConfigServer里面的内容也改了。 在这里插入图片描述 7.浏览器输入:http://127.0.0.1:8882/getUserInfo,可以看出ConfigClient实时修改: 在这里插入图片描述

4.2 SpringCloud Bus实时刷新配置文件

不建议使用,具体的网上百度。

5 总结

代码已上传至Github:有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringCloud-DynamicConfig-Demo 在这里插入图片描述

关注
打赏
1688896170
查看更多评论

杨林伟

暂无认证

  • 2浏览

    0关注

    3183博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2678s