服務注冊中心 Eureka
Eureka,是 Spring Cloud Netflix 組件之一。 包含服務治理(Eureka)包括服務注冊、服務發現和服務檢測監控等。
在Spring Cloud Netflix 整合技術棧中,Eureka既可以作為服務注冊中心也可以用於服務發現對整個微服務架構起着最核心的整合作用。
那啥是服務注冊中心?
舉個例子:某某洗浴中心則作為服務注冊中心。
那啥是服務注冊?
舉個例子:某某洗浴中心有N個PLMM來上班將信息登記到上面去
那啥是服務發現?
舉個例子:某男子來到了洗浴中心翻了翻了人員信息找到了某項洗浴服務選了一位對應的PLMM,然后就干了些該干的事情然后就走了。
在微服務架構中,由於每一個服務的粒度相對傳統SOA來說要小的多,所以服務的數量會成倍增加。這時如果有效管理服務的注冊信息就尤為重要。
在分布式系統中,我們不僅僅是需要在注冊中心找到服務和服務地址的映射關系這么簡單,我們還需要考慮更多更復雜的問題:
- 服務注冊后,如何被及時發現
- 服務宕機后,如何及時下線
- 服務如何有效的水平擴展
- 服務發現時,如何進行路由
- 服務異常時,如何進行降級
- 注冊中心如何實現自身的高可用
這些問題解決都依賴服務注冊中心。
注冊中心是微服務架構非常重要的一個組件,管理各種服務功能包括服務的注冊、發現、熔斷、負載、降級等。可以說是微服務架構中的”通訊錄“,它記錄了服務和服務地址的映射關系。在分布式架構中,服務會注冊到這里,當服務需要調用其它服務時,就到這里找到服務的地址,進行調用。
工程版本信息
- SpringCloud Hoxton.SR3
- SpringBoot 2.2.1.RELEASE
- java1.8
父工程依賴管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
SpringCloud Eureka 服務注冊中心搭建
創建名稱為eureka-server的子工程pom信息如下:
<dependencies>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- eureka服務端依賴jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
創建SpringBoot啟動類EurekaServerApplication內容如下:
/**
* Eureka服務端
* author: SimpleWu
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
創建application.properties內容如下:
spring.application.name=eureka-server
server.port=23001
#是否向服務注冊中心注冊自己
eureka.client.register-with-eureka=false
#是否檢索服務
eureka.client.fetch-registry=false
#eureka.client.service-url.defaultZone= http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka
eureka.client.service-url.defaultZone= http://localhost:${server.port}/eureka
到了這里直接啟動EurekaServerApplication main方法。
啟動成功后可以訪問:http://localhost:23001/ 就能夠查看到Eureka Web界面。
Eureka增加安全認證
增加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
增加properties配置:
#spring security 用戶密碼
spring.security.user.name=root
spring.security.user.password=123456
關閉csrf:
/**
* 關閉 csrf
* @author SimpleWu
*/
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
//http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
super.configure(http);
}
}
然后我們重新訪問的時候就需要輸入賬號與密碼才能夠放文檔Eureka服務。
客戶端連接Eureka需要攜帶賬號密碼properties配置可以使用
eureka.client.service-url.defaultZone= http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka
Eureka集群模式
3個服務組成一組通用的高可用服務分別為23001端口,23002端口,23003端口
配置Host三個實例服務映射:
127.0.0.1 eureka001
127.0.0.1 eureka002
127.0.0.1 eureka003
然后分別啟動3個Eureka服務,配置分別如下:
eureka001:
server.port=23001
eureka.instance.hostname=eureka001
eureka.client.service-url.defaultZone= http://eureka002:23002/eureka,http://eureka003:23003/eureka
eureka002:
server.port=23002
eureka.instance.hostname=eureka002
eureka.client.service-url.defaultZone= http://eureka001:23001/eureka,http://eureka003:23003/eureka
eureka003:
server.port=23003
eureka.instance.hostname=eureka003
eureka.client.service-url.defaultZone= http://eureka001:23001/eureka,http://eureka003:23003/eureka
客戶端(服務提供方配置):
eureka.client.service-url.defaultZone= http://eureka001:23001/eureka,http://eureka002:23002/eureka,http://eureka003:23003/eureka
到這里我們Eureka高可用集群就搭建成功了,可以客戶端只向Eureka001注冊服務,euereka002,eureka003中也會同步到euerka001的服務節點數據。
Eureka監聽事件
- EurekaInstanceCanceledEvent 服務下線事件
- EurekaInstanceRegisteredEvent 服務注冊事件
- EurekaInstanceRenewedEvent 服務續約事件
- EurekaRegistryAvailableEvent Eureka注冊中心啟動事件
- EurekaServerStartedEvent Eureka Server啟動事件
只需要在方法上標注@EventListener注解就可以監聽到以上事件案例如下:
@EventListener
public void listen(EurekaInstanceCanceledEvent event) {
logger.info("服務{}已下線", event.getAppName());
logger.info("server地址信息{}", event.getServerId());
//郵箱通知給管理員發送警告
}