微服務Spring Cloud (五)配置中心 Config Bus

轉載請表明出處 https://blog.csdn.net/Amor_Leo/article/details/87884267 謝謝

Spring Cloud Config概述

Spring Cloud Config 是Sping Cloud下用於分佈式配置管理的組件,分成了兩個角色Config Server和Config Client;Config Server端分佈式配置中心;並對外提供接口方便Config Client訪問,接口使用HTTP的方式對外提供訪問;Config Client通過接口獲取配置文件,然後可以在應用中使用;Config Server存儲/管理的配置文件可以來自本地文件,遠程Git倉庫以及遠程Svn倉庫,默認Git。

Spring Cloud Config搭建

Config Server:

  • pom

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</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>
    
  • yml

    • 默認
    server:
      port: 8888
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://github.com/amor412/spring-cloud-config-repo  # 配置Git倉庫的地址
              username:                                                 # Git倉庫的賬號
              password:                                                 # Git倉庫的密碼
    	  health: # 健康狀況指示器
          	repositories:
            	a-foo: 
              		label: config-label-v2.0
            		name: provide-foo
              		profiles: dev
    eureka:
      client:
        service‐url:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false  
    
    • 佔位符支持
    server:
      port: 8888
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://github.com/amor412/{application}
              username:                                                         # Git倉庫的賬號
              password:                                                         # Git倉庫的密碼
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    eureka:
      client:
        service‐url:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false  
    
    • 模式匹配和多倉庫
    server:
      port: 8888
    spring:
      application:
        name: config-server
      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        
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    eureka:
      client:
        service‐url:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false  
    
    • 子目錄查詢(搜索目錄)
      search-paths 支持佔位符;
    server:
      port: 8888
    spring:
      application:
        name: config-server
      cloud:
        config:
      server:
        git:
          uri: http://github.com/amor412/spring-cloud-config-repo
          search-paths: foo,bar*
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    eureka:
      client:
        service‐url:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false  
    
    • 啓動時clone
      clone-on-start: 設置Config Server啓動時是否克隆git倉庫;
    server:
      port: 8888
    spring:
      application:
        name: config-server
      cloud:
        config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: http://github.com/team-a/spring-cloud-config-repo
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: http://github.com/team-b/spring-cloud-config-repo
            team-c:
                pattern: team-c-*
                uri: http://github.com/team-c/spring-cloud-config-repo
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    eureka:
      client:
        service‐url:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false  
    
    • svn

      • pom
      	<dependency>
      		   <groupId>org.tmatesoft.svnkit</groupId>
      		   <artifactId>svnkit</artifactId>
      		   <version>1.8.10</version>
      	</dependency>
      
      • yml
      server:
        port: 8888
      spring:
        application:
          name: config-server
        cloud:
          config:
            enabled: true
            server:
              svn:
                uri: http://ip:port/svn/Code/Config/app-config
                username: username
                password: password
                default-label: config-repo  #指定svn目錄下的某個文件夾作爲配置倉庫  默認爲trunk
        profiles:
          active: subversion #指定配置中心使用svn管理
      eureka:
        client:
          service‐url:
            defaultZone: http://localhost:8761/eureka
        instance:
          prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
      management:
        endpoints:
          web:
            exposure:
              include: "*"
        security:
          enabled: false  
      
  • Application類上

    @EnableEurekaClient
    @EnableConfigServer
    

ConfigClient:

可以使ConfigServer的端點獲取配置文件的內容,端點與配置文件的映射規則如下:

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

application對應微服務名稱;profile表示使用哪種環境的配置文件,這裏可以是dev,test,prod;label可選的標籤,git倉庫默認值master,svn倉庫默認值是trunk。
provide-foo-dev.properties profile=dev
provide-foo-test.properties profile=test
provide-foo-prod.properties profile=prod

  • pom
  <dependency>
   	<groupId>org.springframework.boot</groupId>
   	<artifactId>spring-boot-starter-web</artifactId>
 	</dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  • Application類上
    @EnableEurekaClient
    
  • yml
    application.yml
server:
  port: 8081

bootstrap.yml

spring:
  application:
    name: provide-foo    # 對應config server所獲取的配置文件的{application}
  cloud:
    config:
      discovery:
        enabled: true                                  # 表示使用服務發現的configserver配置,而不是自己配置的Config Server的uri,默認false
        service-id: config-server  # 指定Config Server在服務發現中的serviceId,默認是configserver
      profile: dev            # profile對應config server所獲取的配置文件中的{profile} 
      label: master           # 指定Git倉庫的分支,對應config server所獲取的配置文件的{label}
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
management:
  endpoints:
    web:
      exposure:
        include: "*"
  security:
    enabled: false  
  • Controller
@RestController
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String hello() {
    return this.profile;
  }
}

ConfigClient配置/refresh手動刷新

  • ConfigServer
    • pom
    	<dependency>
    	   <groupId>org.springframework.boot</groupId>
    	   <artifactId>spring-boot-starter-web</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
    • application.yml
    server:
      port: 8888
    spring:
      application:
        name: config-server
      cloud:
        bus:
          trace:
            enabled: true
        config:
          label: master
          server:
            git:
              uri: https://github.com/SophiaLeo/spring-cloud-config-repo
              username: 
              password: 
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false
    
  • ConfigClient
  • pom
     <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud--starter-config</artifactId>
     </dependency>
     <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
     <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
    
  • Application類上
    @EnableEurekaClient
    
  • yml
    application.yml
