SpringCloud Eureka的使用教程
什么是Eureka
Eureka是Netfilx開源的一個(gè)用來實(shí)現(xiàn)微服務(wù)的注冊與發(fā)現(xiàn)的組件。它包含Server和Client兩部分。
為什么要有Eureka
例如目前有兩個(gè)服務(wù)分別為服務(wù)A,服務(wù)B,我們可以在服務(wù)A調(diào)用服務(wù)B的接口地址完成調(diào)用,但是當(dāng)服務(wù)間的調(diào)用關(guān)系復(fù)雜起來的時(shí)候,比如服務(wù)A還需要調(diào)用服務(wù)CDE,那么服務(wù)A需要維護(hù)它調(diào)用的所有服務(wù)的地址,一旦地址的變更都需要手動(dòng)去修改。
當(dāng)使用了Eureka這樣的服務(wù)治理框架后,服務(wù)ABCDE可以一起注冊到EurekaServer服務(wù)上,直接通過服務(wù)名來調(diào)用其他服務(wù)。
Eureka的使用
通過Eureka,實(shí)現(xiàn)消費(fèi)者服務(wù)80通過服務(wù)名調(diào)用服務(wù)提供者8001的接口。
cloud-eureka-server7001關(guān)鍵代碼
引入server依賴:
<!--eureka-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
server端配置:
eureka: instance: hostname: localhost client: register-with-eureka: false #false表示不向注冊中心注冊自己。 fetch-registry: false #false表示自己端就是注冊中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù) service-url: #集群指向其它eureka defaultZone: http://eureka7002.com:7002/eureka/ #單機(jī)就是7001自己 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
啟動(dòng)類注解:
@SpringBootApplication @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class, args); } }
cloud-provider-payment8001關(guān)鍵代碼
引入client依賴:
<!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
client端配置:
eureka: client: #表示是否將自己注冊進(jìn)EurekaServer默認(rèn)為true。 register-with-eureka: true #是否從EurekaServer抓取已有的注冊信息,默認(rèn)為true。單節(jié)點(diǎn)無所謂,集群必須設(shè)置為true才能配合ribbon使用負(fù)載均衡 fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka
啟動(dòng)類注解:
@SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class, args); } }
cloud-consumer-order80
關(guān)鍵代碼同服務(wù)提供者
通過服務(wù)名調(diào)用其他服務(wù)的接口(RestTemple記得要加@LoadBalanced,否則找不到服務(wù)名):
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE"; @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment(@PathVariable("id") Long id) { return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class); }
如果使用Eureka集群
EurekaServer配置
#集群指向其它eureka defaultZone: http://eureka7002.com:7002/eureka/
服務(wù)Cient配置
# 集群 #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #
總結(jié)
到此這篇關(guān)于SpringCloud Eureka使用的文章就介紹到這了,更多相關(guān)SpringCloud Eureka使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?ConcurrentHashMap實(shí)現(xiàn)線程安全的代碼示例
眾所周知ConcurrentHashMap是HashMap的多線程版本,HashMap?在并發(fā)操作時(shí)會(huì)有各種問題,而這些問題,只要使用ConcurrentHashMap就可以完美解決了,本文將給詳細(xì)介紹ConcurrentHashMap是如何保證線程安全的2023-05-05Java調(diào)用MySQL存儲(chǔ)過程并獲得返回值的方法
這篇文章主要介紹了Java調(diào)用MySQL存儲(chǔ)過程并獲得返回值的方法,實(shí)例分析了java實(shí)現(xiàn)MySQL存儲(chǔ)過程的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07關(guān)于@PropertySource配置的用法解析
這篇文章主要介紹了關(guān)于@PropertySource配置的用法解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java 高并發(fā)中volatile的實(shí)現(xiàn)原理
這篇文章主要介紹了java 高并發(fā)中volatile的實(shí)現(xiàn)原理的相關(guān)資料,在多線程并發(fā)編程中synchronized和Volatile都扮演著重要的角色,Volatile是輕量級的synchronized,它在多處理器開發(fā)中保證了共享變量的“可見性”,需要的朋友可以參考下2017-03-03Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java中的動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例,jdk動(dòng)態(tài)代理本質(zhì)上是使用被代理對象的類加載器,通過被代理類實(shí)現(xiàn)的接口在運(yùn)行時(shí)動(dòng)態(tài)構(gòu)造出代理類來增強(qiáng)原始類的功能的方法,需要的朋友可以參考下2023-12-12Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)
RequestMapping、@PostMapping和@GetMapping是三個(gè)非常常用的注解,本文就來介紹一下這三種注解的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07關(guān)于同一個(gè)service調(diào)用service本身的方法
這篇文章主要介紹了關(guān)于同一個(gè)service調(diào)用service本身的方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06