二: 分布式配置中心(Spring Cloud Config)

2.1、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件Spring Cloud Config ,它支持配置服务放在配置服务的内存中,也支持放在远程Git仓库中。在Spring Cloud Config 组件中,分两个角色,一是config server,二是config client。

2.2、构建Config Server

创建一个叫congfig-server的Spring Boot项目,引入相关maven包。



    4.0.0

    com.voyer
    config-server
    0.0.1-SNAPSHOT
    jar

    config-server
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
        Finchley.M9
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            aliyun-maven
            http://maven.aliyun.com/nexus/content/groups/public/
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    



在pom.xml中增加阿里云maven库,可以提高下载速度。

        
            aliyun-maven
            http://maven.aliyun.com/nexus/content/groups/public/
        

在默认的启动程序上增加congfigservier注解@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
@RestController
public class ConfigServerApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

接下来要在配置文件里配置一下git相关信息

spring.application.name=config-server
server.port=8888
spring.cloud.config.server.default-application-name=config-server

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/voyer/config-repo
# 配置仓库路径,多个路径用逗号分隔
#spring.cloud.config.server.git.search-paths=aliyun
# 配置仓库的分支
spring.cloud.config.label=master
# 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
# 访问git仓库的用户名
#spring.cloud.config.server.git.username=xxxxoooo
# 访问git仓库的用户密码 
#spring.cloud.config.server.git.password=xxxxoooo

当然,config也支持配置多个Repositories,也可以通过匹配相关格式来获取配置信息,多个匹配格式用逗号分隔,例如:(可以参考官方文档)。

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo

远程仓库中有几个文件


二: 分布式配置中心(Spring Cloud Config)_第1张图片

启动程序访问http://localhost:8080/test/dev返回结果:

{"name":"test","profiles":["dev"],"label":null,"version":"74cdd22f87198215007e8edf138eb59954b66778","state":null,"propertySources":[]}

证明config server可以从远程git仓库获取配置信息。

http请求地址和资源文件映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

2.3、构建一个config client

创建一个叫congfig-client的Spring Boot项目,引入相关maven包。



    4.0.0

    com.voyer
    config-client
    0.0.1-SNAPSHOT
    jar

    config-client
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
        Finchley.M9
    

    
        
            org.springframework.cloud
            spring-cloud-starter-config
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

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




注意此处需要引入spring-boot-starter-web,否则会无法启动(但是可以编译通过),接下来要引入配置文件application.properties

spring.application.name=config-client
#指明远程仓库的分支
spring.cloud.config.label=master
#配置文件环境
spring.cloud.config.profile=dev
# 指明配置服务中心的网址。
spring.cloud.config.uri=http://localhost:8888/
server.port=8889
# 总结一下,config-client项目启动报错,获取不到配置文件里的值:
# 1、首先确定项目config-client与config-service的spring-boot,spring-cloud的依赖版本要一致
# 2、配置文件命名,要遵循一定规则,如果项目config-client里命名为config-client,那配置文件就应该是config-client-**.properties

在程序启动入口增加获取config server配置信息的代码:

@SpringBootApplication
@RestController
public class ConfigClientApplication {
    @Value("${test}")
    String test;
    @RequestMapping(value = "hi")
    public String hi(){
        return test;
    }
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

注意此处的spring.application.name=config-client,当启动程序时,会看到控制台打印的日志:

2018-04-04 16:21:14.904  INFO 32036 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
2018-04-04 16:21:15.795  INFO 32036 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=74cdd22f87198215007e8edf138eb59954b66778, state=null
2018-04-04 16:21:15.796  INFO 32036 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/voyer/config-repo/config-client-dev.yml'}, MapPropertySource {name='https://gitee.com/voyer/config-repo/config-client.yml'}]}

可以看到加载了两个配置文件https://gitee.com/voyer/config-repo/config-client-dev.ymlhttps://gitee.com/voyer/config-repo/config-client.yml,访问http://localhost:8889/hi,会看到返回信息:

bbb

唔,此处笔者远程git仓库的test值为bbb,各位读者可以自己定义。
这就说明,config-client从config-server获取了test的属性,而config-server是从git仓库读取的。

你可能感兴趣的:(二: 分布式配置中心(Spring Cloud Config))