概述
用一個簡單的例子演示Spring Cloud中EureKa和Ribbon的基本用法。
版本和環境
構建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.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。