六、Config

一、Config簡介

在分佈式系統中,由於服務數量巨多,爲了方便服務配置文件統一管理,實時更新,所以需要分佈式配置中心組件。在Spring Cloud中,有分佈式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。

二、搭建配置中心服務端

<?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>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>
    <description>分佈式配置中心服務端</description>

    <dependencies>
        <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>
    </dependencies>
</project>
server:
  port: 8767
spring:
  application:
    name: config-server
  cloud:
    # 配置中心的信息
    config:
      server:
        # 使用git進行配置文件的管理
        git:
          # git倉庫的地址
          uri: https://gitee.com/mojianshengwei/spirngcloud-config
          # 倉庫具體的文件夾路徑
          search-paths: config
          # 用戶名和密碼
          username:
          password:
      # 倉庫的分支
      label: master
package com.mo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * EnableConfigServer 開啓配置中心服務端功能
 *
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/15 22:46
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

訪問一下地址,讀取git倉庫的配置文件 

 http://127.0.0.1:8767/config-client.yml

http://127.0.0.1:8767/config/client

 

 三、搭建配置中心客戶端

<?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>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>
    <description>配置中心客戶端</description>

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

</project>
# bootstrap配置文件爲springboot遠程加載的配置文件
spring:
  application:
    name: config-client
  cloud:
    config:
      # 遠程倉庫的分支
      label: master
      # 配置文件的環境,默認環境爲default
      profile: default
      # 配置中心的網址
      uri: http://127.0.0.1:8767/
server:
  port: 7566
package com.mo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/15 23:18
 */
@RestController
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }


    @Value("${mo.user}")
    String user;

    /**
     * 訪問該接口,獲取遠程git倉庫中配置文件的信息
     *
     * @return
     */
    @RequestMapping(value = "/hi")
    public String hi() {
        return user;
    }
}

 

四、搭建集羣化配置中心服務端 

<?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>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-cluster</artifactId>
    <description>配置中心服務端集羣化</description>


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

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

</project>
server:
  port: 0
spring:
  application:
    name: config-cluster
  cloud:
    # 配置中心的信息
    config:
      server:
        # 使用git進行配置文件的管理
        git:
          # git倉庫的地址
          uri: https://gitee.com/mojianshengwei/spirngcloud-config
          # 倉庫具體的文件夾路徑
          search-paths: config
          # 用戶名和密碼
          username:
          password:
      # 倉庫的分支
      label: master
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${random.int}
    prefer-ip-address: true
package com.mo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * 將配置中心註冊到註冊中心,進行多集羣部署,可以進行負載均衡
 *
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/15 23:24
 */
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClusterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClusterApplication.class, args);
    }
}

 五、搭建配置中心客戶端(集羣)

<?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>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-eureka-client</artifactId>
    <description>配置中心客戶端從註冊中心獲取配置文件</description>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
</project>
# bootstrap.yml 遠程配置文件
server:
  port: 0
spring:
  application:
    name: config-eureka-client
  cloud:
    config:
      label: master
      profile: default
      discovery:
        # 開啓從註冊中心獲取配置文件
        enabled: true
        # 配置中心服務的服務名
        service-id: config-cluster
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${random.int}
    prefer-ip-address: true
package com.mo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author x.pan
 * @email [email protected]
 * @date 2020/4/15 23:38
 */
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigEurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigEurekaClientApplication.class, args);
    }

    @Value("${mo.password}")
    String password;

    /**
     * 訪問該接口,獲取遠程git倉庫中配置文件的信息
     *
     * @return
     */
    @RequestMapping(value = "/hi")
    public String hi() {
        return password;
    }
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章