Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用
概述
用一個簡單的例子演示Spring Cloud中EureKa和Ribbon的基本用法。
版本和環(huán)境
- IDEA
- Spring Boot 1.5.·0
- JDK 1.8
- Maven 3
構(gòu)建eureka server
在Spring Cloud,可以使用eureka來管理微服務(wù),微服務(wù)可以注冊到eureka中。
首先可以用IDEA的Spring Initialzr
來創(chuàng)建eureka server注冊中心。
修改application.properties文件,添加如下內(nèi)容
spring.application.name=eureka-server eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false server.port=8881
在Spring Boot為我們生成的啟動類ServerApplication
中加入@EnableEurekaServer
注解
package com.springcloud.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
在瀏覽器中輸入http://localhost:8881/
可以看到如下界面:
可以看到暫時還沒有服務(wù)注冊上來。到此,一個簡單的微服務(wù)注冊中心就構(gòu)建完了。
編寫微服務(wù)userservice
接下來用rest構(gòu)建一個微服務(wù)接口,并注冊到注冊中心上去。依然使用Spring Initialzr
來構(gòu)建一個新的工程。使用方式跟上面的一樣。
注意這次要勾選Eureka Discovery
組件。而不是Eureka Server
。
修改application.properties文件,添加如下內(nèi)容:
spring.application.name=user server.port=8882 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/
在Spring Boot
為我們生成的UserApplication
類中使用@EnableDiscoveryClient
注解。
package com.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
創(chuàng)建一個rest full的微服務(wù)接口。
package com.springcloud; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/getUser") public String getUser() { return "I am user list."; } }
運(yùn)行UserApplication后,再次訪問http://localhost:8881/
會發(fā)現(xiàn)user這個服務(wù)已經(jīng)注冊上來了。
編寫微服務(wù)order
接下來,我們再構(gòu)建一個訂單的微服務(wù),并訪問user這個微服務(wù)中的接口。
依然使用Spring Initialzr
來構(gòu)建一個新工程。user這個微服務(wù)是可以部署到多臺機(jī)器上的??蛻舳嗽谠L問這個服務(wù)的時候,請求可以路由到任何一臺部署了user服務(wù)的機(jī)器。因此客戶端需要使用一個路由算法來調(diào)度user這個服務(wù)。在Spring Cloud中,可以使用Ribbon組件來做客戶端路由。Ribbon內(nèi)部會去服務(wù)注冊中心獲取服務(wù)列表的,以便調(diào)用對應(yīng)的服務(wù)。
這次除了勾選Eureka Discovery
組件。還需要勾選Ribbon
。
修改application.properties文件,添加如下內(nèi)容:
spring.application.name=order server.port=8883 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/
在Spring Boot
為我們生成的OrderApplication
類中增加如下配置。
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class OrderApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
由于使用了Ribbon,這里需要使用@LoadBalanced
注解。
編寫OrderController
。
package com.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/getOrderUser") public String getOrderUser() { return restTemplate.getForEntity("http://user/getUser",String.class).getBody(); } }
運(yùn)行OrderApplication
后,訪問http://localhost:8881/
會發(fā)現(xiàn)order這個服務(wù)也被注冊到注冊中心了。
接下來我們訪問OrderController
中的getOrderUser
方法,觸發(fā)調(diào)用UserController
的getUser
方法。
在瀏覽器中輸入:http://localhost:8883/getOrderUser
可以看到返回了:I am user list.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot應(yīng)用gradle?Plugin示例詳解
這篇文章主要介紹了Springboot應(yīng)用gradle?Plugin詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04fastjson對JSONObject中的指定字段重新賦值的實(shí)現(xiàn)
這篇文章主要介紹了fastjson對JSONObject中的指定字段重新賦值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁面重復(fù)登錄問題的解決方法
這篇文章主要為大家詳細(xì)介紹了javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁面重復(fù)登錄問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09測試springboot項(xiàng)目出現(xiàn)Test Ignored的解決
這篇文章主要介紹了測試springboot項(xiàng)目出現(xiàn)Test Ignored的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Idea2020 無法share項(xiàng)目到svn的解決方法
這篇文章主要介紹了Idea2020 無法share項(xiàng)目到svn的解決方法,需要的朋友可以參考下2020-09-09java中Class類的基礎(chǔ)知識點(diǎn)及實(shí)例
在本篇文章里小編給大家分享了關(guān)于java中Class類的基礎(chǔ)知識點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧分享
Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12springboot實(shí)現(xiàn)瀏覽器截屏并添加文字
大家好,本篇文章主要講的是springboot實(shí)現(xiàn)瀏覽器截屏并添加文字,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02