Eureka服務(wù)治理****Spring Cloud

目前在大型的招聘,或者是Java界,對(duì)SpringCloud的要求也是越來(lái)越多,有的公司不僅僅要求了解基本的配置信息,以及代碼書(shū)寫(xiě)能力,而且深入源碼,了解底層。
本人不才,也不知道底層是怎么實(shí)現(xiàn)的,就基于Eureka的服務(wù)治理做一下筆記整理。
不足之處,還望各位大神不吝賜教,再次感激涕零。
基于Eureka實(shí)現(xiàn)的服務(wù)治理,在理論上和zookeeper對(duì)服務(wù)治理差不多
關(guān)系調(diào)用:
服務(wù)生產(chǎn)者啟動(dòng)時(shí),向服務(wù)注冊(cè)中心注冊(cè)自己提供的服務(wù)。
服務(wù)消費(fèi)者啟動(dòng)時(shí),向服務(wù)注冊(cè)中心訂閱自己需要的服務(wù)。
注冊(cè)中心會(huì)返回服務(wù)提供者的地址信息給消費(fèi)者。
消費(fèi)者則從服務(wù)提供者調(diào)用服務(wù)。
使用Eureka進(jìn)行服務(wù)治理:
首先搭建注冊(cè)中心,為了以后項(xiàng)目能在此基礎(chǔ)上繼續(xù)開(kāi)發(fā),所以選擇了基于maven建立模塊化。
建立父項(xiàng)目POM
這是基本的架子,后續(xù)可增加配置中心以及其他的服務(wù)。
先看一下父pom文件中的配置,項(xiàng)目搭建可參考:
<?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.zhaixingzu</groupId>
<artifactId>yyc</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>yyc</name>
<description>yyc-pom</description>
<modules>
<module>yyc-registry</module>
</modules>
<!-- 集中定義版本號(hào) -->
<properties>
<spring-boot.version>2.1.3.RELEASE</spring-boot.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!--eureka 客戶(hù)端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--測(cè)試依賴(lài)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
</project>
下來(lái)建立yyc-registry
在yyc-registry中添加pom的配置信息:
<?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>
<parent>
<groupId>com.zhaixingzu</groupId>
<artifactId>yyc</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>
<artifactId>yyc-register</artifactId>
<packaging>jar</packaging>
<name>yyc-register</name>
<description>yyc 注冊(cè)中心</description>
<dependencies>
<!--服務(wù)中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--security-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<!--web 模塊-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除tomcat依賴(lài)-->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在RegistryApplication中我們添加注解
package com.zhaixingzu.yyc.registry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 服務(wù)注冊(cè)中心
* @author Herbert
* @date 2019年06月19日
*/
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class, args);
}
}
在application.properties中添加配置信息:
server.port=9900
spring.application.name=yyc-registry
spring.security.user.name=admin
spring.security.user.password=admin
spring.application.admin.enabled=false
eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true
#是否開(kāi)啟自我保護(hù)(運(yùn)行期間spring會(huì)統(tǒng)計(jì)信條失敗的比例在15分鐘之內(nèi)是否低于85%,如果不低于85%,Eureka會(huì)將實(shí)例注冊(cè)信息保護(hù)起來(lái),讓這些實(shí)例不會(huì)過(guò)期)
eureka.server.enable-self-preservation= false
#3秒鐘自動(dòng)剔除失效的節(jié)點(diǎn),清理無(wú)效的節(jié)點(diǎn)
eureka.server.eviction-interval-timer-in-ms= 3000
#eureka server刷新readCacheMap的時(shí)間,注意,client讀取的是readCacheMap,這個(gè)時(shí)間決定了多久會(huì)把readWriteCacheMap的緩存更新到readCacheMap上
eureka.server.response-cache-update-interval-ms= 3000
#eureka server緩存readWriteCacheMap失效時(shí)間,這個(gè)只有在這個(gè)時(shí)間過(guò)去后緩存才會(huì)失效,失效前不會(huì)更新,過(guò)期后從registry重新讀取注冊(cè)服務(wù)信息,registry是一個(gè)ConcurrentHashMap。
#由于啟用了evict其實(shí)就用不太上改這個(gè)配置了
#默認(rèn)180s
eureka.server.response-cache-auto-expiration-in-seconds=180
#不要向注冊(cè)中心注冊(cè)自己
eureka.client.register-with-eureka=false
#禁止檢索服務(wù) 設(shè)置為true可以從其他eureka節(jié)點(diǎn)獲取注冊(cè)信息
eureka.client.fetch-registry=false
#設(shè)置與eureka交互的地址
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
到此所有的配置已經(jīng)完成,直接啟動(dòng)啟動(dòng)類(lèi):
訪問(wèn)頁(yè)面:
輸入賬號(hào),密碼:


