Ribbon可以实现客户端的负载均衡和故障转移,默认情况下,Ribbon使用到的负载均衡算法是轮询算法
下面是Ribbon实现客户端负载均衡的列子
使用到的项目有spring-cloud-eureka,spring-cloud-provider-01,spring-cloud-provider-02,spring-cloud-consumer-01
服务提供端
spring-cloud-provider-01
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.example</groupId>
<artifactId>spring-cloud-provider-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-provider-01</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.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>Camden.SR7</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
</project>
application.yaml
server:
port: 8080
spring:
application:
name: spring-cloud-provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
User.java
package com.example.demo.model;
import java.util.Date;
public class User {
private String id;
private String userName;
private int age;
private Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public User(String id, String userName, int age, Date birthday) {
super();
this.id = id;
this.userName = userName;
this.age = age;
this.birthday = birthday;
}
public User(String id, String userName, int age) {
super();
this.id = id;
this.userName = userName;
this.age = age;
}
}
UserController.java
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserController {
private Map<String, User> umap = new HashMap();
public UserController(){
umap.put("1", new User("1", "xiaoming1", 30));
umap.put("2", new User("2", "xiaohong1", 20));
umap.put("3", new User("3", "zhangsan1", 80));
umap.put("4", new User("4", "lisi1", 10));
umap.put("5", new User("5", "wangwu1", 22));
}
@GetMapping(value="/get")
@ResponseBody
public User get(String id){
return umap.get(id);
}
}
spring-cloud-provider-02
spring-cloud-provider-01和spring-cloud-provider-02不同的地方是UserController
其他地方都是一样
UserController.java
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserController {
private Map<String, User> umap = new HashMap();
public UserController(){
umap.put("1", new User("1", "xiaoming", 30));
umap.put("2", new User("2", "xiaohong", 20));
umap.put("3", new User("3", "zhangsan", 80));
umap.put("4", new User("4", "lisi", 10));
umap.put("5", new User("5", "wangwu", 22));
}
@GetMapping(value="/get")
@ResponseBody
public User get(String id){
return umap.get(id);
}
}
服务消费端
spring-cloud-consumer-01
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.example</groupId>
<artifactId>spring-cloud-consumer-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-consumer-01</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.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>Camden.SR7</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
</project>
application.yaml
server:
port: 8082
spring:
application:
name: spring-cloud-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
SpringCloudConsumer01Application.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConsumer01Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConsumer01Application.class, args);
}
}
CoreConfiguration.java
package com.example.demo.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;
@Controller
public class CoreConfiguration {
@LoadBalanced
@Bean
public RestTemplate restTemplateBean(){
return new RestTemplate();
}
}
UserConstroller.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.example.demo.model.User;
@RestController
@RequestMapping(value="/user")
public class UserConstroller {
@Autowired
private RestTemplate restTemplate;
@ResponseBody
@GetMapping("/get")
public User get(String uid){
return restTemplate.getForObject("http://spring-cloud-provider/user/get?id="+uid, User.class);
}
}
User.java
package com.example.demo.model;
import java.util.Date;
public class User {
private String id;
private String userName;
private int age;
private Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public User(String id, String userName, int age, Date birthday) {
super();
this.id = id;
this.userName = userName;
this.age = age;
this.birthday = birthday;
}
public User(String id, String userName, int age) {
super();
this.id = id;
this.userName = userName;
this.age = age;
}
public User(){}
}
验证消费端
http://192.168.88.101:8082/user/get?uid=1
在浏览器中打开http://192.168.88.101:8082/user/get?uid=1,并且不停的刷新,页面中轮回显示下面的内容:
{"id":"1","userName":"xiaoming1","age":30,"birthday":null}
{"id":"1","userName":"xiaoming","age":30,"birthday":null}
当服务提供端挂了一个的时候,可以实现故障转移