溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringCloud微服務(wù)之Config知識(shí)點(diǎn)有哪些

發(fā)布時(shí)間:2021-05-20 09:38:44 來源:億速云 閱讀:221 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了SpringCloud微服務(wù)之Config知識(shí)點(diǎn)有哪些,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一、什么是Spring Cloud Config?

  • Spring Cloud Config 可以為微服務(wù)架構(gòu)中的應(yīng)用提供集中化的外部配置支持,它分為服務(wù)端和客戶端兩個(gè)部分。

  • Spring Cloud Config 服務(wù)端被稱為分布式配置中心,它是個(gè)獨(dú)立的應(yīng)用,可以從配置倉庫獲取配置信息并提供給客戶端使用。

  • Spring Cloud Config 客戶端可以通過配置中心來獲取配置信息,在啟動(dòng)時(shí)加載配置。

  • Spring Cloud Config 的配置中心默認(rèn)采用Git來存儲(chǔ)配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客戶端來方便地管理和訪問配置信息。

二、搭建GIT環(huán)境

創(chuàng)建倉庫

SpringCloud微服務(wù)之Config知識(shí)點(diǎn)有哪些

創(chuàng)建文件

  • master分支

# config-dev.yml
config:
  info: "config info for dev(master)"
# config-test.yml
config:
  info: "config info for test(master)"
# config-prod.yml
config:
  info: "config info for prod(master)"
  • dev分支

# config-dev.yml
config:
  info: "config info for dev(dev)"
# config-test.yml
config:
  info: "config info for test(dev)"
# config-prod.yml
config:
  info: "config info for prod(dev)"

三、服務(wù)端示例

添加依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

添加配置

  • 啟動(dòng)類

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class SpringcloudConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigServerApplication.class, args);
    }
}
  • application.yml配置文件

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        # 配置存儲(chǔ)配置信息的Git倉庫
        git:
          uri: https://gitee.com/prochick/spring-cloud-config.git
          # Git用戶名
          username: xxx
          # Git密碼
          password: xxx
          # 指定是否開啟啟動(dòng)時(shí)直接從git獲取配置
          clone-on-start: true

eureka:
  instance:
    prefer-ip-address: true
    instance-id: config-server-8888
  client:
    fetch-registry: false
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8010/eureka/

訪問說明

# 獲取配置信息
/{label}/{application}-{profile}
# 獲取配置文件信息
/{label}/{application}-{profile}.yml
  • application

代表應(yīng)用名稱,默認(rèn)為配置文件中的spring.application.name,如果配置了spring.cloud.config.name,則為該名稱

  • label

代表分支名稱,對(duì)應(yīng)配置文件中的spring.cloud.config.label

  • profile

代表環(huán)境名稱,對(duì)應(yīng)配置文件中的spring.cloud.config.profile

測試使用

# 訪問http://localhost:8888/master/config-dev來獲取master分支上dev環(huán)境的配置信息
# 訪問http://localhost:8888/master/config-dev.yml來獲取master分支上dev環(huán)境的配置文件信息
# 訪問http://localhost:8888/master/config-test.yml來獲取master分支上test環(huán)境的配置文件信息
# 訪問http://localhost:8888/dev/config-dev.yml來獲取dev分支上dev環(huán)境的配置文件信息

四、客戶端示例

添加依賴

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

添加配置

  • 配置文件

bootstrap.yml

server:
  port: 9999
spring:
  application:
    name: config-client

  cloud:
    config:
      # 配置中心地址
      uri: http://localhost:8888
      # 分支名稱
      label: master
      # 配置文件名稱
      name: config
      # 配置后綴名稱
      profile: dev

eureka:
  instance:
    prefer-ip-address: true
    instance-id: config-client-9999
    client:
      fetch-registry: false
      register-with-eureka: true
      service-url:
        defaultZone: http://localhost:8010/eureka/
 
# 暴露刷新監(jiān)控 
management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

控制器類

@RestController
@RefreshScope
public class ConfigController {

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

    @GetMapping("/configInfo")
    public String getConfigInfo() {

        return configInfo;
    }
}

測試使用

# 訪問http://localhost:9999/configInfo 可以獲取到dev分支下dev環(huán)境的配置

五、安全認(rèn)證示例

添加依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

添加配置

  • 服務(wù)端配置

server:
  port: 8888
spring:
  application:
    name: config-server
  # 配置存儲(chǔ)配置信息的Git倉庫
  cloud:
    config:
      server:
        git:
          # 訪問地址
          uri: https://gitee.com/prochick/spring-cloud-config.git
          # Git用戶名
          username: xxx
          # Git密碼
          password: xxx
          # 指定是否開啟啟動(dòng)時(shí)直接從git獲取配置
          clone-on-start: true
  # 配置用戶名和密碼
  security: 
    user:
      name: xxx
      password: xxx
  • 客戶端配置

server:
  port: 9999
spring:
  application:
    name: config-client

  cloud:
    config:
      # 配置中心地址
      uri: http://localhost:8888
      # 分支名稱
      label: master
      # 配置文件名稱
      name: config
      # 配置后綴名稱
      profile: dev
      # 配置中心用戶名
      username: xxx
      # 配置中心密碼
      password: xxx

六、集群搭建示例

添加配置

bootstrap.yml

server:
  port: 9999
spring:
  application:
    name: config-client

  cloud:
    config:
      # 分支名稱
      label: master
      # 配置文件名稱
      name: config
      # 配置后綴名稱
      profile: dev
      # 集群綁定
      discovery:
        enabled: true
        service-id: config-server

eureka:
  instance:
    prefer-ip-address: true
    instance-id: config-client-9999
    client:
      fetch-registry: true
      register-with-eureka: true
      service-url:
        defaultZone: http://localhost:8010/eureka/

測試訪問

# 訪問eureka-server

SpringCloud微服務(wù)之Config知識(shí)點(diǎn)有哪些

# 訪問http://localhost:9999/configInfo 可以獲取到dev分支下dev環(huán)境的配置

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringCloud微服務(wù)之Config知識(shí)點(diǎn)有哪些”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI