《springcloud 三》分散式配置中心

x號開發者發表於2019-04-09

Git環境搭建

使用碼雲環境搭建git伺服器端 

碼雲環境地址:https://gitee.com/majie2018

服務端詳解

專案名稱:springboot2.0-config_server

Maven依賴資訊

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <!-- 管理依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--spring-cloud 整合 config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- SpringBoot整合eureka客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>
    <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

application.yml配置

###服務註冊到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka
spring:
  application:
    ####註冊中心應用名稱
    name: config-server
  cloud:
    config:
      server:
        git:
          ###git環境地址
          uri: https://gitee.com/itmayi/config.git
          ####搜尋目錄
          search-paths:
            - config  
      ####讀取分支      
      label: master
####埠號      
server:
  port: 8888

專案啟動

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

@EnableConfigServer 開啟分散式配置中心伺服器端

 

讀取配置檔案資訊 http://127.0.0.1:8888/config-client-dev.properties

 

客戶端詳解

專案名稱:springboot2.0-config_client

 

Maven依賴資訊

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <!-- 管理依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- SpringBoot整合Web元件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <!-- SpringBoot整合eureka客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

bootstrap.yml

spring:
  application:
    ####註冊中心應用名稱
    name:  config-client
  cloud:
    config:
    ####讀取字尾
      profile: dev
      ####讀取config-server註冊地址
      discovery:
        service-id: config-server
        enabled: true
#####    eureka服務註冊地址    
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka    
server:
  port: 8882

讀取配置檔案

@RestController
public class IndexController {
    @Value("${name}")
    private String name;

    @RequestMapping("/name")
    private String name() {
        return name;
    }

}

動態重新整理資料

在SpringCloud中有手動重新整理配置檔案和實時重新整理配置檔案兩種方式。

手動方式採用actuator端點重新整理資料

實時重新整理採用SpringCloud Bus訊息匯流排

 

 

actuator端點重新整理資料

 

Maven依賴資訊

<!-- actuator監控中心 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

Bootstrap.xml新增

開啟監控斷點

management:
  endpoints:
    web:
      exposure:
        include: "*"

生效前提

在需要重新整理的Bean上新增@RefreshScope註解。

@RestController
// @SpringBootApplication
@RefreshScope
public class ConfigClientController {

http://127.0.0.1:8882/actuator/refresh 
    @Value("${itmayieduInfo}")
    private String itmayieduInfo;

當配置更改時,標有@RefreshScope的Bean將得到特殊處理來生效配置

手動重新整理介面

Post請求手動重新整理

 

http://127.0.0.1:8882/actuator/refresh  啟動重新整理器 從cofnig server讀取

 

實際專案中一般都採用手動重新整理,  基於bus匯流排的實時重新整理太佔記憶體,消耗cpu

相關文章