原文地址:https://blog.csdn.net/hanchao5272/article/details/80561199
注意:在做高可用性配置之前,一定要把etc中的host文件做地址映射 :
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
翻譯:Eureka [juəˈri:kə] 我發現了==>服務注冊中心,服務提供者
本章主要對服務注冊中心Eureka組件進行學習,包括三個層次:
簡單配置:簡單的服務注冊中心Eureka Server與服務提供者Eureka Client
高可用配置:高可用的服務注冊中心Eureka Server
認證配置:帶安全認證的服務注冊中心Eureka Server
1.簡單配置
1.1.創建主工程
創建主工程spring-cloud-demo,如何創建參考:Spring-Cloud筆記02:IDEA中構建多模塊的Maven項目。
spring-cloud-demo的pom.xml內容如下:
<?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>pers.hanchao</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--微服務信息-->
<modules>
<!--注冊中心示例 Eureka -->
<module>eureka-server</module>
<module>eureka-hi</module>
</modules>
<!--Spring Boot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--編碼、java版本、Spring Cloud版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
<docker.image.prefix>hanchao5272</docker.image.prefix>
</properties>
<!--基礎的Spring Boot/Cloud依賴-->
<dependencies>
<!--服務監視器組件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--服務注冊與發現組件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--web組件-->
<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>
<!--Spring Cloud的版本序列,代表了一些列組件的默認版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
1.2.創建服務注冊中心
1.2.1.創建子模塊
創建子模塊eureka-server,如何創建參考:[Spring-Cloud筆記02:IDEA中構建多模塊的Maven項目]。
1.2.2.編輯pom.xml
編輯pom.xml,如下:
<?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>pers.hanchao.springclouddemo</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Cloud Eureka Server</description>
<parent>
<groupId>pers.hanchao</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
說明:對於服務注冊中心來說,必不可少的組件是spring-cloud-starter-eureka-server,繼承自主工程。
1.2.3.添加注解@EnableEurekaServer
在啟動類EurekaServerApplication上添加注解@EnableEurekaServer,標識這是一個服務注冊中心。
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
1
2
3
4
5
6
7
8
1.2.4.編輯配置文件
默認生成的配置文件是application.properties,我習慣性將其重命名為application.yml,配置如下:
# 端口號
server:
port: 8761
# 服務注冊相關配置
eureka:
# 服務實例主機名
instance:
hostname: localhost
# 服務提供者配置
client:
# 不進行注冊(當服務注冊中心是單點而非高考用時的配置方式)
registerWithEureka: false
# 不獲取注冊信息(當服務注冊中心是單點而非高考用時的配置方式)
fetchRegistry: false
# 服務注冊中心地址
serviceUrl:
defaultZone: http://${security.user.name}:${server.port}/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
注意:
registerWithEureka: false表示當前服務注冊中心不在其他服務注冊中心進行注冊。
fetchRegistry: false表示當前服務注冊中心不獲取其他服務注冊中心的注冊信息。
以上兩個配置,只有在當服務注冊中心是單點配置時才如此配置,因為單點情況下這兩個值必然為false。
1.2.5.啟動服務注冊中心
啟動服務注冊中心,在瀏覽器訪問http://localhost:8761/,如下:
觀察Instances currently registered with Eureka部分,可以發現,目前沒有可用的實例,表示還沒有服務提供者注冊到當前的服務注冊中心。
1.3.創建服務提供者
1.3.1.創建子模塊
創建子模塊eureka-hi,如何創建參考:[Spring-Cloud筆記02:IDEA中構建多模塊的Maven項目]。
1.3.2.編輯pom.xml
編輯pom.xml,如下:
<?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>pers.hanchao.springclouddemo</groupId>
<artifactId>eureka-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-hi</name>
<description>Demo project for Spring Cloud Eureka Client</description>
<parent>
<groupId>pers.hanchao</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
說明:對於服務提供者來說,必不可少的組件是spring-cloud-starter-eureka-server,繼承自主工程。
1.3.3.添加注解@EnableEurekaServer
在啟動類EurekaHiApplication上添加注解@EnableEurekaClient,標識這是一個服務提供者。
@RestController
@EnableEurekaClient
@SpringBootApplication
public class EurekaHiApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaHiApplication.class, args);
}
/** 獲取端口號 */
@Value("${server.port}")
String port;
/**
* 定義一個簡單接口
* @param name
* @return
*/
@GetMapping("/hi/{name}")
public String home(@PathVariable String name){
return "hi " + name + ",I am from port :" + port;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
上述java類中,還進行了以下編碼:
添加@RestController,相當於@Controller+@ResponseBody,標識這個類對外提供Restful風格的HTTP接口服務。
通過注解@Value獲取配置文件application.yml中的配置。
通過注解@GetMapping("/hi")對外提供一個GET類型的接口。
通過注解@PathVariable來獲取路徑上的參數,從而形成RESTful風格的請求路徑。
1.3.4.編輯配置文件
默認生成的配置文件是application.properties,我習慣性將其重命名為application.yml,配置如下:
# 服務端口號
server:
port: 8763
# 服務名稱,即serviceId
spring:
application:
name: service-hi
# 服務注冊與發現相關配置
eureka:
client:
# 服務注冊地址
serviceUrl:
defaultZone: http://localhost:8761/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
1.3.5.啟動服務提供者
啟動服務eureka-hi,再次在瀏覽器訪問http://localhost:8761/,如下:
觀察Instances currently registered with Eureka部分,可以發現,目前已經存在一個SERVICE-HI的服務。
說明Eureka Client確實注冊到了Eureka Server中心。
在瀏覽器訪問http://localhost:8763/hi/david,得到如下結果:
說明Eureka Client確實能夠提供RESTful風格的服務。
1.4.網絡拓撲圖
簡單的服務注冊中心Eureka Server與服務提供者Eureka Client的網絡拓撲圖,可以總結如下:
這種配置方式有以下致命缺陷:
當成千上萬的服務提供者都向它單節點的服務注冊中心進行注冊時,它的負載是非常高的。
一旦這個單節點的服務注冊中心掛掉,則所有服務提供者的注冊信息都將變得不可用。
2.高可用配置
所謂高可用,就是將微服務集群化,當其中一個節點掛掉之后,其他的節點還能夠提供正常的功能。
高可用配置,可以在前一章節的基礎上進行修改。
為了便於區分,本人重新創建了eureka-server-ha和eureka-hi-ha服務,其配置過程與前面章節一致。
2.1.修改Eureka Server的application.yml
先上Eureka Server的application.yml配置文件:
---
# 高可用節點1的配置
server:
port: 8771
spring:
# 節點1的標簽
profiles: peer1
# 服務名保持一致
application:
name: eureka-ha
eureka:
instance:
hostname: peer1
client:
# 進行注冊(高可用配置、默認配置)
# registerWithEureka: true
# 獲取注冊信息(高可用配置、默認配置)
# fetchRegistry: true
serviceUrl:
# 節點1向節點2/3進行服務注冊
defaultZone: http://peer2:8772/eureka/,http://peer3:8773/eureka/
---
# 高可用節點2的配置
server:
port: 8772
spring:
# 節點2的標簽
profiles: peer2
# 服務名保持一致
application:
name: eureka-ha
eureka:
instance:
hostname: peer2
client:
# 進行注冊(高可用配置、默認配置)
# registerWithEureka: true
# 獲取注冊信息(高可用配置、默認配置)
# fetchRegistry: true
serviceUrl:
# 節點2向節點1/3進行服務注冊
defaultZone: http://peer1:8771/eureka/,http://peer3:8773/eureka/
---
# 高可用節點3的配置
server:
port: 8773
spring:
# 節點3的標簽
profiles: peer3
# 服務名保持一致
application:
name: eureka-ha
eureka:
instance:
hostname: peer3
client:
# 進行注冊(高可用配置、默認配置)
# registerWithEureka: true
# 獲取注冊信息(高可用配置、默認配置)
# fetchRegistry: true
serviceUrl:
# 節點3向節點1/2進行服務注冊
defaultZone: http://peer1:8771/eureka/,http://peer2:8772/eureka/
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
說明:
在yml文件中,通過---來區分多個文件,減少配置文件個數。
高可用配置中,每個節點的端口號不同。
高可用配置中,所有節點的服務名相同,即spring.application.name相同。
高可用配置中,默認情況下,registerWithEureka: true、fetchRegistry: true。
高可用配置中的節點,需要向除此節點之外的節點進行服務注冊。
高可用配置中,設置了每個節點的標簽spring.profiles,通過此標簽來區分到底啟動哪個配置頁。
配置文件中,全部采用駝峰命名
2.3.分別啟動服務注冊中心的三個節點
在Run/Debug Configurations界面中,配置程序參數為--spring.profiles.active=peer1。
然后運行,以此來啟動節點1的服務,配置如下圖所示。
分別配置--spring.profiles.active=peer2、--spring.profiles.active=peer3來啟動另外兩個服務。
在本例中,三個節點的port分別是8771、8772、8773,訪問http://localhost:8771/,如下:
依次訪問http://localhost:8772/、http://localhost:8773/,結果類似。
2.3.修改Eureka Client的application.yml
修改Eureka Client的配置文件。
# 服務地址
server:
port: 8783
# 服務名稱
spring:
application:
name: service-hi-ha
eureka:
client:
serviceUrl:
# 這里只需要執行其中一個服務注冊中心節點即可
defaultZone: http://peer1:8771/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
注意:
這里只需要執行其中一個服務注冊中心節點即可,因為其注冊信息時可以各節點之間復制的。
2.4.高可用測試
啟動eureka-hi-ha,然后刷新http://localhost:8771/,發現注冊中心多了一個SERVICE-HI-HA服務。
關閉服務節點peer1,再次訪問http://localhost:8771/,發現服務不可用,表示節點1真的宕機了。
訪問http://localhost:8772/或者http://localhost:8773/,發現服務可用如下:
注意:
peer1節點並沒有立即從服務列表中消失,這是因為服務注冊中心通過心跳來檢測服務是否存活。
服務宕機之后,服務並不會馬上從服務注冊中心注銷。
只有當超過規定時間還未檢測到服務時,才會注銷服務。
雖然peer1節點掛掉了,但是eureka-hi-ha的注冊信息在其他注冊中心節點還是存在的。
2.5.網絡拓撲圖
高可用的服務注冊中心Eureka Server與服務提供者Eureka Client的網絡拓撲圖,可以總結如下:
當前其中任意節點宕機之后,其他節點上還保存着所有的服務注冊信息。
3.認證配置
無論前面的簡單配置,還是高可用配置,都存在一個很低級的安全問題,那就是誰都可以進行服務注冊。
為了解決這個問題,可以通過安全認證組件security來進行配置。
本章節以1.簡單配置章節為基礎進行修改。
3.1.修改Eureka Server
修改pom.xml
添加security組件:
<dependencies>
<!--添加安全認證-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
1
2
3
4
5
6
7
修改application.yml
# 端口號
server:
port: 8761
# 安全認證
security:
# 開啟基本安全認證
basic:
enabled: true
# 設置用戶名密碼
user:
name: admin
password: admin
# 服務注冊相關配置
eureka:
# 服務實例主機名
instance:
hostname: localhost
# 服務提供者配置
client:
# 不進行注冊(當服務注冊中心是單點而非高考用時的配置方式)
registerWithEureka: false
# 不獲取注冊信息(當服務注冊中心是單點而非高考用時的配置方式)
fetchRegistry: false
# 服務注冊中心地址
serviceUrl:
# 無安全認證的配置
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 安全認證的配置
defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
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
3.2.修改Eureka Client
無需修改pom.xml
修改application.yml
# 服務端口號
server:
port: 8763
# 服務名稱,即serviceId
spring:
application:
name: service-hi
# 服務注冊與發現相關配置
eureka:
client:
# 服務注冊地址
serviceUrl:
# 無安全認證的配置
# defaultZone: http://localhost:8761/eureka/
# 安全認證配置
defaultZone: http://admin:admin@localhost:8761/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3.3.安全認證測試
第一次測試:
依次啟動服務eureka-server和eureka-hi,在瀏覽器訪問:http://localhost:8761/,會出現以下輸入框:
輸入用戶名admin和密碼admin之后,進入注冊中心。在注冊中心,發現了SERVICE-HI已經注冊中心。
第二次測試:
關閉所有服務,關閉瀏覽器,修改Eureka Client的application.yml,將defaultZone還原成http://localhost:8761/eureka/。
依次啟動服務eureka-server和eureka-hi,在瀏覽器訪問:http://localhost:8761/,重新彈出安全認證輸入框。
輸入用戶名admin和密碼admin之后,進入注冊中心。在注冊中心,發現了SERVICE-HI不在注冊中心。
查看eureka-hi的啟動日志,發現報錯:
2018-06-03 23:07:22.261 INFO 17504 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-06-03 23:07:22.341 WARN 17504 --- [ main] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failure with status code 401; retrying on another server if available
2018-06-03 23:07:22.341 ERROR 17504 --- [ main] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-HI/DESKTOP-4J0FACQ:service-hi:8763 - was unable to refresh its cache! status = Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.6.2.jar:1.6.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.6.2.jar:1.6.2]
...
1
2
3
4
5
6
7
8
9
10
經測試,簡單的安全認證配置成功
————————————————
版權聲明:本文為CSDN博主「hanchao5272」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/hanchao5272/article/details/80561199