Java多用戶商城系統B2B2C源碼 (七)高可用的分佈式配置中心(Spring Cloud Config)

講述了一個服務如何從配置中心讀取文件,配置中心如何從遠程git讀取配置文件,當服務實例不少時,都從配置中心讀取文件,這時能夠考慮將配置中心作成一個微服務,將其集羣化,從而達到高可用,架構圖以下:html

Azure (3).png

1、準備工做

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

在其pom.xml文件引入Eureka的起步依賴spring-cloud-starter-eureka-server,代碼以下:git

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?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,加上做爲服務註冊中心的基本配置,代碼以下:複製代碼
1
2
3
4
5
6
7
8
9
10
11
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http: //${eureka.instance.hostname}:${server.port}/eureka/

  入口類:github

1
2
3
4
5
6
7
8
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication. class , args);
}
}

  

2、改造config-server

在其pom.xml文件加上EurekaClient的起步依賴spring-cloud-starter-eureka,代碼以下:web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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

1
2
3
4
5
6
7
8
9
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的註解。apache

3、改造config-client

將其註冊微到服務註冊中心,做爲Eureka客戶端,須要pom文件加上起步依賴spring-cloud-starter-eureka,代碼以下:bootstrap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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/

複製代碼
1
2
3
4
5
6
7
8
9
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

  架構代碼以下:bash

"分佈式b2b <wbr

資料和源碼來源地址架構

Spring Cloud大型企業分佈式微服務雲架構源碼請加企鵝求求:一七九一七四三三八零

相關文章
相關標籤/搜索