server:
  port: 8081

bootstrap.yml:

spring:
  application:
    name: provide-foo    # 對應config server所獲取的配置文件的{application}
  cloud:
    config:
      discovery:
        enabled: true                                  # 表示使用服務發現的configserver配置,而不是自己配置的Config Server的uri,默認false
        service-id: config-server  # 指定Config Server在服務發現中的serviceId,默認是configserver
      profile: dev            # profile對應config server所獲取的配置文件中的{profile} 
      label: master           # 指定git倉庫的分支,對應config server所獲取的配置文件的{label}
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因爲springboot2.1.必須加上,支持訪問/actuator/hystrix.stream
  security:
    enabled: false
  • Controller
@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String hello() {
    return this.profile;
  }
}

修改 provide-foo-dev.properties 內容爲profile=dev-change
訪問http://localhost:8081/profile
post請求訪問http://localhost:8888/actuator/bus-refresh 接口
再次訪問http://localhost:8081/profile

ConfigClient使用SpringCloud Bus配置自動刷新:

  • ConfigServer
    • pom
    	<dependency>
    	   <groupId>org.springframework.boot</groupId>
    	   <artifactId>spring-boot-starter-web</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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
    • application.yml
    server:
      port: 8888
    spring:
      application:
        name: config-server
      cloud:
        bus:
          trace:
            enabled: true
        config:
          label: master
          server:
            git:
              uri: https://github.com/SophiaLeo/spring-cloud-config-repo
              username: 
              password: 
      rabbitmq:
        host: 192.168.0.111
        port: 5672
        username: admin
        password: admin
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka
      instance:
        prefer‐ip‐address: true
    logging:
      level:
        org.springframework.cloud: DEBUG
        org.springframework.boot: DEBUG
    management:
      endpoints:
        web:
          exposure:
            include: "*"
      security:
        enabled: false
    
  • ConfigClient
  • pom
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  • Application類上
    @EnableEurekaClient
    

application.yml

server:
  port: 8081

bootstrap.yml:

spring:
  application:
    name: provide-foo    # 對應config server所獲取的配置文件的{application}
  cloud:
    config:
      discovery:
        enabled: true                                  # 表示使用服務發現的configserver配置,而不是自己配置的Config Server的uri,默認false
        service-id: config-server  # 指定Config Server在服務發現中的serviceId,默認是configserver
      profile: dev            # profile對應config server所獲取的配置文件中的{profile} 
      label: master           # 指定Git倉庫的分支,對應config server所獲取的配置文件的{label}
  rabbitmq:
    host: 192.168.0.111
    port: 5672
    username: admin
    password: admin
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因爲springboot2.1.必須加上,支持訪問/actuator/hystrix.stream
  security:
    enabled: false
  • Application類上
    @EnableEurekaClient
    
  • Controller
@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${profile}")
  private String profile;

  @GetMapping("/profile")
  public String hello() {
    return this.profile;
  }
}
  • 如果是Zuul作爲Config Client 則Application類添加:
   @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
       returnnew ZuulProperties();
    }

修改 provide-foo-dev.properties 內容爲profile=dev-change-bus
訪問http://localhost:8081/profile
post請求訪問http://localhost:8888/actuator/bus-refresh 接口
再次訪問http://localhost:8081/profile
可以藉助Git倉庫的WebHooks

SpringCloud Config 用戶認證與Eureka一起

  • ConfigServer:
    • pom
    <dependencies>
      <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>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
      </dependency>
    </dependencies>
    
    • yml
    server:
      port: 8888
    spring:
      security:
        user:
          name: user                  # 配置登錄的賬號是user
          password: password123       # 配置登錄的密碼是password123
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://github.com/amor412/spring-cloud-config-repo    # 配置Git倉庫的地址
              username:                                                         # Git倉庫的賬號
              password:                                                         # Git倉庫的密碼
    eureka:
      client:
        service‐url:
          defaultZone: http://localhost:8761/eureka
      instance:
    	prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
    
    • Application類上
    @EnableConfigServer
    @EnableEurekaClient
    
  • ConfigClient:
  • pom
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
    </dependencies>
    
    • yml
      • application.yml
        server:
          port: 8080
        
      • bootstrap.yml
        spring:
          application:
            name: provide-foo    # 對應config server所獲取的配置文件的{application}
          cloud:
            config:
              username: user
              password: password123
              profile: dev
              label: master
              discovery:
                enabled: true                                  # 表示使用服務發現的configserver配置,而不是自己配置的Config Server的uri,默認false
                service-id: config-server  # 指定Config Server在服務發現中的serviceId,默認是configserver
        eureka:
          client:
            service‐url:
              defaultZone: http://localhost:8761/eureka
          instance:
            prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
        
    • Application類上
    @EnableEurekaClient
    
    • Controller
    @RestController
    public class ConfigClientController {
      @Value("${profile}")
      private String profile;
    
      @GetMapping("/profile")
      public String hello() {
        return this.profile;
      }
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章