注:從上圖看到,在"Instances currently registered with Eureka"信息中,沒(méi)有一個(gè)實(shí)例,說(shuō)明目前還沒(méi)有服務(wù)注冊(cè)。
注冊(cè)中心啟動(dòng)完成
下來(lái)記錄其中重點(diǎn)筆記:
1:在父級(jí)項(xiàng)目yyc中定義的maven包為pom,引用了一些SpringCloud項(xiàng)目常用的一些依賴(lài)包,這樣在每一個(gè)子項(xiàng)目中就不會(huì)再去依賴(lài)
2:子項(xiàng)目yyc-registry中排除了springboot用tomcat作為內(nèi)嵌容器,引進(jìn)了undertow容器,tomcat和undertow的負(fù)載能力基本差不多。引進(jìn)的原因是因?yàn)閡ndertow為輕量級(jí)容器,比Jeety性能稍?xún)?yōu)
3:@SpringBootApplication = (默認(rèn)屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan
4:application.properties 和 application.yml 中對(duì)配置沒(méi)有任何區(qū)別,yml的配置文件可增強(qiáng)可讀性,在書(shū)寫(xiě)方面也比較方便
5:如需要開(kāi)啟密碼認(rèn)證,需要進(jìn)行安全配置:
引人Jar包
并且在application中進(jìn)行配置用戶(hù)名與密碼:
設(shè)置與eureka的交互地址:
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
6:配置信息詳解:
server: port: 9900spring: application: name: yyc-registry security: user: name: admin password: admin cloud: config: enabled: falseeureka: instance: hostname: 127.0.0.1 prefer-ip-address: true server: #是否開(kāi)啟自我保護(hù)(運(yùn)行期間spring會(huì)統(tǒng)計(jì)信條失敗的比例在15分鐘之內(nèi)是否低于85%,如果不低于85%,Eureka會(huì)將實(shí)例注冊(cè)信息保護(hù)起來(lái),讓這些實(shí)例不會(huì)過(guò)期) enable-self-preservation: false eviction-interval-timer-in-ms: 3000 #3秒鐘自動(dòng)剔除失效的節(jié)點(diǎn),清理無(wú)效的節(jié)點(diǎn) response-cache-update-interval-ms: 3000 #eureka server刷新readCacheMap的時(shí)間,注意,client讀取的是readCacheMap,這個(gè)時(shí)間決定了多久會(huì)把readWriteCacheMap的緩存更新到readCacheMap上 response-cache-auto-expiration-in-seconds: 180 #eureka server緩存readWriteCacheMap失效時(shí)間,這個(gè)只有在這個(gè)時(shí)間過(guò)去后緩存才會(huì)失效,失效前不會(huì)更新,過(guò)期后從registry重新讀取注冊(cè)服務(wù)信息,registry是一個(gè)ConcurrentHashMap。 #由于啟用了evict其實(shí)就用不太上改這個(gè)配置了 #默認(rèn)180s client: register-with-eureka: false #不要向注冊(cè)中心注冊(cè)自己 fetch-registry: false #禁止檢索服務(wù) 設(shè)置為true可以從其他eureka節(jié)點(diǎn)獲取注冊(cè)信息 service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #設(shè)置與eureka交互的地址
7: 對(duì)于配置application.yml 和 bootstrap.yml 的區(qū)別:
Spring Cloud 構(gòu)建于 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap,另外一種是 application,
application 配置文件這個(gè)容易理解,主要用于 Spring Boot 項(xiàng)目的自動(dòng)化配置。
bootstrap 是應(yīng)用程序的父上下文,也就是說(shuō) bootstrap 加載優(yōu)先于 applicaton。
bootstrap 主要用于從額外的資源來(lái)加載配置信息,還可以在本地外部配置文件中解密屬性。
這兩個(gè)上下文共用一個(gè)環(huán)境,它是任何Spring應(yīng)用程序的外部屬性的來(lái)源。
bootstrap 里面的屬性會(huì)優(yōu)先加載,它們默認(rèn)也不能被本地相同配置覆蓋。
boostrap 由父 ApplicationContext 加載,比 applicaton 優(yōu)先加載
boostrap 里面的屬性不能被覆蓋
8:Eureka中啟動(dòng)成功后顯示紅色標(biāo)記(如下圖)
系統(tǒng)在三種情況下會(huì)出現(xiàn)紅色加粗的字體提示:
a.在配置上,自我保護(hù)機(jī)制關(guān)閉
RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
b.自我保護(hù)機(jī)制開(kāi)啟了
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE
NOT BEING EXPIRED JUST TO BE SAFE.
c.在配置上,自我保護(hù)機(jī)制關(guān)閉了,但是一分鐘內(nèi)的續(xù)約數(shù)沒(méi)有達(dá)到85% , 可能發(fā)生了網(wǎng)絡(luò)分區(qū),會(huì)有如下提示
THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
9:在啟動(dòng)加入banner.txt可以出現(xiàn)banner自定義樣式
歡迎關(guān)注公眾號(hào)摘星族
