1、Eureka集群搭建
2、Eureka自我保護機制
Eureka集群搭建
高可用集群配置
當注冊中心扛不住高並發的時候,這時候 要用集群來扛;
普通操作
我們再新建兩個module microservice-eureka-server-2002 microservice-eureka-server-2003
pom.xml 把依賴加下
<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-test</artifactId> <scope>test</scope> </dependency> <!-- 修改后立即生效,熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
2、2002 2003的主啟動類EurekaServerApplication_2002,EurekaServerApplication_2003復制修改下;
package com.hmc.microserviceeurekaserver2002;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServer2002Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServer2002Application.class, args);
}
}
3、前面單機的時候 eureka注冊中心實例名稱 是localhost,現在是集群,不能三個實例都是localhost,這里復雜的辦法是搞三個虛擬機,麻煩,這里有簡單辦法,直接配置本機hosts,來實現本機域名映射;
找到 C:\Windows\System32\drivers\etc 打開hosts,加配置
127.0.0.1 eureka2001.hmc.com
127.0.0.1 eureka2002.hmc.com
127.0.0.1 eureka2003.hmc.com
4、修改三個項目的application.yml文件,主要是修改 hostname和defaultZone,
2001修改:
server:
port: 2001
context-path: /
eureka:
instance:
# 單機 hostname: localhost #eureka注冊中心實例名稱
hostname: eureka2001.hmc.com # 集群
client:
register-with-eureka: false #false 由於該應用為注冊中心,所以設置為false,代表不向注冊中心注冊自己。
service-url:
defaultZone: http://eureka2002.hmc.com:2002/eureka/,http://eureka2003.hmc.com:2003/eureka/ # 集群
#單機defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #設置與Eureka注冊中心交互的地址,查詢服務和注冊服務用到
2002修改:
server:
port: 2002
context-path: /
eureka:
instance:
# \u5355\u673A hostname: localhost #eureka\u6CE8\u518C\u4E2D\u5FC3\u5B9E\u4F8B\u540D\u79F0
hostname: eureka2002.hmc.com # \u96C6\u7FA4
client:
register-with-eureka: false #false \u7531\u4E8E\u8BE5\u5E94\u7528\u4E3A\u6CE8\u518C\u4E2D\u5FC3\uFF0C\u6240\u4EE5\u8BBE\u7F6E\u4E3Afalse,\u4EE3\u8868\u4E0D\u5411\u6CE8\u518C\u4E2D\u5FC3\u6CE8\u518C\u81EA\u5DF1\u3002
service-url:
defaultZone: http://eureka2001.hmc.com:2001/eureka/,http://eureka2003.hmc.com:2003/eureka/ # \u96C6\u7FA4
#\u5355\u673AdefaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #\u8BBE\u7F6E\u4E0EEureka\u6CE8\u518C\u4E2D\u5FC3\u4EA4\u4E92\u7684\u5730\u5740\uFF0C\u67E5\u8BE2\u670D\u52A1\u548C\u6CE8\u518C\u670D\u52A1\u7528\u5230
2003修改:
server:
port: 2003
context-path: /
eureka:
instance:
# \u5355\u673A hostname: localhost #eureka\u6CE8\u518C\u4E2D\u5FC3\u5B9E\u4F8B\u540D\u79F0
hostname: eureka2003.hmc.com # \u96C6\u7FA4
client:
register-with-eureka: false #false \u7531\u4E8E\u8BE5\u5E94\u7528\u4E3A\u6CE8\u518C\u4E2D\u5FC3\uFF0C\u6240\u4EE5\u8BBE\u7F6E\u4E3Afalse,\u4EE3\u8868\u4E0D\u5411\u6CE8\u518C\u4E2D\u5FC3\u6CE8\u518C\u81EA\u5DF1\u3002
service-url:
defaultZone: http://eureka2001.hmc.com:2001/eureka/,http://eureka2002.hmc.com:2002/eureka/ # \u96C6\u7FA4
#\u5355\u673AdefaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #\u8BBE\u7F6E\u4E0EEureka\u6CE8\u518C\u4E2D\u5FC3\u4EA4\u4E92\u7684\u5730\u5740\uFF0C\u67E5\u8BE2\u670D\u52A1\u548C\u6CE8\u518C\u670D\u52A1\u7528\u5230
5、修改服務提供者項目的application.yml,主要修改eureka.client.service-url.defaultZone
eureka:
instance:
hostname: localhost #eureka客戶端主機實例名稱
appname: microservice-student #客戶端服務名
instance-id: microservice-student:1001 #客戶端實例名稱
prefer-ip-address: true #顯示IP
client:
service-url:
defaultZone: http://localhost:2001/eureka #把服務注冊到eureka注冊中心
最后我們測試下:
啟動三個注冊中心,以及服務提供者項目;
上面eureka服務搭建,除了yml文件不一樣,其他文件都一樣,那么我們有什么辦法能夠將多個eureka服務集合到一個工程中去呢?
創建一個microservice-eureka-server(三合一)子工程
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hmc</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-eureka-server</artifactId> <properties> <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-test</artifactId> <scope>test</scope> </dependency> <!-- 修改后立即生效,熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Yml文件
---
server:
port: 2001
context-path: /
eureka:
instance:
hostname: eureka2001.hmc.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2002.hmc.com:2002/eureka/,http://eureka2003.hmc.com:2003/eureka/
spring:
profiles: eureka2001
---
server:
port: 2002
context-path: /
eureka:
instance:
hostname: eureka2002.hmc.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.hmc.com:2001/eureka/,http://eureka2003.hmc.com:2003/eureka/
spring:
profiles: eureka2002
---
server:
port: 2003
context-path: /
eureka:
instance:
hostname: eureka2003.hmc.com
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka2001.hmc.com:2001/eureka/,http://eureka2002.hmc.com:2002/eureka/
spring:
profiles: eureka2003
啟動類MicroserviceEurekaServerApplication.java
package com.hmc.microserviceeurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
}
}
Eureka自我保護機制
開發環境,我們經常會遇到這個紅色的警告;
當我們長時間為訪問服務以及變更服務實例名稱的時候,就會出現這個紅色警告;
默認情況,如果服務注冊中心再一段時間內沒有接收到某個微服務實例的心跳,服務注冊中心會注銷該實例(默認90秒)。
由於正式環境,經常會有網絡故障,網絡延遲問題發生,服務和注冊中心無法正常通信,此時服務是正常的,不應該注銷該服務,Eureka這時候,就通過“自我保護模式”來解決問題,當短時間和服務失去通信時,保留服務信息,當恢復網絡和通信時候,退出“自我保護模式”;
通過“自我保護模式”,使Eureka集群更加的健壯和穩定;