ribbon是netflix开源的一款用于客户端负载均衡的框架。现在市面上流行的负载的方案为两种:一种是服务端负载均衡,比如硬件的F5,软件nginx。另一种是客户端负载均衡,就比如ribbon,根据自己的请求情况做负载均衡。
搭建ribbon消费者需要用到eureka高可用的注册中心,搭建eureka高可用注册中心,请看两个月学习springcloud(4):搭建高可用的eureka注册中心 。eureka服务提供者,搭建eureka服务提供者,请看两个月学习springcloud(2):搭建eureka服务提供者 。
话不多说,上代码,边敲边学习,才能记得牢。
1.新建ribbon-consumer模块,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.feng</groupId>
<artifactId>cloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ribbon-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbon-consumer</name>
<description>cloud study for two month</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.yml配置文件
spring:
application:
name: ribbon-consumer
server:
port: 8804
servlet:
context-path: /ribbon
eureka:
instance:
instanceId: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://peer1:8701/register/eureka/,http://peer2:8702/register/eureka/
3.ribbon-consumer模块,创建RestTemplate类,注入@LoadBalanced,开启负载均衡。
@Bean
@LoadBalanced //开启负载均衡
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
4.ribbon-consumer模块新建controller测试类,看看效果如何
package com.feng.cloud.study.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author: fengyantao
* @date: 2019/11/7 下午2:25
* @version: V1.0
* @review: fengyantao/2019/11/7 下午2:25
*/
@RestController
@RequestMapping("/user")
public class RibbonUserController {
@Autowired
private RestTemplate restTemplate;
/**
* 正确示例
* 使用eureka实例注册名访问
*
* @return
*/
@GetMapping("/findMovies")
public String findMovies() {
//注意这里使用的eureka实例的注册名
ResponseEntity<String> forEntity = restTemplate
.getForEntity("http://eureka-producer/producer/movie/getAllMovies", String.class);
String body = forEntity.getBody();
return body;
}
/**
* 错误示例
* 使用ip:port形式访问
*
* @return
*/
@GetMapping("/findMoviesError")
public String findMoviesError() {
//如果使用ip:port形式访问,则会报错 java.lang.IllegalStateException: No instances available for localhost
ResponseEntity<String> forEntity = restTemplate
.getForEntity("http://localhost:8802/producer/movie/getAllMovies", String.class);
String body = forEntity.getBody();
return body;
}
}
注意:开启负载均衡模式后,不能再使用ip:port形式访问eure服务提供者,会报错。 使用eureka服务提供者的注册名代替ip:port形式来进行访问eureka服务提供者
5.先后启动eureka高可用注册模块的peer1,peer2。eureka-producer模块,ribbon-consumer模块。访问ribbon-consumer的测试类http://localhost:8804/ribbon/user/findMovies ,得到eureka服务提供者的响应信息
到此为止,ribbon消费已经搭建完成,代码git地址:https://github.com/fengyantao/cloud-study/tree/master
大家如果有疑问,欢迎下方留言沟通!