Spring Cloud EureKa Ribbon 服務注冊發(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來管理微服務,微服務可以注冊到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/
可以看到如下界面:

可以看到暫時還沒有服務注冊上來。到此,一個簡單的微服務注冊中心就構(gòu)建完了。
編寫微服務userservice
接下來用rest構(gòu)建一個微服務接口,并注冊到注冊中心上去。依然使用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的微服務接口。
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.";
}
}
運行UserApplication后,再次訪問http://localhost:8881/
會發(fā)現(xiàn)user這個服務已經(jīng)注冊上來了。

編寫微服務order
接下來,我們再構(gòu)建一個訂單的微服務,并訪問user這個微服務中的接口。
依然使用Spring Initialzr 來構(gòu)建一個新工程。user這個微服務是可以部署到多臺機器上的??蛻舳嗽谠L問這個服務的時候,請求可以路由到任何一臺部署了user服務的機器。因此客戶端需要使用一個路由算法來調(diào)度user這個服務。在Spring Cloud中,可以使用Ribbon組件來做客戶端路由。Ribbon內(nèi)部會去服務注冊中心獲取服務列表的,以便調(diào)用對應的服務。

這次除了勾選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();
}
}
運行OrderApplication后,訪問http://localhost:8881/
會發(fā)現(xiàn)order這個服務也被注冊到注冊中心了。

接下來我們訪問OrderController中的getOrderUser 方法,觸發(fā)調(diào)用UserController的getUser方法。
在瀏覽器中輸入:http://localhost:8883/getOrderUser
可以看到返回了:I am user list.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
fastjson對JSONObject中的指定字段重新賦值的實現(xiàn)
這篇文章主要介紹了fastjson對JSONObject中的指定字段重新賦值的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
javaweb用戶注銷后點擊瀏覽器返回刷新頁面重復登錄問題的解決方法
這篇文章主要為大家詳細介紹了javaweb用戶注銷后點擊瀏覽器返回刷新頁面重復登錄問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
測試springboot項目出現(xiàn)Test Ignored的解決
這篇文章主要介紹了測試springboot項目出現(xiàn)Test Ignored的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧分享
Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12

