1. 程式人生 > >史上最簡單的SpringCloud教程 | 第七篇: 高可用的分散式配置中心(Spring Cloud Config)

史上最簡單的SpringCloud教程 | 第七篇: 高可用的分散式配置中心(Spring Cloud Config)

上一篇文章講述了一個服務如何從配置中心讀取檔案,配置中心如何從遠端git讀取配置檔案,當服務例項很多時,都從配置中心讀取檔案,這時可以考慮將配置中心做成一個微服務,將其叢集化,從而達到高可用,架構圖如下:

Azure (3).png

一、準備工作

繼續使用上一篇文章的工程,建立一個eureka-server工程,用作服務註冊中心。

在其pom.xml檔案引入Eureka的起步依賴spring-cloud-starter-eureka-server,程式碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.forezp</groupId>
	<artifactId>eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>


</project>

在配置檔案application.yml上,指定服務埠為8889,加上作為服務註冊中心的基本配置,程式碼如下:

server:
  port: 8889

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

入口類:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

二、改造config-server

在其pom.xml檔案加上EurekaClient的起步依賴spring-cloud-starter-eureka,程式碼如下:

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>

配置檔案application.yml,指定服務註冊地址為http://localhost:8889/eureka/,其他配置同上一篇文章,完整的配置如下:

spring.application.name=config-server
server.port=8888

spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username= your username
spring.cloud.config.server.git.password= your password
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/

最後需要在程式的啟動類Application加上@EnableEureka的註解。

三、改造config-client

將其註冊微到服務註冊中心,作為Eureka客戶端,需要pom檔案加上起步依賴spring-cloud-starter-eureka,程式碼如下:

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</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-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

配置檔案bootstrap.properties,注意是bootstrap。加上服務註冊地址為http://localhost:8889/eureka/

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/

eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=8881


  • spring.cloud.config.discovery.enabled 是從配置中心讀取檔案。
  • spring.cloud.config.discovery.serviceId 配置中心的servieId,即服務名。

這時發現,在讀取配置檔案不再寫ip地址,而是服務名,這時如果配置服務部署多份,通過負載均衡,從而高可用。

依次啟動eureka-servr,config-server,config-client
訪問網址:http://localhost:8889/

Paste_Image.png

訪問http://localhost:8881/hi,瀏覽器顯示:

foo version 3

四、參考資料

優秀文章推薦:


掃碼關注公眾號有驚喜

