spring cloud集成Eureka
spring cloud整合Eureka
文章目錄
- spring cloud整合Eureka
- Server模塊的搭建
- pom.xml配置
- 啟動類配置
- application.yam配置
- 查看Eureka主頁面
- client模塊的搭建
- pom.xml配置
- 啟動類配置
- application.yam配置
- 在Eureka中查看注冊
- Eureka集群搭建
- Eureka的保護機制
- Eureka,zookeeper,consul對比
- Eureka中自帶的Ribbon
- 使用
Server模塊的搭建
pom.xml配置
pom.xml文件,這里沒有版本號是因為,這里的依賴都繼承于父工程的pom
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>啟動類配置
要使用@EnableEurekaServer開啟EurekaServer服務
@SpringBootApplication @EnableEurekaServer public class EureMain {public static void main(String[] args) {SpringApplication.run(EureMain.class,args);} }application.yam配置
eureka:instance:hostname: localhost#client端的配置,因為這里不需要自我注冊所以設置為falseclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/查看Eureka主頁面
使用瀏覽器打開http://localhost:7001 查看主頁面。
client模塊的搭建
pom.xml配置
在要注冊的微服務模塊中的pom.xml添加Eureka-client依賴
<!-- eureka-client --> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>啟動類配置
要使用@EnableEurekaClient開啟EurekaClient服務
@SpringBootApplication @EnableEurekaClient public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class,args);} }application.yam配置
#服務名稱 spring:application:name: cloud-payment-service #配置服務發(fā)現(xiàn) eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:7001/eureka在Eureka中查看注冊
進入Eureka的主頁,查看服務是否注冊成功。
Eureka集群搭建
另一篇博客有寫,不重復累贅:Eureka集群搭建
Eureka的保護機制
最近項目在Kubernetes上使用Eureka遇到一些問題,在網站上找到一篇針對Eureka自我保護機制原理的文章,覺得不錯,總結如下:
Eureka的自我保護特性主要用于減少在網絡分區(qū)或者不穩(wěn)定狀況下的不一致性問題
- Eureka自我保護的產生原因:
Eureka在運行期間會統(tǒng)計心跳失敗的比例,在15分鐘內是否低于85%,如果出現(xiàn)了低于的情況,Eureka Server會將當前的實例注冊信息保護起來,同時提示一個警告,一旦進入保護模式,Eureka Server將會嘗試保護其服務注冊表中的信息,不再刪除服務注冊表中的數(shù)據(jù)。也就是不會注銷任何微服務。
原文地址:Eureka的自我保護機制 - ericnie - 博客園 (cnblogs.com)
Eureka的自我保護機制默認是開啟的。可以通過一些配置進行修改
eureka:server:enable-self-preservation: falseeviction-interval-timer-in-ms: 2000Eureka,zookeeper,consul對比
| Eureka | Java | AP | 無 | 可配支持 | HTTP | 已集成 |
| Consul | Go | CP | Raft | 支持 | HTTP/DNS | 已集成 |
| Zookeeper | Java | CP | Paxos | 支持 | 客戶端 | 已集成 |
Eureka中自帶的Ribbon
在新版的Eureka中自包含了Ribbon,所以在導入spring-cloud-starter-netflix-eureka-client時就自帶Ribbon做負載均勻。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>使用
@Configuration public class ApplicationContextConfig {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();} } @Configuration public class ApplicationContextConfig {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();} }總結
以上是生活随笔為你收集整理的spring cloud集成Eureka的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阳雀花的功效与作用、禁忌和食用方法
- 下一篇: 金山毒霸2011 SP5怎么使用