SpringCloud之配置中心(config)的使用Git+數據庫實現


-------------------------目錄------------------------------
一、配置中心應用(Git)
二、配置中心的手動刷新
三、配置中心的自動刷新(Spring Cloud Confifig+Spring Cloud Bus 實現)
四、配置中心應用(數據庫)
-------------------------------------------------------

一、配置中心應用(Git)

Spring Cloud Config是⼀個分布式配置管理⽅案,包含了 Server端和 Client端兩個部分。Config Server是集中式的配置服務,⽤於集中管理應⽤程序各個環境下的配置。 默認使⽤Git存
儲配置⽂件內容,也可以SVN和數據庫,在配置的時候也可以區分開發環境,測試環境和生產環境
配置前說明:

  1、如果在GitHub上建立的倉庫是私有的,那么還要加上spring.cloud.config.server.git.username和spring.cloud.config.server.git.password 這兩個配置

  2、springcloud config 的URL與配置文件的映射關系如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

  3、如果github上建立的目錄下的文件為application-config-dev.yml,那么當啟動配置中心服務器端時,可以通過http://localhost:9006/config/application-config-dev.yml訪問配置文件,如果訪問成功則表示配置中心搭建成功。這里的config是分支名稱

第一步:配置中心服務端

  1、依賴配置

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

  2、配置文件

spring:
application:
#應⽤名稱,會在Eureka中作為服務的id標識(serviceId)
name: config-server9006
cloud:
config:
server:
git:
uri: git@gitee.com:niunafei1/springcloud.git
username: niunafei0315@163.com
password:
label: config
eureka:
client:
#eureka server的路徑
serviceUrl:
#注冊單實例只需要寫一台服務器即可
#集群模式下,也需要寫其它 http://Server其他服務地址:其他服務端口/eureka,如果多個服務需要使用逗號分隔
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
#使⽤ip注冊,否則會使⽤主機名注冊了(此處考慮到對⽼版本的兼容,新版本經過實驗都是ip)
prefer-ip-address: true
#⾃定義實例顯示格式,加上版本號,便於多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@

  3、在啟動類上添加注解,@EnableDiscoveryClient【@EnableDiscoveryClient注解可替換為@EnableEurekaClient】和@EnableConfigServer

package city.albert;

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 niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/9/22  11:44 PM
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServer9006 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServer9006.class, args);
    }
}

訪問:http://localhost:9006/config/application-config-dev.yml

第二步:配置中心客戶端

   1、引入依賴

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

   2、配置文件設置,文件名為bootstrap.yml

   在spring boot中系統文件名bootstrap.yml優先被加載使用,需要在系統加載application.yml之前初始化獲取配置文件。

spring:
  application:
    #應⽤名稱,會在Eureka中作為服務的id標識(serviceId)
    name: gateway-server9002
  cloud:
    config:
      name: application-config #配置⽂件名稱
      profile: dev #后綴名稱
      label: config #分⽀名稱
      uri: http://localhost:9006 #ConfigServer配置中⼼地址

  3、使用可以用@Value("${spring.port}")注入即可

二、配置中心的手動刷新

   實現手動刷新不⽤重啟微服務,只需要⼿動的做⼀些其他的操作(訪問⼀個地址/refresh)刷新,之后再訪問即可此時,客戶端取到了配置中⼼的值,但當我們修改GitHub上⾯的值時,服務端(Confifig Server)能實時獲取最新的值,但客戶端(Confifig Client)讀的是緩存,⽆法實時獲取最新值。Spring Cloud已 經為我們解決了這個問題,那就是客戶端使⽤post去觸發refresh,獲取最新數據。

  1)Client客戶端添加依賴springboot-starter-actuator(已添加)
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  2)Client客戶端bootstrap.yml中添加配置(暴露通信端點),依賴actuator中的對外暴露接口
management:
  endpoints:
    web:
     exposure:
       include: "*"
  3)Client客戶端使⽤到配置信息的類上添加@RefreshScope
  4)⼿動向Client客戶端發起POST請求,http://localhost:8080/actuator/refresh,刷新配置信息
注意:⼿動刷新⽅式避免了服務重啟(流程:Git改配置—>for循環腳本⼿動刷新每個微服務)

三、配置中心的自動刷新(Spring Cloud Confifig+Spring Cloud Bus 實現)

   MQ消息代理,我們還選擇使⽤RabbitMQ,ConfifigServer和ConfifigClient都添加都消息總線的⽀持以及與RabbitMq的連接信息

  1)Confifig Server服務端添加消息總線⽀持( config服務端和客戶端),因為需要依賴RabbitMQ。
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>  
  2)ConfifigServer添加配置( config服務端和客戶端
spring:
  rabbitmq:
    host: 127.0.0.1
    password: guest
    port: 5672
    username: guest
  3)微服務暴露端⼝服務(config服務端)
  a、引入依賴:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

  b、暴露服務

management:
  endpoints:
    web:
     exposure:
       include: "*"
  4)Client客戶端使⽤到配置信息的類上添加@RefreshScope 
    5)重啟各個服務
  更改配置之后:
   全部實力生效:向配置中⼼服務端發送post請求http://localhost:9003/actuator/bus-refresh,各個客戶端配置即可⾃動刷新。
   定向某個服務生效:在發起刷新請求的時候http://localhost:9006/actuator/bus-refresh/lagou-service-resume:8081,即為最后⾯跟上要定向刷新的實例的 服務名:端⼝號即可

四、配置中心應用(數據庫)

  以下顯示基於 “一、配置中心應用(Git)”上的改動
  1、引入數據庫依賴
        <!--連接msql數據庫相關jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

  2、改動配置文件

spring:
  application:
    name: config-server-jdbc
  profiles:
    active: jdbc
  cloud:
    config:
      server:
        default-label: dev
        jdbc:
          sql: SELECT akey , avalue FROM config_server where APPLICATION=? and APROFILE=? and LABEL=?
 # mysql 屬性配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: root

這里主要講下連接配置信息

(1)spring.profiles.active=jdbc,自動實現JdbcEnvironmentRepository。

(2)sql語句自定義,否則會默認為“SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?”,具體可以參考JdbcEnvironmentRepository實現。

(3)本人數據庫建表為config_server,由於key,value和profile是mysql關鍵字,所以我都在最前面加了a。當然表名字段名都可以自定義。

(4) {application} 對應客戶端的"spring.application.name"屬性;

         {aprofile} 對應客戶端的 "spring.profiles.active"屬性(逗號分隔的列表); 和

          {label} 對應服務端屬性,這個屬性能標示一組配置文件的版本.

(5)只要select出來是兩個字段,框架會自動包裝到environment的map<key,value>。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM