Eureka 介紹
Eureka提供基於REST的服務,在集群中主要用於服務管理。 Eureka提供了基於Java語言的客戶端組件,客戶端組件實現了負載均衡的功能,為業務組件的集群部署創造了條件。使用該框架,可以將業務組件註冊到Eureka容器中,這些業務組件可進行集群部署,Eureka主要維護這些服務的列表並自動檢查它們的狀態。
程序結構
創建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 啟動端口在application.yml
server: port: 8761
開啟Eureka服務註解@EnableEurekaServer
@EnableEurekaServer@SpringBootApplicationpublic class EKServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EKServerApplication.class).run(args); }}啟動springboot
[Thread-11] oscneserver.EurekaServerBootstrap: Initialized server context[main] sbcetTomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http)[main] .scnesEurekaAutoServiceRegistration: Updating port to 8761[main] cbfirstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)
啟動期間會出現一個無法連接到服務器的異常這個是由於Eureka在啟動的時候會把自己當作一個客戶端去服務器抓取註冊信息
複製代碼代碼如下:
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
增加如下配置啟動時便不會再出現該異常
eureka: client: registerWithEureka: false fetchRegistry: false
registerWithEureka 聲明是否將自己的信息註冊到Eureka服務器,默認值為true。
fetchRegistry 聲明是否到Eureka服務器中抓取註冊信息,默認值為true。
在瀏覽器中訪問http://localhost:8761 查看Eureka控制台輸入圖片說明
創建服務提供者
依賴
<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實例名稱和Eureka服務地址
server: port: 8080spring: application: name: ek-providereureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
創建一個REST 服務
@RestControllerpublic class HelloController { @RequestMapping("/hello") public String hello(HttpServletRequest request) { return "hello:" + request.getRequestURL(); }}開啟Eureka客戶端註解@EnableEurekaServer
@EnableEurekaClient@SpringBootApplicationpublic class EkProviderApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkProviderApplication.class).run(args); }}啟動之後在Eureka 控制台可以看到服務提供者已經在Eureka 中註冊
創建服務調用者
依賴
<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實例名稱和Eureka服務地址
server: port: 9000spring: application: name: ek-invokeeureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
編寫一個REST 服務調用服務提供者的“/hello”
@RestController@Configurationpublic 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); }}在傳統模式中,我們通常會用Apache中的Httpclient來調用REST 服務,在這裡我們使用Spring 提供調用REST 服務的組件RestTemplate。 RestTemplate 本身並不具備調用分佈式服務的能力,但是RestTemplate的bean被@LoadBalanced註解修飾後,這個RestTemplate實例就具有訪問分佈式服務的能力,這得益於Spring 為其提供的各種攔截器
開啟Eureka客戶端註解@EnableEurekaServer
@EnableEurekaClient@SpringBootApplicationpublic class EkInvokeApplication { public static void main(String[] args) { new SpringApplicationBuilder(EkInvokeApplication.class).run(args); }}啟動之後在Eureka 控制台可以看到服務調用者已經在Eureka 中註冊
之後在瀏覽器訪問服務調用者的“invoke” 接口返回如下
總結
Eureka 服務器通過心跳鏈接來維護最新的註冊信息,這些註冊信息都保存在內存中。
Eureka 服務提供者主要進行:
Eureka 服務調用者主要進行:
源碼地址:https://github.com/xc564864894/springcloud/tree/master/Eureka(%E4%B8%80)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。