天天看點

分布式配置中心SrpingCloud之Config産生背景什麼是分布式配置中心分布式配置中心架構Config架構Config搭建

産生背景

       在微服務中,如果使用傳統的方式進行管理配置檔案,配置檔案管理器非常複雜。如果生産環境配置檔案,可能需要發生改變的時候,重新打war包,重新讀取配置資訊,緩存到JVM記憶體中。

什麼是分布式配置中心

       在微服務中,使用同一個伺服器管理所有服務配置檔案資訊,能夠實作背景可管理,當伺服器正在運作的時候,如果配置檔案需要發生改變,可以實作不需要重新開機伺服器,實時更改配置檔案資訊。

分布式配置中心架構

  • 阿波羅,攜程分布式配置中心,有圖形界面可管理配置檔案資訊,配置檔案資訊存放到資料庫中
  • SpringCloud Config沒有背景可管理的分布式配置中心,配置檔案資訊存放在版本控制器裡(git|svn)
  • 使用Zookeeper實作分布式配置中心,持久節點+事件通知

Config架構

分布式配置中心SrpingCloud之Config産生背景什麼是分布式配置中心分布式配置中心架構Config架構Config搭建
分布式配置中心SrpingCloud之Config産生背景什麼是分布式配置中心分布式配置中心架構Config架構Config搭建

設計一層ConfigServer伺服器的目的就是緩存git上配置檔案資訊

Config搭建

Git環境搭建

使用碼雲環境搭建git伺服器端 

碼雲環境位址:https://gitee.com/JBL_lun/Config

Eureka服務搭建

1、pom檔案引入

<!-- 管理依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--SpringCloud eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <!-- 注意: 這裡必須要添加, 否者各種依賴有問題 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
           

2、配置檔案

###服務端口号
server:
  port: 8100
##定義服務名稱
spring:
  application:
    name: app-lun-eureka
eureka:
  instance:
    ###注冊中心ip位址
    hostname: 127.0.0.1
  client:
    serviceUrl:
    ##注冊位址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    ####因為自己是注冊中心,是否需要将自己注冊給自己的注冊中心(叢集的時候是需要是為true)
    register-with-eureka: false
    ###因為自己是注冊中心, 不需要去檢索服務資訊
    fetch-registry: false
    # 測試時關閉自我保護機制,保證不可用服務及時踢出
  server:
    # 測試時關閉自我保護機制,保證不可用服務及時踢出
    enable-self-preservation: false
    ###   ##剔除失效服務間隔
    eviction-interval-timer-in-ms: 2000
           

2、啟動項

@SpringBootApplication
// @EnableEurekaServer 表示開啟EurekaServer服務 開啟注冊中心
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
           

ConfigServer搭建

1、pom檔案引入

<!-- 管理依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--spring-cloud 整合 config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- SpringBoot整合eureka用戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>
    <!-- 注意: 這裡必須要添加, 否者各種依賴有問題 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
           

2、配置檔案

###服務注冊到eureka位址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
spring:
  application:
    ####注冊中心應用名稱
    name: config-server
  cloud:
    config:
      server:
        git:
          ###git環境位址
          uri: https://gitee.com/JBL_lun/Config.git
          ####搜尋目錄
          search-paths:
            - config
      ####讀取分支
      label: master
####端口号
server:
  port: 8888
           

3、啟動項

@SpringBootApplication
//@EnableConfigServer 開啟分布式配置中心伺服器端
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
           

4、通路http://127.0.0.1:8888/config-client-dev.properties,就能檢視到送出到碼雲上的配置檔案了

分布式配置中心SrpingCloud之Config産生背景什麼是分布式配置中心分布式配置中心架構Config架構Config搭建

ConfigServer搭建

1、pom檔案引入

<!-- 管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web元件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka用戶端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
	<!-- 注意: 這裡必須要添加, 否者各種依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
           

2、配置檔案

spring:
  application:
    ####注冊中心應用名稱
    name:  config-client
  cloud:
    config:
      ####讀取字尾
      profile: dev
      ####讀取config-server注冊位址
      discovery:
        service-id: config-server
        enabled: true
#####    eureka服務注冊位址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
server:
  port: 8882
           

3、啟動項

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

4、擷取配置檔案資訊

@RestController
public class IndexController {
	//冒号後面為預設值,如果找不到傳回預設值
	@Value("${lunInfo:defultValue}")
	private String name;
	@RequestMapping("/name")
	private String name() {
		return name;
	}
}
           
分布式配置中心SrpingCloud之Config産生背景什麼是分布式配置中心分布式配置中心架構Config架構Config搭建

動态重新整理資料

在SpringCloud中有手動重新整理配置檔案和實時重新整理配置檔案兩種方式:

  • 手動方式采用actuator端點重新整理資料
  • 實時重新整理采用SpringCloud Bus消息總線

actuator端點重新整理資料

1、pom檔案新增

<!-- actuator監控中心 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
           

2、配置檔案新增

management:
  endpoints:
    web:
      exposure:
        include: "*"
           

3、在需要重新整理的Bean上添加@RefreshScope注解

@RestController
//當配置更改時,标有@RefreshScope的Bean将得到特殊處理來生效配置
@RefreshScope
public class IndexController {
	//冒号後面為預設值,如果找不到傳回預設值
	@Value("${lunInfo:defultValue}")
	private String name;
	@RequestMapping("/name")
	private String name() {
		return name;
	}
}
           

4、手動重新整理

http://127.0.0.1:8882/actuator/refresh  啟動重新整理器 從cofnig server讀取​​​​​​​

繼續閱讀