一、 什么是服務注冊中心
服務注冊中心是服務實現服務化管理的核心組件,類似於目錄服務的作用,主要用來存儲服務信息,譬如提供者url串、路由信息等。服務注冊中心是SOA架構中最基礎的設施之一。
1 服務注冊中心的作用
1,服務的注冊
2,服務的發現
2. 常見的注冊中心有哪些
1,Dubbo 的注冊中心Zookeeper
2,Sringcloud的注冊中心Eureka
3. 服務注冊中心解決了什么問題
- 服務管理;
- 服務的依賴關系管理;
4. 什么是Eureka注冊中心
Eureka是Netflix開發的服務發現組件,本身是一個基於REST的服務。Spring Cloud將它集成在其子項目spring-cloud-netflix中,以實現Spring Cloud的服務注冊與發現,同時還提供了負載均衡、故障轉移等能力。
5. Eureka注冊中心三種角色
5.1 Eureka Server
通過Register、Get、Renew等接口提供服務的注冊和發現。
5.2 Application Service (Service Provider)
服務提供方
把自身的服務實例注冊到Eureka Server中
5.3 Application Client (Service Consumer)
服務調用方
通過Eureka Server 獲取服務列表,消費服務。
二、 Eureka入門案例
1、創建項目
2、pom.xml文件如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.9.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.angei</groupId> 12 <artifactId>eurekaserver</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>eurekaserver</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 </dependency> 33 </dependencies> 34 35 <dependencyManagement> 36 <dependencies> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-dependencies</artifactId> 40 <version>${spring-cloud.version}</version> 41 <type>pom</type> 42 <scope>import</scope> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build> 55 56 </project>
提示:如果IDEA加載pom.xml時一直下載失敗,可以在pom.xml中添加如下配置,使其從國內阿里雲鏡像中下載相關內容,下載速率將會大幅提升。
<repositories> <repository> <id>aliyun</id> <name>aliyun</name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories>
3、添加application.yml全局配置文件
1 server: 2 port: 8761 3 eureka: 4 instance: 5 appname: provider-service 6 hostname: localhost 7 client: 8 service-url: 9 defaultZone: 10 http://localhost:8761/eureka/ 11 register-with-eureka: false 12 fetch-registry: false
說明:
1 server: 2 port: 8761 3 eureka: 4 instance: 5 #服務名,默認取 spring.application.name 配置值,如果沒有則為 unknown 6 appname: provider-service 7 #設置當前實例的主機名稱 8 hostname: localhost 9 client: 10 service-url: 11 #指定服務注冊中心地址,類型為 HashMap,並設置有一組默認值, 12 #默認的Key為 defaultZone;默認的Value為 http://localhost:8761/eureka , 13 #如果服務注冊中心為高可用集群時,多個注冊中心地址以逗號分隔。 14 defaultZone: 15 http://localhost:8761/eureka/ 16 17 #是否將自己注冊到Eureka-Server中,默認的為true 18 register-with-eureka: false 19 20 #是否從Eureka-Server中獲取服務注冊信息,默認為true 21 fetch-registry: false
細節注意:
4、修改啟動類
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 4 5 @SpringBootApplication 6 @EnableEurekaServer 7 public class EurekaserverApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(EurekaserverApplication.class, args); 11 } 12 13 }
5、通過瀏覽器訪問Eureka-Server服務管理平台
三、 搭建高可用Eureka注冊中心(Eureka集群)
在微服務架構中,注冊中心是一個必不可少的組件,前面我們搭建的注冊中心只適合本地開發使用,在生產環境必須搭建一個集群來保證高可用。
Eureka的集群搭建很簡單,每一台Eureka都需要在配置中指定另外N個Eureka的地址就可以。
在Eureka服務端的配置項eureka.client.serviceUrl.defaultZone中地址那一行要使用ip或域名,由於這里是在本地一台機子上模擬集群環境,ip地址都一樣,所以通過更改本地host文件的方式創建三個可用的域名。
1.修改C:\Windows\System32\drivers\etc\hosts文件:
127.0.0.1 server1 127.0.0.1 server2 127.0.0.1 server3
2.本地刷新dns:
指令: ipconfig /flushdns
3.創建3個application.yml
注意:在集群環境下,每一個Eureka都需要將自己注冊到Eureka-Server中(register-with-eureka默認就是true故可不配置)、也要從Eureka-Server中獲取服務注冊信息(fetch-registry默認就是true故可不配置)。
application-p8761.yml
server: port: 8761 eureka: instance: #服務名,默認取 spring.application.name 配置值,如果沒有則為 unknown appname: provider-service #設置當前實例的主機名稱 hostname: server1 client: service-url: #指定服務注冊中心地址,類型為 HashMap,並設置有一組默認值, #默認的Key為 defaultZone;默認的Value為 http://localhost:8761/eureka , #如果服務注冊中心為高可用集群時,多個注冊中心地址以逗號分隔,地址形式是ip或域名:端口號 defaultZone: http://server2:8762/eureka/,http://server3:8763/eureka/
application-p8762.yml
server: port: 8762 eureka: instance: appname: provider-service hostname: server2 client: service-url: defaultZone: http://server1:8761/eureka/,http://server3:8763/eureka/
application-p8763.yml
server: port: 8763 eureka: instance: appname: provider-service hostname: server3 client: service-url: defaultZone: http://server1:8761/eureka/,http://server2:8762/eureka/
4.然后需要注意,application-xxxx.yml不是默認的配置形式,是無法被自動識別的,可以通過配置spring.profiles.active的方式指定運行時加載。
5.分別啟動pEurekaServerApplication-8761, EurekaServerApplication-8762, EurekaServerApplication-8763
顯示如下:
6.同時,在客戶端的配置項中設置服務注冊中心地址時,設置為哪一個都可以,最好都寫上(用逗號隔開),這樣當其中一個節點掛了,客戶端還會自動嘗試連接其他節點。
server: port: 80 spring: application: name: order-service eureka: client: service-url: #設置服務注冊中心地址 defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
四、 在Eureka注冊中心中構建客戶端服務
Eureka客戶端開發要點:
①、maven 依賴 spring-cloud-starter-netflix-eureka-client;
②、application.yml 配置 eureka.client.service-url.defaultZone;
③、入口類増加 @EnableEurekaClient;
1、創建項目
2、pom.xml文件如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.9.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.angei</groupId> 12 <artifactId>eureka-client</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>eureka-client</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.cloud</groupId> 29 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-test</artifactId> 35 <scope>test</scope> 36 </dependency> 37 </dependencies> 38 39 <dependencyManagement> 40 <dependencies> 41 <dependency> 42 <groupId>org.springframework.cloud</groupId> 43 <artifactId>spring-cloud-dependencies</artifactId> 44 <version>${spring-cloud.version}</version> 45 <type>pom</type> 46 <scope>import</scope> 47 </dependency> 48 </dependencies> 49 </dependencyManagement> 50 51 <build> 52 <plugins> 53 <plugin> 54 <groupId>org.springframework.boot</groupId> 55 <artifactId>spring-boot-maven-plugin</artifactId> 56 </plugin> 57 </plugins> 58 </build> 59 60 </project>
3、添加application.yml全局配置文件
1 server: 2 port: 80 3 spring: 4 application: 5 name: order-service 6 eureka: 7 client: 8 service-url: 9 #設置服務注冊中心地址 10 defaultZone: 11 http://localhost:8761/eureka/
4、修改啟動類
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 4 5 @SpringBootApplication 6 @EnableEurekaClient 7 public class EurekaClientApplication { 8 public static void main(String[] args) { 9 SpringApplication.run(EurekaClientApplication.class, args); 10 } 11 }
5、測試
1 package com.angei.eurekaclient.Controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 @Controller 9 public class orderController { 10 11 @GetMapping("/order/{id}") 12 @ResponseBody 13 public String findById(@PathVariable("id") Integer orderId){ 14 if(orderId==2019){ 15 return "{\"Id\":1,\"Title\":\"餓了么訂單\"}"; 16 }else{ 17 return null; 18 } 19 } 20 }
先啟動服務器,再啟動客戶端:
五、 在高可用的Eureka注冊中心中模擬構建provider服務和consumer服務
(一)搭建provider提供服務
1.搭載環境
配置application.properties,將服務注冊到注冊中心。
spring.application.name=eureka-provider server.port=9090 #設置服務注冊中心地址 eureka.client.serviceUrl.defaultZone=http://server1:8761/eureka/,http://server2:8762/eureka/,http://server3:8763/eureka/
2.模擬提供服務
創建User實體類:
1 import lombok.Getter; 2 import lombok.Setter; 3 import java.io.Serializable; 4 5 @Getter 6 @Setter 7 public class User implements Serializable { 8 9 private int id; 10 11 private String name; 12 13 public User() { 14 } 15 16 public User(int id, String name) { 17 this.id = id; 18 this.name = name; 19 } 20 21 @Override 22 public String toString() { 23 return "學號:" + this.id + "\t姓名:" + this.name; 24 } 25 }
創建userController:
1 import com.example.demo.pojo.User; 2 import org.springframework.stereotype.Controller; 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 import java.util.ArrayList; 7 8 @Controller 9 @RestController 10 public class userController { 11 12 @RequestMapping("/getAllUser") 13 public ArrayList<User> getAllUser() { 14 ArrayList<User> list = new ArrayList<>(); 15 list.add(new User(2018, "Benjieming")); 16 list.add(new User(2019, "Huangsi")); 17 list.add(new User(2020, "Yangyi")); 18 return list; 19 20 } 21 }
3.啟動項目並檢測
(二) 搭建consumer調用服務
1.創建項目,過程同provider,最終項目結構如下:
application.yml內容如下:
server: port: 9091 spring: application: name: eureka-consumer eureka: client: service-url: #設置服務注冊中心地址 defaultZone: http://server1:8761/eureka/,http://server2:8762/eureka/,http://server3:8763/eureka/
2.創建userService和userController
userService.java
1 package com.angei.eurekaclient.Service; 2 3 import com.angei.eurekaclient.pojo.User; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.cloud.client.ServiceInstance; 6 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 7 import org.springframework.core.ParameterizedTypeReference; 8 import org.springframework.http.HttpMethod; 9 import org.springframework.http.ResponseEntity; 10 import org.springframework.stereotype.Service; 11 import org.springframework.web.client.RestTemplate; 12 13 import java.util.List; 14 15 @Service 16 public class userService { 17 18 @Autowired 19 private LoadBalancerClient loadBalancerClient;//ribbon負載均衡器 20 21 public List<User> getUsers() { 22 23 //選擇調用的服務的名稱 24 //ServiceInstance類封裝了服務的基本信息,如 IP,端口等 25 ServiceInstance si = this.loadBalancerClient.choose("eureka-provider"); 26 //拼接訪問服務的URL 27 StringBuffer sb = new StringBuffer(); 28 //http://server1:8761/getAllUser 29 System.out.println("host:" + si.getHost()); 30 System.out.println("Port:" + si.getPort()); 31 sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/getAllUser"); 32 33 34 //springMVC RestTemplate 35 RestTemplate rt = new RestTemplate(); 36 37 ParameterizedTypeReference<List<User>> type = new ParameterizedTypeReference<List<User>>() { 38 }; 39 40 //ResponseEntity:封裝了返回值信息 41 ResponseEntity<List<User>> response = rt.exchange(sb.toString(), HttpMethod.GET, null, type); 42 List<User> list = response.getBody(); 43 return list; 44 } 45 }
注意:這里是向注冊中心獲取服務並拉取信息。
userController.java
1 package com.angei.eurekaclient.Controller; 2 3 import com.angei.eurekaclient.Service.userService; 4 import com.angei.eurekaclient.pojo.User; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import javax.annotation.Resource; 9 import java.util.List; 10 11 @RestController 12 public class userController { 13 14 @Resource 15 private userService userservice; 16 17 @RequestMapping("/getAllUsers") 18 public List<User> getUsers(){ 19 return userservice.getUsers(); 20 } 21 }
3.啟動項目並測試
五、 Eureka注冊中心架構原理
1、Eureka架構圖
說明:
Register(服務注冊):把自己的IP和端口注冊給Eureka。
Renew(服務續約):發送心跳包,每30秒發送一次。告訴Eureka自己還活着。
Cancel(服務下線):當provider關閉時會向Eureka發送消息,把自己從服務列表中刪除。防止consumer調用到不存在的服務。
Get Registry(獲取服務注冊列表):獲取其他服務列表。
Replicate(集群中數據同步):eureka集群中的數據復制與同步。
Make Remote Call(遠程調用):完成服務的遠程調用。
2、CAP原則
CAP原則又稱CAP定理,指的是在一個分布式系統中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分區容錯性),三者不可兼得。
CAP由Eric Brewer在2000年PODC會議上提出。該猜想在提出兩年后被證明成立,成為我們熟知的CAP定理。
3、基於分布式CAP定理,分析注冊中心兩大主流框架:分析Eureka與Zookeeper的區別
六、 Eureka優雅停服
1、在什么條件下,Eureka會啟動自我保護?
什么是自我保護模式
1,自我保護的條件
一般情況下,微服務在Eureka上注冊后,會每30秒發送心跳包,Eureka通過心跳來判斷服務是否健康,同時會定期刪除超過90秒沒有發送心跳服務。
2,有兩種情況會導致Eureka Server收不到微服務的心跳
a.是微服務自身的原因;
b.是微服務與Eureka之間的網絡故障;
通常微服務的自身的故障關閉只會導致個別服務出現故障,一般不會出現大面積故障,而網絡故障通常會導致Eureka Server在短時間內無法收到大批心跳。
考慮到這個區別,Eureka設置了一個閥值,當判斷掛掉的服務的數量超過閥值時,Eureka Server認為很大程度上出現了網絡故障,將不再刪除心跳過期的服務。
3,那么這個閥值是多少呢?
15分鍾之內故障率是否低於85%;
Eureka Server在運行期間,會統計心跳失敗的比例在15分鍾內是否低於85%,
這種算法叫做Eureka Server的自我保護模式。
2、為什么要啟動自我保護?
為什么要自我保護
1,因為同時保留"好數據"與"壞數據"總比丟掉任何數據要更好,當網絡故障恢復后,這個Eureka節點會退出"自我保護模式"。
2,Eureka還有客戶端緩存功能(也就是微服務的緩存功能)。即便Eureka集群中所有節點都宕機失效,微服務的Provider和Consumer都能正常通信。
3,微服務的負載均衡策略會自動剔除死亡的微服務節點。
3、如何關閉自我保護?
修改Eureka Server配置文件
#關閉自我保護:true為開啟自我保護,false為關閉自我保護 eureka.server.enableSelfPreservation=false #清理間隔(單位:毫秒,默認是60*1000) eureka.server.eviction.interval-timer-in-ms=60000
4、☆如何優雅停服?(springboot - version: 2.1.0.RELEASE及以上)
第1步:不需要在Eureka Server中再配置關閉自我保護的相關配置。
#如果在剛剛的演示中添加了如下配置,則全部刪去。 #關閉自我保護:true為開啟自我保護,false為關閉自我保護 eureka.server.enableSelfPreservation=false #清理間隔(單位:毫秒,默認是60*1000) eureka.server.eviction.interval-timer-in-ms=60000
第2步:在服務中添加actuator.jar依賴包。
<!-- springboot監控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
第3步:修改配置文件,添加如下內容:
1 management: 2 endpoint: 3 shutdown: 4 enabled: true 5 endpoints: 6 web: 7 exposure: 8 include: "*"
management.endpoint.shutdown.enabled
這是說是否啟動/shutdown端點,默認是false,management.endpoints.web.exposure.include
這是說公開所有端點。
第4步:發送一個關閉服務的URL請求。注意:請求方式必須是post請求。
請求格式:http://待關閉微服務ip:端口號/actuator/shutdown
如:http://127.0.0.1:9090/actuator/shutdown
(1)方式一:以下是通過一個HttpClientUtil工具類來發送一個post請求。
1 public class HttpClientUtil { 2 3 public static String doGet(String url, Map<String, String> param) { 4 5 // 創建Httpclient對象 6 CloseableHttpClient httpclient = HttpClients.createDefault(); 7 8 String resultString = ""; 9 CloseableHttpResponse response = null; 10 try { 11 // 創建uri 12 URIBuilder builder = new URIBuilder(url); 13 if (param != null) { 14 for (String key : param.keySet()) { 15 builder.addParameter(key, param.get(key)); 16 } 17 } 18 URI uri = builder.build(); 19 20 // 創建http GET請求 21 HttpGet httpGet = new HttpGet(uri); 22 23 // 執行請求 24 response = httpclient.execute(httpGet); 25 // 判斷返回狀態是否為200 26 if (response.getStatusLine().getStatusCode() == 200) { 27 resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 28 } 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } finally { 32 try { 33 if (response != null) { 34 response.close(); 35 } 36 httpclient.close(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 return resultString; 42 } 43 44 public static String doGet(String url) { 45 return doGet(url, null); 46 } 47 48 public static String doPost(String url, Map<String, String> param) { 49 // 創建Httpclient對象 50 CloseableHttpClient httpClient = HttpClients.createDefault(); 51 CloseableHttpResponse response = null; 52 String resultString = ""; 53 try { 54 // 創建Http Post請求 55 HttpPost httpPost = new HttpPost(url); 56 // 創建參數列表 57 if (param != null) { 58 List<NameValuePair> paramList = new ArrayList<>(); 59 for (String key : param.keySet()) { 60 paramList.add(new BasicNameValuePair(key, param.get(key))); 61 } 62 // 模擬表單 63 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); 64 httpPost.setEntity(entity); 65 } 66 // 執行http請求 67 response = httpClient.execute(httpPost); 68 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } finally { 72 try { 73 response.close(); 74 } catch (IOException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } 78 } 79 80 return resultString; 81 } 82 83 public static String doPost(String url) { 84 return doPost(url, null); 85 } 86 87 public static String doPostJson(String url, String json) { 88 // 創建Httpclient對象 89 CloseableHttpClient httpClient = HttpClients.createDefault(); 90 CloseableHttpResponse response = null; 91 String resultString = ""; 92 try { 93 // 創建Http Post請求 94 HttpPost httpPost = new HttpPost(url); 95 // 創建請求內容 96 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); 97 httpPost.setEntity(entity); 98 // 執行http請求 99 response = httpClient.execute(httpPost); 100 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 } finally { 104 try { 105 response.close(); 106 } catch (IOException e) { 108 e.printStackTrace(); 109 } 110 } 111 112 return resultString; 113 } 114 115 public static void main(String[] args) { 116 String url ="http://127.0.0.1:9090/actuator/shutdown"; 117 //該url必須要使用doPost方式來發送 118 HttpClientUtil.doPost(url); 119 } 120 }
(2)方式二:通過postman軟件發送shutdown命令。
七、 如何加強Eureka注冊的安全認證
1、在Eureka Server中添加security的依賴包
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--設置登錄密碼需要用到Spring Security,導入security的依賴包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、修改Eureka的Server配置文件 application.yml
#設置eurekaServer的登錄密碼
spring:
security:
user:
name: admin # 用戶名
password: admin # 用戶密碼
注意:如果是在集群環境下,例如上面示例中的集群環境,則需要分別在applicaion-p8761.properties、applicaion-p8762.properties、applicaion-p8763.properties都進行eurekaServer的登錄密碼設置。
3、修改Eureka集群節點之間互相訪問的url值
eureka.client.serviceUrl.defaultZone=http://admin:admin@server2:8762/eureka/, http://admin:admin@server3:8763/eureka/
4、關閉注冊中心Spring Security的CSRF驗證
如果不關閉,那么客戶端就連接不上。
1 import org.springframework.context.annotation.Configuration; 2 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 3 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 4 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 5 6 /** 7 如果eurekaServer設置了登錄密碼,就必須關閉Spring Security的CSRF驗證 8 */ 9 @EnableWebSecurity 10 @Configuration //@Configuration標注在類上,相當於把該類作為spring的xml配置文件中的<beans>,作用為:配置spring容器(應用上下文)。 11 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 12 13 @Override 14 protected void configure(HttpSecurity http) throws Exception { 15 http.csrf().disable(); //關閉csrf 16 super.configure(http); 17 } 18 }
5、修改微服務的配置文件添加訪問注冊中心的用戶名與密碼
配置Client的application.properties:
spring.application.name=eureka-provider server.port=9090 #設置服務注冊中心地址,指向另一個注冊中心 eureka.client.serviceUrl.defaultZone=http://admin:admin@server1:8761/eureka/, http://admin:admin@server2:8762/eureka/, http://admin:admin@server3:8763/eureka/ #啟用shutdown endpoints.shutdown.enabled=true #禁用密碼驗證 endpoints.shutdown.sensitive=false
演示效果: