Spring cloud --分佈式配置中心組件Spring Cloud Config

概念:爲了方便服務配置文件統一管理,更易於部署、維護,就需要分佈式配置中心組件了,在spring cloud中,有分佈式配置中心組件spring cloud config,它支持配置文件放在在配置服務的內存中,也支持放在遠程Git倉庫裏。
這裏演示存放到遠程Git倉庫

準備:先在git創建一個倉庫和三個配置文件

在這裏插入圖片描述
在這裏插入圖片描述
UserProvider01-dev.yml 其它兩個略 更改成你想要的切換的配置就行
在這裏插入圖片描述

1.搭建Config Server模塊

在父工程下
在這裏插入圖片描述
新建一個maven項目 名稱爲 Config Server01

2.引入相關依賴
 <dependency>
	  <groupId>org.springframework.cloud</groupId>
	  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.寫一個啓動程序

注意:要添加@EnableConfigServer註解,開啓Config Server

@SpringBootApplication
@EnableConfigServer
public class ConfigServer001Application {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServer001Application.class, args);
	}
}
4.修改配置文件application.yml配置服務信息以及git信息
server:
  port: 7001
spring:
  application:
    name: CONFIGSERVER
  cloud:
    config:
      server:
        git:
          #git倉庫地址
          uri: https://github.com/xuexi007/java1203-configserver/
          #配置文件在git倉庫的存放文件夾路徑
          search-paths: config
          username: xuexi007    #github賬戶名
          password: ********    #github密碼

5.測試地址

http://localhost:7001/UserProvdier01/pro/
http://localhost:7001/UserProvdier01/test/
http://localhost:7001/UserProvdier01/dev/
如果顯示下面內容就說明成功了,然後進行下面的操作
在這裏插入圖片描述

Config Client客戶端調用配置中心配置

(服務提供方)

1.導入依賴
<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.在resources文件夾下新建配置文件bootstrap.yml並進行配置

在這裏插入圖片描述
注意:application.yml不要刪掉,內容清空就行,否則後面可能會讀取不到

spring:
  application:
    name: CONFIGSERVER   
  cloud:
    config:
      uri: http://localhost:7001
      profile: test   #指明是哪個環境
      label: master   #分支
3.啓動測試就行

這裏就有個問題,每次修改git上的配置信息後,都要重啓服務器才能進行刷新,那麼怎麼實現動態刷新呢?


修改配置文件實現動態刷新

(在服務提供方)

1.導入依賴
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
2.修改UserProvdier01的application.yml增加配置,打開refresh端點
management:
 endpoints:
   web:
     exposure:
       include: refresh,health,info
3.在要提取配置中信息的controller上增加註解:@RefreshScope

提取配置中的變量用以下方法
@Value("${ProviderVersion}")
private String ProviderVersion;

4.使用postMan發出post請求到如下地址:

http://localhost:9009/actuator/refresh (注意發出post請求)

5.刷新頁面可以看到內容動態改變了

搭建Spring Cloud Config集羣

圖解思路:
在這裏插入圖片描述
在這裏插入圖片描述
git倉庫
在這裏插入圖片描述
config下面
在這裏插入圖片描述

ConfigServer001

1.編寫啓動類
package com.offcn;
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;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient   //允許註冊發現
public class configserver01Starter {
    public static void main(String[] args) {
        SpringApplication.run(configserver01Starter.class,args);
    }
}
2.配置application.yml 文件

在這裏插入圖片描述

3.pom依賴
    <dependencies>
        <!--引入spring-config-server所需的依賴包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!--引入Eureka Client依賴包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

ConfigServer002

1.啓動類
package com.offcn;

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;

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

在這裏插入圖片描述

3.pom.xml文件
<dependencies>
    <!--引入spring-config-server所需的依賴包-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    <!--引入Eureka Client依賴包-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

到這Spring Cloud Config的集羣就配置好了,啓動檢查一下
出現這個就說明啓動成功了
在這裏插入圖片描述

Config Client客戶端調用配置中心配置

創建bootstrap.yml文件並進行配置,application.yml文件保留,內容爲空

在這裏插入圖片描述
啓動測試
查看控制檯 連接到了7002
在這裏插入圖片描述
重連後連接到了7001
在這裏插入圖片描述
可以發現,默認是輪詢機制

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