SpringCloud之搭建配置中心


一、搭建config-server

1、引入pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <!-- 配置中心服務端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- Rabbitmq模塊 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    <!-- 解決啟動報Caused by: java.lang.ClassNotFoundException: org.eclipse.jgit.api.TransportConfigCallback -->
    <dependency>  
           <groupId>org.eclipse.jgit</groupId>  
           <artifactId>org.eclipse.jgit</artifactId>  
           <version>4.9.0.201710071750-r</version>  
       </dependency>  
</dependencies>

 

2、配置文件

application.properties

spring.profiles.active=@environment@

# 應用名稱
spring.application.name=crm-config-server # 服務端口 server.port=21200 # 禁用actuator管理端鑒權 management.security.enabled=false # 啟用shutdown host:port/shutdown endpoints.shutdown.enabled=true # 禁用密碼驗證 endpoints.shutdown.sensitive=false

application-local.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:20000/eureka/
  instance:
    lease-renewal-interval-in-seconds: 10
    lease-expiration-duration-in-seconds: 30
    preferIpAddress: true
spring:
  cloud:
    config: 
      server:
        git:
          uri: http://gitlab.xxx.com/crm/crm-config-repo.git
          username: xxx
          password: 'RHqGz$N31'
      # 這里可以寫死,也可以寫成{profile}來動態化 search-paths: local rabbitmq: addresses: amqp://10.18.75.231:5672 username: user_admin password: 222 virtual-host: crm

 

3、Application.java

package com.tomato.crm.config;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class CrmConfigServerApplication { public static void main(String[] args) { SpringApplication.run(CrmConfigServerApplication.class, args); } }

通過屬性:spring.profiles.active 來控制不同環境不同的配置文件(git路徑不同)

 

二、配置倉庫

 采用分功能(例如數據庫的、redis的)、分項目的形式進行划分配置文件

 

三、具體的項目(config client)

1、引入pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

2、bootstrap.properties

spring.application.name=crm-security-service
server.port=23100

# 默認為local,可以通過啟動參數來賦值進行覆蓋
spring.profiles.active=local # 非本地的啟動,注冊中心采用啟動參數傳入,本地測試也在啟動參數中注入 # 此參數請勿放開並提交,因為bootstrap的優先級最高(高於啟動參數),這里不能寫死 #eureka.client.serviceUrl.defaultZone=http://127.0.0.1:20000/eureka/ # 配置中心配置 spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=crm-config-server # 分支, 默認master spring.cloud.config.label=master # 環境,如果配置了則會去讀取common.properties + common-local.properties/yml # spring.cloud.config.profile=local spring.cloud.config.name=common,bus,redis,security-service

這里采用通過注冊中心獲取config server的形式來做多實例負載均衡。

 

四、配置刷新

方法1:

post請求需要刷新的服務的Endpoint是:/refresh

方法2:

通過配置中心發起廣播(MQ)刷新:手動post請求到配置中心服務的:/bus/refresh

方法3:

通過git lab的事件機制來post請求方法2中的url


免責聲明!

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



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