1. 程式人生 > 實用技巧 >SpringCloud config分散式配置中心

SpringCloud config分散式配置中心

分散式系統面臨的配置問題

springcloud聽過的config可以完美的解決上述的問題,springcloud config是什麼

是什麼

怎麼玩‘

能幹嘛

集中管理配置檔案

不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release

執行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置檔案,服務會向配置中心統一拉取配置自己的資訊

當配置發生變動時,服務不需要重啟即可感知到配置的變化並應用新的配置

將配置資訊以REST介面的形式暴露

與Github整合配置

由於SpringCloud Config預設使用Git來儲存配置檔案(也有其它方式,比如支援svn和本地檔案,但最推薦的還是Git,而且使用的是http/https訪問的形式)

Config服務端配置與測試

在自己的github建立一個倉庫,並在本地下載

這些前提條件搞好了之後建立cloud-config-center-3344服務端

pom檔案引入依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-conmmon</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--springcloud-config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--SpringBoot熱部署配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

 application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/changtoufadeguniang/springcloud-config
          search-paths:
            - springcloud-config
      label: main
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

  啟動類

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

 測試http://localhost:3344/main/application-dev.yml

Config客戶端配置與測試 建立cloud-config-client-3355客戶端

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-conmmon</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--springcloud-config-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--SpringBoot熱部署配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 bootstrap.yml

server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config:
      label: main
      name: application
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka
啟動類
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class,args);
    }
}

  編寫controller獲取github application-dev.yml的內容

@RestController
@RequestMapping("/client")
public class ConfigClientController {


    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/getConfigInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

  測試http://localhost:3355/client/getConfigInfo

發現問題,當github 上application-dev.yml的內容發生改變後,在不重啟服務的情況下,服務端3344內容可以實時更新,但是3355獲得的內容沒有發生改變。只有重啟3355客戶端才會重新整理改變

Config客戶端之動態重新整理

cloud-config-client-3355客戶端

pom檔案加入依賴

  <!--config客戶端動態重新整理需要引入的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

 修改bootstrap.yml配置檔案暴露埠

server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config:
      label: main
      name: application
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka
#暴露埠
management:
  endpoints:
    web:
      exposure:
        include: "*"

  controller添加註解@RefreshScope

@RestController
@RequestMapping("/client")
@RefreshScope
public class ConfigClientController {


    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/getConfigInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

  一切搞好之後重啟服務,然後修改github上配置檔案的內容,在訪問http://localhost:3355/client/getConfigInfo內容還是沒有更新

這時需要先發送一次post請求,http://localhost:3355/actuator/refresh重新整理一下服務

再次請求

內容已經發生了改變