Spring Cloud Config 分布式配置中心

环境

安装RabbitMQ(如果不用bus-refresh,可不安装)

Config-Server

  • 创建config-server工程

屏幕快照 2018-10-11 上午9.30.18.png
屏幕快照 2018-10-11 上午9.30.40.png
屏幕快照 2018-10-11 上午9.31.08.png

注:config server和config client两个根据需求选择

  • pom.xml



    4.0.0

    com.example
    server
    0.0.1-SNAPSHOT
    jar

    server
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
        Finchley.SR1
    

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

        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        


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

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

        
            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
            
        
    



  • application.yml

server:
  port: 8888


# 本地
#spring:
#  cloud:
#    config:
#      server:
#        native:
#          search-locations: /Users/lichen/Documents/WorkSpace/Config/

# git
spring:
  cloud:
    config:
      server:
        git:
          #服务器仓库地址
#          uri: https://github.com/xwlichen/smartim-config-resource
          uri: https://github.com/xwlichen/config-server

          #配置文件所在目录
          search-paths: /config-resource
          username: xwlichen
          password: 123456
      #所在分支
      label: master
  • bootstrap.yml(运行在application.yml之前)

spring:
#  profiles:
#    #本地
#    active: native
  cloud:
    config:
      discovery:
        enabled: true
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: root
    password: 123456

management:
  endpoints:
    web:
      exposure:
        # spring-boot-starter-actuator 2.0版本以上得这么配置,将接口暴露出来,如果要暴露全部节点使用include: "*"
        include: "*"
  • application-dev.properties

url=5555555dfdfdfd444444
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456
  • ServerAppApplication

@SpringBootApplication
// 开启config服务中心
@EnableConfigServer
// 发现和注册服务
@EnableDiscoveryClient
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}
  • 启动config-server

屏幕快照 2018-10-11 上午9.53.02.png

注:成功会返回200,但没有返回信息,因为我用的是Finchley.SR1版本,所以用actuator/info

Config-Client

  • 创建config-cleint工程(如上步骤,选择config cleint)

  • pom.xml



    4.0.0

    com.example
    config-client
    0.0.1-SNAPSHOT
    jar

    config-client
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
        Finchley.SR1
    

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

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

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

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

    

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

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



  • application.properties

server.port=8081
management.endpoints.web.exposure.include=refresh
  • bootstrap.properties

spring.cloud.config.uri=http://127.0.0.1:8888
spring.application.name=application
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.bus.trace.enable=true
  • ClientAppApplication

@SpringBootApplication
// 发现和注册服务
//@EnableDiscoveryClient
public class ConfigClientApplication {

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

@RestController
@RefreshScope
public class HiControl {

    @Value("${url}")
    private String datUrl;

    @RequestMapping(value = "/hi")
    public String hi(){
        return datUrl;
    }
}

注:@RefreshScope 用于刷新配置

  • 启动config-client

屏幕快照 2018-10-11 上午10.07.41.png
  • 客服端手动刷新配置

屏幕快照 2018-10-11 上午10.08.28.png

注:返回的url是修改的部分
屏幕快照 2018-10-11 上午10.08.42.png
  • 配置中心服务端自动刷新所有配置

屏幕快照 2018-10-11 上午10.19.48.png

注:虽然返回了204,但是已经刷新了客服端的配置


屏幕快照 2018-10-11 上午10.22.32.png
屏幕快照 2018-10-11 上午10.20.04.png

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