(轉載本站文章請註明作者和出處 方誌朋的部落格

相關推薦

簡單SpringCloud教程 | : 可用分散式配置中心(Spring Cloud Config)

上一篇文章講述了一個服務如何從配置中心讀取檔案,配置中心如何從遠端git讀取配置檔案,當服務例項很多時,都從配置中心讀取檔案,這時可以考慮將配置中心做成一個微服務,將其叢集化,從而達到高可用,架構圖如下: 一、準備工作 繼續使用上一篇文章的工程,建立一個e

: 分布式配置中心(Spring Cloud Config)

映射 quest 例子 ati word 註冊 mas hid esp 一、簡介 在分布式系統中,由於服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。 在Spring Cloud中,有分布式配置中心組件spring cloud conf

簡單SpringCloud教程可用分散式配置中心(Spring Cloud Config)

最新Finchley版本請訪問: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f7-config/ 或者 http://blog.csdn.net/forezp/article/details/81041

簡單SpringCloud教程分散式配置中心(Spring Cloud Config)

最新Finchley版本: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f6-config/ 或者 http://blog.csdn.net/forezp/article/details/81041028

簡單SpringCloud教程分散式配置中心(Spring Cloud Config)(Finchley版本)

在上一篇文章講述zuul的時候,已經提到過,使用配置服務來儲存各個服務的配置檔案。它就是Spring Cloud Config。 在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring Cloud中,有分散式配置中心元件spri

簡單SpringCloud教程 | : 分散式配置中心(Spring Cloud Config)(Finchley版本)

在上一篇文章講述zuul的時候,已經提到過,使用配置服務來儲存各個服務的配置檔案。它就是Spring Cloud Config。 一、簡介 在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring Cloud中,

簡單SpringCloud教程 | : 可用的服務註冊中心(Finchley版本)

文章 史上最簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現(Eureka) 介紹了服務註冊與發現,其中服務註冊中心Eureka Server,是一個例項,當成千上萬個服務向它註冊的時候,它的負載是非常高的,這在生產環境上是不太合適的,這篇文章主要介紹怎麼

簡單SpringCloud教程 | : 分散式配置中心(Spring Cloud Config)

在上一篇文章講述zuul的時候,已經提到過,使用配置服務來儲存各個服務的配置檔案。它就是Spring Cloud Config。 一、簡介 在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring

簡單SpringCloud教程 | : 可用的服務註冊中心

轉載請標明出處: 原文首發於https://www.fangzhipeng.com/springcloud/2017/07/12/sc-ha-eureka/ 本文出自方誌朋的部落格 文章 史上最簡單的 SpringCloud 教程 | 第一篇: 服務的註冊

企業級 SpringCloud 教程可用分散式配置中心(Spring Cloud Config)

一、準備工作 繼續使用上一篇文章的工程,建立一個eureka-server工程,用作服務註冊中心。 在其pom.xml檔案引入Eureka的起步依賴spring-cloud-starter-eureka-server,程式碼如下 : <?xml version="1.0" e

【轉載】SpringCloud教程 | : 可用的服務註冊中心

文章 史上最簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現(Eureka) 介紹了服務註冊與發現,其中服務註冊中心Eureka Server,是一個例項,當成千上萬個服務向它註冊的時候,它的負載是非常高的,這在生產環境上是不太合適的,這篇

企業級 SpringCloud 教程 (六) 分散式配置中心(Spring Cloud Config)

一、簡介 在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring Cloud中,有分散式配置中心元件spring cloud config ,它支援配置服務放在配置服務的記憶體中(即本地),也支援放在遠端Git倉庫中。在spring cloud

企業級 SpringCloud 教程 (六) 分散式配置中心(Spring Cloud Config

一、簡介在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring Cloud中,有分散式配置中心元件spring cloud config ,它支援配置服務放在配置服務的記憶體中(即本地),也支援放在遠端Git倉庫中。在spring cloud co

SpringCloud微服務雲架構構建B2B2C電子商務平臺之-(可用分散式配置中心(Spring Cloud Config)

講述了一個服務如何從配置中心讀取檔案,配置中心如何從遠端git讀取配置檔案,當服務例項很多時,都從配置中心讀取檔案,這時可以考慮將配置中心做成一個微服務,將其叢集化,從而達到高可用,架構圖如下: 一、準備工作 繼續使用上一篇文章的工程,建立一個eureka-server工程,用作服務註冊中心。 在其

SpringCloud微服務雲架構構建B2B2C電子商務平臺之- (可用分散式配置中心(Spring Cloud Config)

講述了一個服務如何從配置中心讀取檔案,配置中心如何從遠端git讀取配置檔案,當服務例項很多時,都從配置中心讀取檔案,這時可以考慮將配置中心做成一個微服務,將其叢集化,從而達到高可用,架構圖如下: 一、準備工作 繼續使用上一篇文章的工程,建立一個eureka-server工程,用作服務註冊中心。 在

關於SpringCloud微服務雲架構構建B2B2C電子商務平臺之-(可用分散式配置中心(Spring Cloud Config)

講述了一個服務如何從配置中心讀取檔案,配置中心如何從遠端git讀取配置檔案,當服務例項很多時,都從配置中心讀取檔案,這時可以考慮將配置中心做成一個微服務,將其叢集化,從而達到高可用,架構圖如下: 一、準備工作 繼續使用上一篇文章的工程,建立一個eureka-server工程,用作服務註

一起來學Spring Cloud | 章:分散式配置中心(Spring Cloud Config)

上一章節,我們講解了服務閘道器zuul,本章節我們從git和本地兩種儲存配置資訊的方式來講解springcloud的分散式配置中心-Spring Cloud Config。 一、Spring Cloud Config簡介: Spring Cloud Config專案是一個解決分散式系統的配置管理方案。它包

: 可用分散式配置中心(Spring Cloud Config)

上一篇文章講述了一個服務如何從配置中心讀取檔案,配置中心如何從遠端git 或 本地讀取配置檔案,當服務例項很多時,都從配置中心讀取檔案,這時可以考慮將配置中心做成一個微服務,將其叢集化,從而達到高可用,架構圖如下: 一、準備工作 繼續使用前面文章的工程(註冊中心 leopard-eur

: 分散式配置中心(Spring Cloud Config)

在上一篇文章講述zuul的時候,已經提到過,使用配置服 一、簡介 在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring Cloud中,有分散式配置中心元件spring cloud config ,它支援配置服務放在配置服務的記憶

關於SpringCloud微服務雲架構構建B2B2C電子商務平臺之-(六)分散式配置中心(Spring Cloud Config)

一、簡介 在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,實時更新,所以需要分散式配置中心元件。在Spring Cloud中,有分散式配置中心元件spring cloud config ,它支援配置服務放在配置服務的記憶體中(即本地),也支援放在遠端Git倉庫中。在spring cloud