Ribbon简介
Ribbon是Netflix发布的负载均衡器,它可以帮我们控制HTTP和TCP客户端的行为。只需为Ribbon配置服务提供者地址列表,Ribbon就可基于负载均衡算法计算出要请求的目标服务地址。
Ribbon默认为我们提供了很多的负载均衡算法,例如轮询、随机、响应时间加权等——当然,为Ribbon自定义负载均衡算法也非常容易,只需实现 IRule 接口即可。
Ribbon入门
新建提供者模块spring-cloud-provider:
pom:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-wsl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-provider</name>
<description>Demo project for Spring Cloud Provider</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka服务端依赖jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml配置文件:
server:
port: 8880
spring:
application:
name: provider
#eureka客户端连接配置
eureka:
client:
service-url:
#注册中心地址
defaultZone: http://localhost:8761/eureka/
instance:
#将ip注册到eureka上
prefer-ip-address: true
controller:
@RestController
public class TestController {
@Value("${server.port}")
private String port;
// 注入配置文件上下文
@Autowired
private ConfigurableApplicationContext applicationContext;
@GetMapping(value = "/test/{name}")
public String test(@PathVariable String name) {
return "success! hello: " + name + " i am from port " + port;
}
// 从上下文中读取配置
@GetMapping(value = "/hi")
public String sayHi() {
return "Hello " + applicationContext.getEnvironment().getProperty("user.name");
}
}
启动spring-cloud-provider模块
先启动spring-cloud-eureka,再启动spring-cloud-provider
打开http://localhost:8761/: 红框部分说明已经注册到注册中心
输入http://localhost:8880/test/wangshilin
提供者调用成功创建消费者模块spring-cloud-consumer-ribbon:
pom:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-wsl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-consumer-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-consumer-ribbon</name>
<description>Demo project for Spring Cloud Consumer Ribbon</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端依赖jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
配置文件application.yml:
server:
port: 8882
spring:
application:
name: consumer-ribbon
#eureka客户端连接配置
eureka:
client:
service-url:
#注册中心地址
defaultZone: http://localhost:8761/eureka/
instance:
#将ip注册到eureka上
prefer-ip-address: true
配置RestTemplate:
@Configuration
public class ConsumerConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
controller:
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping(value = "/test/app/name")
public String test() {
return restTemplate.getForObject("http://provider/test/{name}", String.class, appName);
}
}
启动spring-cloud-consumer-ribbon模块:
启动成功浏览器输入地址http://localhost:8882/test/app/name
先启动eureka、provider模块
为了测试负载均衡,我们修改provider启动配置
在Allow parallel run处打√
修改provider端口号为8881
再启动provider模块
目前启动了两个provider模块,一个是8880端口,一个是8881端口
启动consumer模块:
浏览器中输入:http://localhost:8882/test/app/name
刷新浏览器发现端口号变化
ribbon其实还有很多负载均衡的策略,在此不多说,网上教程一大堆,这里只讲各个模块如何搭建
网友评论