第十篇: 高可用的服务注册中心(Finchley版本)V2.0_dev
生活随笔
收集整理的這篇文章主要介紹了
第十篇: 高可用的服务注册中心(Finchley版本)V2.0_dev
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、準備工作
Eureka通過運行多個實例,使其更具有高可用性。事實上,這是它默認的熟性,你需要做的就是給對等的實例一個合法的關聯serviceurl。
二、創建eureka-server
引入依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>添加配置信息
eureka:client:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ spring:application:name: eurka-server運行主類
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}分別啟動3個eureka-server實例,
第一步:啟動以第1個eureka-server實例:
-Dserver.port=8761
配置信息修改如下:
第一步:啟動以第1個eureka-server實例:
-Dserver.port=8762
配置信息修改如下:
第三步:啟動以第1個eureka-server實例:
-Dserver.port=8763
配置信息修改如下:
依次訪問:localhost:8761、localhost:8761、localhost:8761
可以看到運行了3個eureka-serser實例
創建eureka-client工程
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>啟動主類
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class ServiceHiApplication {public static void main(String[] args) {SpringApplication.run(ServiceHiApplication.class, args);} }配置文件
server:port: 8765spring:application:name: service-hieureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/web訪問
@RestController public class HiController {@Value("${server.port}")private String port;@GetMapping("/hi")public String home(@RequestParam(value = "name", defaultValue = "gblfy") String name) {return "hi," + name + ",i am from port" + port;} }啟動eureka-client,
再依次訪問:localhost:8761、localhost:8761、localhost:8761
發現eureka-client已經成功注冊到3個eureka-server服務端了,本次策略采取的是,eureka-server兩兩注冊,就算其中一個宕機了,也不會影響服務的發現和注冊。
當然eureka-client建議采用集群策略,以達到高可用
總結
以上是生活随笔為你收集整理的第十篇: 高可用的服务注册中心(Finchley版本)V2.0_dev的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gradle下载及配置
- 下一篇: 使用Jenkins搭建自动化测试环境_环