一.Ribbon简介
说起Ribbon,首先要说下负载均衡。将负载分摊到多个操作单元上进行执行就是负载均衡,简单的理解,就是将多个请求按照某种策略均匀的分配给不同的服务器处理,由此解决单台服务器的压力,减少响应速度。可以想象成以前是十件事一个人做,现在是3个人一起做这十件事,自然处理速度和压力都小的多。而Ribbon就是这样一个实现负载均衡的工具,它是通过代码层面来实现的负载均衡。与之相对的另一种方式就是通过一个独立的进程来分配请求,实现负载均衡,Nngix就是典型例子。
二.通过RestTemplate+Ribbon简单实现负载均衡
- 创建一个eureka-server服务,application.yml配置文件如下:
server:
#端口号
port: 8761
eureka:
instance:
#设置主机ip以及是否提交ip信息
prefer-ip-address: true
hostname: localhost
service-url:
#服务注册地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
client:
register-with-eureka: false
fetch-registry: false
- 创建一个eureka-client客户端,提供一个"/hellworld/{name}"接口,如下:
package com.springcloud.eurekaclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/helloWorld")
public class HelloRibbonController {
@Value("${server.port}")
private String port;
@GetMapping("/{name}")
public String helloRibbon(@PathVariable("name") String name) {
return "hello " + name + ",this eureka port is " + port;
}
}
eureka-client客户端的application.yml配置文件如下:
server:
#端口号
port: 8763 #8762
eureka:
client:
service-url:
#服务注册地址,这里填的是之前在Eureka Server配置的服务注册地址,只要有Eureka Client上线,就会向这个地址注册
defaultZone: http://localhost:8761/eureka/
spring:
application:
#服务名
name: eureka-client
3.创建一个ribbon-client客户端,它会自动从eureka-client里获取服务注册列表,这样访问接口时才能在不同服务间轮询调用,application.yml配置文件如下:
spring:
application:
name: ribbon-client
server:
port: 8764
eureka:
client:
service-url:
defaultZone: http://localhosonfigt:8761/eureka/
然后新增一个RibbonConfig文件,注入RestTemplate 同时通过@LoadBalanced注解开启负载均衡:
package com.example.ribbonclient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
RestTemplate getRestTemlate() {
return new RestTemplate();
}
}
新增一个RibbonService文件:
package com.example.ribbonclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
public String helloRibbon(String name) {
return restTemplate.getForObject("http://eureka-client/helloWorld/" + name, String.class);
}
}
新增一个RibbonController文件:
package com.example.ribbonclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/ribbon")
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@GetMapping("/{name}")
public String helloRibbon(@PathVariable("name") String name) {
return ribbonService.helloRibbon(name);
}
}
- 把3个服务分别启动,其中eureka-client分别用端口8762和8763多实例启动,这样就有两个eureka-client客户端提供服务,通过ribbon-client访问"/hellworld/{name}"接口时,能够轮询的调用服务,如下;
这样,简单的负载均衡就实现了。