Spring Cloud EureKa Ribbon 服務註冊發現與調用,eurekaribbon
概述
用一個簡單的例子示範Spring Cloud中EureKa和Ribbon的基本用法。
版本和環境
- IDEA
- Spring Boot 1.5.·0
- JDK 1.8
- Maven 3
構建eureka server
在Spring Cloud,可以使用eureka來管理微服務,微服務可以註冊到eureka中。
首先可以用IDEA的Spring Initialzr 來建立eureka server註冊中心。
修改application.properties檔案,添加如下內容
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@SpringBootApplicationpublic class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); }}
在瀏覽器中輸入http://localhost:8881/
可以看到如下介面:
可以看到暫時還沒有服務註冊上來。到此,一個簡單的微服務註冊中心就構建完了。
編寫微服務userservice
接下來用rest構建一個微服務介面,並註冊到註冊中心上去。依然使用Spring Initialzr 來構建一個新的工程。使用方式跟上面的一樣。
注意這次要勾選Eureka Discovery 組件。而不是Eureka Server 。
修改application.properties檔案,添加如下內容:
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@SpringBootApplicationpublic class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); }}
建立一個rest full的微服務介面。
package com.springcloud;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @GetMapping("/getUser") public String getUser() { return "I am user list."; }}
運行UserApplication後,再次訪問http://localhost:8881/
會發現user這個服務已經註冊上來了。
編寫微服務order
接下來,我們再構建一個訂單的微服務,並訪問user這個微服務中的介面。
依然使用Spring Initialzr 來構建一個新工程。user這個微服務是可以部署到多台機器上的。用戶端在訪問這個服務的時候,請求可以路由到任何一台部署了user服務的機器。因此用戶端需要使用一個路由演算法來調度user這個服務。在Spring Cloud中,可以使用Ribbon組件來做用戶端路由。Ribbon內部會去服務註冊中心擷取服務列表的,以便調用對應的服務。
這次除了勾選Eureka Discovery 組件。還需要勾選Ribbon 。
修改application.properties檔案,添加如下內容:
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@SpringBootApplicationpublic 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;@RestControllerpublic class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/getOrderUser") public String getOrderUser() { return restTemplate.getForEntity("http://user/getUser",String.class).getBody(); }}
運行OrderApplication後,訪問http://localhost:8881/
會發現order這個服務也被註冊到註冊中心了。
接下來我們訪問OrderController中的getOrderUser 方法,觸發調用UserController的getUser方法。
在瀏覽器中輸入:http://localhost:8883/getOrderUser
可以看到返回了:I am user list.
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。