spring-cloud-part 6 : 分布式配置中心 (spring cloud config)

摘自: http://blog.csdn.net/forezp/article/details/69939114  方志朋微博


一 分布式配置中心 spring cloud config
    spring cloud config 为分布式系统中的外部化配置提供服务器和客户端的支持。
  当应用程序通过从开发环境到测试环境和生产环境的部署管道时,您可以管理这些环境之间的配置,并确保应用程序在迁移时需要运行所需的一切。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,并且可以通过各种工具来访问内容。
    在分布式系统中,因为服务数量巨多,因此服务配置文件也很多;为了方便服务配置文件的统一管理,实时更新,所以需要分布式配置中心组件。在spring cloud 微服务架构中,有服务配置中心组件 spring cloud config,它支持配置服务放置在配置服务的内存中(本地),也支持配置服务放在远程git仓库中。
    spring cloud config 组件中,包含两个角色,config server 和 config client ;

二 分布式配置中心代码实现
 2.1 构建一个 config - server(config - server 是从远程git仓库中读取配置文件的配置)
  01. 导包 spring-cloud-starter-config
  o2. 配置 .yaml 配置文件
server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
        # 配置git仓库的地址
          uri: https://github.com/forezp/SpringCloudConfig
        # 配置仓库的路径
          searchPaths: respo
          # 访问git仓库的用户名
          # username:
          # 访问git仓库的用户密码
          # password:
          # 如果git仓库是公开仓库,那么此处的用户名和密码可以不用写
    # 配置仓库的分支
    label: master

    03.  在config-server 的程序主入口类上加上注解@EnableConfigServer 注解开启配置服务器的功能

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

    04.启动程序,访问  http://localhost:8888/config-client/dev

{"name":"config-client","profiles":["dev"],"label":null,"version":"a68876a6211369bae723348d5f8c3defe4a55e04","state":null,"propertySources":[{"name":"https://github.com/forezp/SpringCloudConfig/respo/config-client-dev.properties","source":{"democonfigclient.message":"hello spring io","foo":"foo version 2"}}]}

            访问  http://localhost:8888/config-client/dev  

{"name":"config-client","profiles":["dev"],"label":null,"version":"a68876a6211369bae723348d5f8c3defe4a55e04","state":null,"propertySources":[{"name":"https://github.com/forezp/SpringCloudConfig/respo/config-client-dev.properties","source":{"democonfigclient.message":"hello spring io","foo":"foo version 2"}}]}

      因为 远程git仓库  https://github/forezp/SpringCloudConfig (配置文件中的git地址 uri )中有一个文件 config-client-dev.properties ;

  按照访问规则 http://localhost:8888/{application}/{profile} -->  json  输出文件中的数据

  (或者  如果按照 Http://localhost:8888/application-profile.properties(直接访问文件内容,原样输出))

访问地址:

  http://localhost:8888/----无论访问什么,这一段是固定不变

  如果是访问某一个文件   config-client/dev----json

               config-client-dev.properties   直接显示文件

  如果直接访问一个文件里的某一个属性,如foo   foo/dev(http://localhost:8888/foo/dev)

Http请求地址与资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
   
2.2 构建一个 config - client (config - client 从 config - server 中读取数据)
   
   01. 新建一个spring boot 项目(config - client)
     02. 导包 (勾选 web/config client)
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

     03. 配置 application.yaml 配置文件

        (端口:8881;application.name:config-client ; 配置config的访问地址URI 就是 config - server 的地址及其 label + profile)

server:
  port: 8881

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:8888/

     04.  在主入口类中直接读取config-client-dev.properties中的属性来进行测试 config - client ;

      (注: foo = foo version 2  是 config-client-dev.properties 配置文件中的一个属性)

  @SpringBootApplication
  @RestController
  public class ConfigClientApplication {

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


	@RequestMapping("/hi")
	public String hi(){
		return foo;
	}

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

      5.   启动程序  访问   http://localhost:8881/hi  --> foo version 2

    ( 在config - client -dev.properties 配置文件中还有一个属性 democonfigclient.message = Hello spring io );如果在读取文件时@Value("${ democonfigclient }"),那么执行结果将是   Hello spring io ;

总结:

  config  - server 从本地或者从远程git仓库读取被统一管理的配置文件(直接读取某个文件或者某个文件中的某个属性);

  config - client  则是从 config - server 中获取的这个配置文件的属性;

原文地址:https://www.cnblogs.com/zdj-/p/8276847.html