Spring Cloud之配置中心搭建


一、配置中心服務端搭建

1)引入相關Maven坐標

<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>

 

2)加入相關注解

//加上@EnableConfigServer注解開啟配置服務器的功能
@EnableConfigServer
@EnableEurekaClient

 

3)相關配置屬性

server:
  port: 8989
spring:
  application:
    #  需要指明spring.application.name,這個很重要,這在以后的服務與服務之間相互調用一般都是根據這個name
    name: zbq-config-server
#  指定主機名稱
#  cloud:
#    client.hostname: localhost


# 在配置文件中注明自己的服務注冊中心的地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8781/eureka
# 開啟除了主機名也可以通過ip來定義注冊中心地址
  instance:
    prefer-ip-address: true
    ip-address: localhost
    # 定義服務續約任務的調用間隔時間,默認為30秒
    lease-renewal-interval-in-seconds: 30
    # 定義服務失效的時間,默認為90秒
    lease-expiration-duration-in-seconds: 90
#    preferIpAddress: true
#    hostname: ${spring.cloud.client.ipAddress}
#    instance-id: ${spring.cloud.client.ipAddress}:${server.port}





# 配置config中心
spring.cloud.config:
  server:
#  配置git倉庫地址 http方式
#    git.uri: https://github.com/zhangboqing/zbq-config-center.git
#  配置git倉庫地址 ssh方式
    git:
      uri: ssh://git@github.com:zhangboqing/zbq-config-center.git
#  跳過ssh校驗
#      skipSslValidation: true
      ignoreLocalSshSettings: true
#  下面兩個參數沒怎么弄明白,配上不好用,感興趣的可以研究下
#      hostKey: someHostKey
#      hostKeyAlgorithm: ssh-rsa
      privateKey:  |
                    -----BEGIN RSA PRIVATE KEY-----
                    你的私鑰
                    -----END RSA PRIVATE KEY-----
#  配置倉庫路徑下相對搜索位置,可以配置多個
    git.searchPaths: zbq/myconfig1,zbq/myconfig2
# 如果Git倉庫為公開倉庫,可以不填寫用戶名和密碼,如果是私有倉庫需要填寫
#  訪問git倉庫的用戶名
#    username:
#  訪問git倉庫的用戶密碼
#    password:
# 配置倉庫的分支
  label: master
#logging:
logging:
  level:
    root: info
    additivity: off
  file: /data/home/logs/zbq-config-server/zbq-config-server.log

 

二、配置中心客戶端搭建

1)將項目配置遷移到zbq-config-center中(統一存放配置的項目)

  配置文件的命名必須按照下面的規范

  {application}-{profile}.properties 或 {application}-{profile}.properties 

  application和profile代表什么,看4點!!!

2)引入相關Maven坐標

 <!--spring cloud-->
        <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-config</artifactId>
        </dependency>
        <!-- retry -->
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- actuator監控模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

3)加入相關注解

@EnableDiscoveryClient

 

4)相關配置屬性,刪除原來項目的application.yml文件,加入bootstrap.yml配置文件,這樣就可以讀取到遠程的配置文件了

##在配置文件中注明自己的服務注冊中心的地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8781/eureka

# 注意:下面配置必須配置在bootstrap.yml或.properties中
# 訪問配置信息URL與配置文件的映射關系
# /{application}/{profile}/{label}
# /{application}/{profile}.yml
# /{label}/{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.properties
spring:
# 需要指明spring.application.name,這個很重要,這在以后的服務與服務之間相互調用一般都是根據這個name
# 對應配置文件規則中的{application}部分
  application.name: zbq-backend
  cloud.config:
# 對應配置文件規則中的{profile}部分
    profile: dev
# 對應配置文件規則中的{label} 部分
    label: master
# 配置中config-server的地址
# 1.通過URI指定配置中心
#    uri: http://localhost:8783/
# 2.通過注冊中心,來發現注冊中心
    discovery:
# 開啟通過服務訪問Config Server的功能
      enabled: true
#  指定Config Server注冊的服務名
      serviceId: zbq-config-server

# 失敗快速響應與重試
# 開啟客戶端優先判斷Config Server獲取是否正常,並快速響應失敗內容
    fail-fast: true

# 設置重試參數
    retry:
# 下一間隔的乘數
      multiplier: 1.1
# 初始重試間隔時間(單位毫秒),默認為1000
      initial-interval: 1000
# 最大間隔時間,默認2000毫秒
      max-interval: 2000
# 最大重試次數,默認6次
      max-attempts: 6

# 動態刷新配置
# config-client中新增spring-boot-starter-actuator監控模塊,其中包含了/refresh端點實現
# 該端點可用於實現客戶端重新獲取和刷新配置信息
#log
logging:
  level:
    root: info
    additivity: off
  file: /data/home/logs/zbq-backend/zbq-backend.log
  pattern:
    console: "%d{yyyy-MM-dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM