今天咱們來學習 服務消費者ribbon 如何調用 服務提供者java
因爲 獲取用戶信息 屬於業務模塊,所以咱們須要新建一個maven主項目:bussnissservicenginx
而後在該主項目上建立一個springboot項目web
點擊下一步,選擇「Eureka Server」 算法
此時的項目結構:spring
一樣,將 application.properties 修改成 bootstrap.yml (之後不作特殊說明都修改成bootstrap.yml)數據庫
spring: application: name: service-user server: port: 8801
使用@EnableEurekaClient註解開啓Eureka客戶端apache
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class BussnessserviceUserApplication { public static void main(String[] args) { SpringApplication.run(BussnessserviceUserApplication.class, args); } }
新建一個controller包,並在此包下寫一個獲取全部用戶的接口bootstrap
import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserManagementController { @Value("${server.port}") String serverPort; @GetMapping("/listUsers") public String ListUsers(){ /** * 模擬從數據庫查詢 */ List<Map<String, Object>> users = new ArrayList<Map<String, Object>>(); for(int i=1; i< 5; i++){ Map<String, Object> user = new HashMap<String, Object>(); user.put("id", i); user.put("name", "小明" + i); users.add(user); } return "服務器端口號: " + serverPort + " | 用戶信息: " + users.toString(); } }
啓動Eureka註冊中心--啓動 service-user 微服務--而後在瀏覽器輸入瀏覽器
http://localhost:8801/listUsersspringboot
端口號爲8801的服務器爲咱們返回了用戶列表信息
ribbon 是 Netflix 發佈的 客戶端負載均衡 組件,(nginx是服務端的負載均衡)
下面看一下具體實現
選擇Eureka Server 和 Ribbon
完整pom.xml以下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mayi.springcloud</groupId> <artifactId>bussnessservice-user-client-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>bussnessservice-user-client-ribbon</name> <description>用戶中心服務調用者ribbon</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.M9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
bootstrp.xml
spring: application: name: service-user-client-ribbon server: port: 8901
在啓動類中@Bean 將 restTemplate注入到ioc容器, 並使用@LoadBalanced 註解聲明開啓 負載均衡
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class BussnessserviceUserClientRibbonApplication { public static void main(String[] args) { SpringApplication.run(BussnessserviceUserClientRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }
接下來,新建一個client包,在該包下寫一個ribbon接口
package com.mayi.springcloud.client; 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 UserManagementRibbonClient { @Autowired RestTemplate restTemplate; @GetMapping("/listUsersByRibbon") public String ListUsersByRibbon(){ String result = this.restTemplate.getForObject("http://service-user/listUsers", String.class); return result; } }
說明:service-user 爲服務名稱, 一個服務名稱對應多個主機IP和端口號,這樣根據服務名調用就實現了負載均衡的功能
爲測試 ribbon負載均衡功能,咱們再啓動一個service-user服務,端口號爲8802:
訪問:http://localhost:8901/listUsersByRibbon
由於默認的負載均衡算法是輪詢,因此兩臺服務器輪流被調用,(也可自行修改負載均衡算法,例如:隨機算法,只是簡單的配置,這裏再也不闡述)
接下來,我會依次更新文章,直至整個架構完成,若有興趣的朋友關注做者 或 加我微信 拉你進入spring cloud社區羣
微信公衆號:java架構師修行
本公衆號從2018-5.1日 - 2019.5.1日期間,將要按照JAVA高級軟件架構師實戰培訓的路線發佈一期完整的架構文章,難度由淺入深,適合有必定開發基礎想轉架構和正在作初級架構開發的人員學習