国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

SpringCloud微服務(wù)之Config知識總結

發(fā)布時(shí)間:2021-07-05 18:40 來(lái)源:腳本之家 閱讀:0 作者:ProChick 欄目: 開(kāi)發(fā)技術(shù)

目錄

一、什么是Spring Cloud Config?

  • Spring Cloud Config 可以為微服務(wù)架構中的應用提供集中化的外部配置支持,它分為服務(wù)端和客戶(hù)端兩個(gè)部分。
  • Spring Cloud Config 服務(wù)端被稱(chēng)為分布式配置中心,它是個(gè)獨立的應用,可以從配置倉庫獲取配置信息并提供給客戶(hù)端使用。
  • Spring Cloud Config 客戶(hù)端可以通過(guò)配置中心來(lái)獲取配置信息,在啟動(dòng)時(shí)加載配置。
  • Spring Cloud Config 的配置中心默認采用Git來(lái)存儲配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客戶(hù)端來(lái)方便地管理和訪(fǎng)問(wèn)配置信息。

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

創(chuàng )建倉庫

創(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ù)端示例

添加依賴(lài)

<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)類(lèi)
@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:
        # 配置存儲配置信息的Git倉庫
        git:
          uri: https://gitee.com/prochick/spring-cloud-config.git
          # Git用戶(hù)名
          username: xxx
          # Git密碼
          password: xxx
          # 指定是否開(kāi)啟啟動(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/

訪(fǎng)問(wèn)說(shuō)明

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

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

  • label

代表分支名稱(chēng),對應配置文件中的spring.cloud.config.label

  • profile

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

測試使用

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

四、客戶(hù)端示例

添加依賴(lài)

<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
      # 分支名稱(chēng)
      label: master
      # 配置文件名稱(chēng)
      name: config
      # 配置后綴名稱(chēng)
      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/
 
# 暴露刷新監控 
management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

控制器類(lèi)

@RestController
@RefreshScope
public class ConfigController {

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

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

        return configInfo;
    }
}

測試使用

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

五、安全認證示例

添加依賴(lài)

<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
  # 配置存儲配置信息的Git倉庫
  cloud:
    config:
      server:
        git:
          # 訪(fǎng)問(wèn)地址
          uri: https://gitee.com/prochick/spring-cloud-config.git
          # Git用戶(hù)名
          username: xxx
          # Git密碼
          password: xxx
          # 指定是否開(kāi)啟啟動(dòng)時(shí)直接從git獲取配置
          clone-on-start: true
  # 配置用戶(hù)名和密碼
  security: 
    user:
      name: xxx
      password: xxx
  • 客戶(hù)端配置
server:
  port: 9999
spring:
  application:
    name: config-client

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

六、集群搭建示例

添加配置

bootstrap.yml

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

  cloud:
    config:
      # 分支名稱(chēng)
      label: master
      # 配置文件名稱(chēng)
      name: config
      # 配置后綴名稱(chēng)
      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/

測試訪(fǎng)問(wèn)

# 訪(fǎng)問(wèn)eureka-server

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

到此這篇關(guān)于SpringCloud微服務(wù)之Config知識總結的文章就介紹到這了,更多相關(guān)SpringCloud Config內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

97AV麻豆蜜桃一区二区| 日本熟妇中文字幕三级| 日日躁夜夜躁狠狠躁超碰97| 国产97色在线 | 日韩| 无码人妻一区二区三区AV| 成人午夜亚洲精品无码区|