Ribbon是Netflix開源的實現了負載均衡等功能的RPC客戶端。
支持HTTP、TCP、UDP協議,且有必定的容錯、緩存等機制。java
Spring Cloud基於Ribbon封裝了Spring Cloud Ribbon,方便結合Eureka、Consul等服務治理框架使用。Ribbon的主要做用是:從服務器端拿到對應服務列表後以負載均衡的方式訪問對應服務。git
從這張圖上來講,Ribbon主要是從Eureka拿到服務列表以後,以負載均衡的策略選擇其中一臺發起調用。github
框架 | 版本 |
---|---|
Spring Boot | 2.0.0.RELEASE |
Spring Cloud | Finchley.BUILD-SNAPSHOT |
JDK | 1.8.x |
參考:https://ken.io/note/spring-cloud-eureka-quickstartweb
參考:https://ken.io/note/spring-cloud-eureka-quickstartspring
項 | 說明 |
---|---|
GroupId | io.ken.springcloud.testservice |
ArtifactId | testservice |
pom.xml、啓動類(App.java)保持不變apache
server: port: 8602 spring: application: name: testservice eureka: client: serviceUrl: defaultZone: http://localhost:8800/eureka/ app: ip: localhost
package io.ken.springcloud.testservice.model; public class Result { private int code; private String message; private Object content; private String serviceName; private String host; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getContent() { return content; } public void setContent(Object content) { this.content = content; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } }
package io.ken.springcloud.testservice.model; public class Plus { private int numA; private int numB; public int getNumA() { return numA; } public void setNumA(int numA) { this.numA = numA; } public int getNumB() { return numB; } public void setNumB(int numB) { this.numB = numB; } }
package io.ken.springcloud.testservice.controller; import io.ken.springcloud.testservice.model.Plus; import io.ken.springcloud.testservice.model.Result; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Value("${spring.application.name}") private String serviceName; @Value("${app.ip}") private String address; @Value("${server.port}") private String port; @RequestMapping("/") public Object index() { Result result = new Result(); result.setServiceName(serviceName); result.setHost(String.format("%s:%s", address, port)); result.setMessage("hello"); return result; } @RequestMapping("/plus") public Object plus(Plus plus) { Result result = new Result(); result.setServiceName(serviceName); result.setHost(String.format("%s:%s", address, port)); result.setMessage("success"); result.setContent(plus.getNumA() + plus.getNumB()); return result; } }
分別以端口8602,8603啓動testservice。
此時testservice等因而在Eureka Server註冊了兩個實例緩存
分別訪問 http://localhost:8602,http://localhost:8603服務器
將會看到如下信息:app
# http://localhost:8602 { "code": 0, "message": "success", "content": 0, "serviceName": "testservice", "host": "localhost:8062" } # http://localhost:8603 { "code": 0, "message": "success", "content": 0, "serviceName": "testservice", "host": "localhost:8063" }
使用maven-archtype-quickstart模板建立項目負載均衡
項 | 說明 |
---|---|
GroupId | io.ken.springcloud.ribbonclient |
ArtifactId | ribbonclient |
<?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>io.ken.springcloud.ribbonclient</groupId> <artifactId>ribbonclient</artifactId> <version>1.0-SNAPSHOT</version> <name>ribbonclient</name> <url>http://ken.io</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <finalName>ribbonclient</finalName> </build> </project>
修改\src\main\java\io\ken\springcloud\eurekaserver\App.java
基於Spring Boot建立啓動類,添加上 @EnableDiscoveryClient
註解
而且向程序的ioc注入一個bean: restTemplate,並經過@LoadBalanced
註解代表這個restRemplate開啓負載均衡的功能。
package io.ken.springcloud.ribbonclient; 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 App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
TestService做爲訪問testservice的代理服務類,經過RestTemplate訪問遠程testservice,Ribbon會負責把servicename替換成實際要訪問的host
package io.ken.springcloud.ribbonclient.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class TestService { @Autowired private RestTemplate restTemplate; public Object index() { return restTemplate.getForObject("http://testservice", String.class); } public Object plus(int numA, int numB) { String url = String.format("http://testservice/plus?numA=%s&numB=%s", numA, numB); return restTemplate.getForObject(url, String.class); } }
TestController做爲訪問服務testservice的入口
package io.ken.springcloud.ribbonclient.controller; import io.ken.springcloud.ribbonclient.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private TestService testService; @RequestMapping("/") public Object index() { return "ribbon client"; } @RequestMapping("/ti") public Object ti() { return testService.index(); } @RequestMapping("/plus") public Object plus(@RequestParam("numa") int numA, @RequestParam("numb") int numB) { return testService.plus(numA, numB); } }
在\src\main下建立文件夾resources文件夾並設置爲Resources Root
在resources文件夾下建立application.yml文件並配置Eureka Client
server: port: 8604 spring: application: name: ribbonclient eureka: client: serviceUrl: defaultZone: http://localhost:8800/eureka/
RibonClient項目啓動後,訪問 http://localhost:8604/ti
會交替顯示如下內容
{ "code": 0, "message": "hello", "content": null, "serviceName": "testservice", "host": "localhost:8602" } { "code": 0, "message": "hello", "content": null, "serviceName": "testservice", "host": "localhost:8603" }
https://github.com/ken-io/springcloud-course/tree/master/chapter-02