Spring Cloud構(gòu)建Eureka應(yīng)用的方法
Eureka 介紹
Eureka提供基于REST的服務(wù),在集群中主要用于服務(wù)管理。Eureka提供了基于Java語(yǔ)言的客戶端組件,客戶端組件實(shí)現(xiàn)了負(fù)載均衡的功能,為業(yè)務(wù)組件的集群部署創(chuàng)造了條件。使用該框架,可以將業(yè)務(wù)組件注冊(cè)到Eureka容器中,這些業(yè)務(wù)組件可進(jìn)行集群部署,Eureka主要維護(hù)這些服務(wù)的列表并自動(dòng)檢查它們的狀態(tài)。
程序結(jié)構(gòu)
創(chuàng)建Eureka Server
maven依賴
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
更改spring boot 啟動(dòng)端口 在application.yml
server: port: 8761
開啟Eureka服務(wù)注解 @EnableEurekaServer
@EnableEurekaServer @SpringBootApplication public class EKServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EKServerApplication.class).run(args); } }
啟動(dòng)springboot
[Thread-11] o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context [main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http) [main] .s.c.n.e.s.EurekaAutoServiceRegistration: Updating port to 8761 [main] c.b.firstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)
啟動(dòng)期間會(huì)出現(xiàn)一個(gè)無法連接到服務(wù)器的異常 這個(gè)是由于Eureka在啟動(dòng)的時(shí)候會(huì)把自己當(dāng)作一個(gè)客戶端去服務(wù)器抓取注冊(cè)信息
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
增加如下配置啟動(dòng)時(shí)便不會(huì)再出現(xiàn)該異常
eureka: client: registerWithEureka: false fetchRegistry: false
registerWithEureka 聲明是否將自己的信息注冊(cè)到Eureka服務(wù)器,默認(rèn)值為true。
fetchRegistry 聲明是否到Eureka服務(wù)器中抓取注冊(cè)信息,默認(rèn)值為true。
在瀏覽器中訪問 http://localhost:8761 查看Eureka控制臺(tái) 輸入圖片說明
創(chuàng)建服務(wù)提供者
依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
在 application.yml 中配置端口、Eureka實(shí)例名稱和Eureka服務(wù)地址
server: port: 8080 spring: application: name: ek-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
創(chuàng)建一個(gè) REST 服務(wù)
@RestController public class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request) { return "hello:" + request.getRequestURL(); } }
開啟Eureka客戶端注解 @EnableEurekaServer
@EnableEurekaClient @SpringBootApplication public class EkProviderApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkProviderApplication.class).run(args); } }
啟動(dòng)之后在 Eureka 控制臺(tái)可以看到服務(wù)提供者已經(jīng)在 Eureka 中注冊(cè)
創(chuàng)建服務(wù)調(diào)用者
依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
在 application.yml 中配置端口、Eureka實(shí)例名稱和Eureka服務(wù)地址
server: port: 9000 spring: application: name: ek-invoke eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
編寫一個(gè) REST 服務(wù) 調(diào)用服務(wù)提供者的 “/hello”
@RestController @Configuration public class InvokeController { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @RequestMapping("/invoke") public String invoke() { RestTemplate restTemplate = getRestTemplate(); return restTemplate.getForObject("http://ek-provider/hello", String.class); } }
在傳統(tǒng)模式中,我們通常會(huì)用Apache中的Httpclient來調(diào)用 REST 服務(wù),在這里我們使用 Spring 提供調(diào)用 REST 服務(wù)的組件 RestTemplate。 RestTemplate 本身并不具備調(diào)用分布式服務(wù)的能力,但是RestTemplate的bean被@LoadBalanced注解修飾后,這個(gè)RestTemplate實(shí)例就具有訪問分布式服務(wù)的能力,這得益于 Spring 為其提供的各種攔截器
開啟Eureka客戶端注解 @EnableEurekaServer
@EnableEurekaClient @SpringBootApplication public class EkInvokeApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkInvokeApplication.class).run(args); } }
啟動(dòng)之后在 Eureka 控制臺(tái)可以看到服務(wù)調(diào)用者已經(jīng)在 Eureka 中注冊(cè)
之后在瀏覽器訪問服務(wù)調(diào)用者的 “invoke” 接口 返回如下
總結(jié)
Eureka 服務(wù)器 通過心跳鏈接來維護(hù)最新的注冊(cè)信息,這些注冊(cè)信息都保存在內(nèi)存中。
Eureka 服務(wù)提供者 主要進(jìn)行:
- 向 Eureka 服務(wù)器注冊(cè)服務(wù)
- 發(fā)送心跳到 Eureka服務(wù)器,使 Eureka 服務(wù)器注冊(cè)信息保持最新
- 向服務(wù)器獲取最新的注冊(cè)列表,通常 Eureka 客戶端既是服務(wù)提供者也是服務(wù)調(diào)用者,但其主要職責(zé)為服務(wù)提供。
Eureka 服務(wù)調(diào)用者 主要進(jìn)行:
- 向 Eureka 服務(wù)器注冊(cè)服務(wù)
- 發(fā)送心跳到 Eureka服務(wù)器,使 Eureka 服務(wù)器注冊(cè)信息保持最新
- 向服務(wù)器獲取最新的注冊(cè)列表,通常 Eureka 客戶端既是服務(wù)提供者也是服務(wù)調(diào)用者,但其主要職責(zé)為發(fā)現(xiàn)與調(diào)用。
源碼地址:https://github.com/xc564864894/springcloud/tree/master/Eureka(%E4%B8%80)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 細(xì)說Springcloud eureka的幾種主動(dòng)下線服務(wù)的方式
- spring cloud eureka微服務(wù)之間的調(diào)用詳解
- 詳解SpringCloud eureka服務(wù)狀態(tài)監(jiān)聽
- 淺談Spring Cloud Eureka 自我保護(hù)機(jī)制
- Spring Cloud中Eureka開啟密碼認(rèn)證的實(shí)例
- Spring cloud Eureka注冊(cè)中心搭建的方法
- SpringCloud之服務(wù)注冊(cè)與發(fā)現(xiàn)Spring Cloud Eureka實(shí)例代碼
- 詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)
- Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)發(fā)現(xiàn)與調(diào)用
- 單臺(tái)Spring Cloud Eureka升級(jí)到三臺(tái)Eureka高可用集群
相關(guān)文章
Java中Comparable接口和Comparator接口的使用比較
Java中提供了兩種對(duì)集合或數(shù)組中元素進(jìn)行排序的方法,一種是實(shí)現(xiàn)Comparable接口,另一種是實(shí)現(xiàn)Comparator接口,下面這篇文章主要給大家介紹了關(guān)于Java中Comparable接口和Comparator接口使用的相關(guān)資料,需要的朋友可以參考下2024-06-06java 數(shù)據(jù)庫(kù)連接與增刪改查操作實(shí)例詳解
這篇文章主要介紹了java 數(shù)據(jù)庫(kù)連接與增刪改查操作,結(jié)合實(shí)例形式詳細(xì)分析了java使用jdbc進(jìn)行數(shù)據(jù)庫(kù)連接及增刪改查等相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2019-11-11Mybatis-plus更新字段為null兩種常用方法及優(yōu)化
Mybatis Plus在進(jìn)行更新操作時(shí),默認(rèn)情況下是不能將字段更新為null的,如果要更新字段為null,需要進(jìn)行以下處理,這篇文章主要給大家介紹了關(guān)于Mybatis-plus更新字段為null的兩種常用方法及優(yōu)化,需要的朋友可以參考下2024-03-03Java中常用緩存Cache機(jī)制的實(shí)現(xiàn)
這篇文章主要介紹了Java中常用緩存Cache機(jī)制的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10