一、ribbon的作用和含义
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP客⼾端的⾏为。为Ribbon,配置服务提供者的地址列表后,Ribbon就可基于某种负载均衡算法,⾃动地帮助服务消费者去请求。Ribbon默认为我们提供了很多的负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现⾃定义的负载均衡算法。在SpringCloud中,当Ribbon与Eureka配 合使⽤时,Ribbon可⾃动从EurekaServer获取服务提供者的地址列表,并基于负载均衡算法,请求其中⼀个服务提供者的实例 (为了服务的可靠性,⼀个微服务可能部署多个实例)。
二、使用步骤
1.引入jar包
<!-- 引入eureka客户端,eureka内部集成了ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.完整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>
<artifactId>com.springcloud</artifactId>
<groupId>com.sc2020</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>consumer-eureka-client81</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>consumer-client81</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sc2020</groupId>
<artifactId>cloud-payment8001</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sc2020</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 引入eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
3. application配置文件
server:
port: 81
eureka:
client:
register-with-eureka: true #是否将自己注册到注册中心,集群必须设置为true配合ribbon
fetch-registry: true #是否从服务端抓取已有的注册信息
service-url:
defaultZone: http://eureka7001.com:7001/eureka #服务注册中心路径,一般为localhost
instance:
instance-id: consumer-client81
prefer-ip-address: true
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://101.34.49.127:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: 数据库名称
password: 数据库密码
application:
name: consumer-client
mybatis:
mapper-locations: classpath:mapper/*.xml
4.main启动类
@SpringBootApplication
@EnableEurekaClient
//name表示使用负载均衡的服务名称,configuration是指定负载均衡策略类
@RibbonClient(name = "cloud-eureka-service",configuration = MyselfRule.class)
public class ConsumerEurekaClient81Application {
public static void main(String[] args) {
SpringApplication.run(ConsumerEurekaClient81Application.class, args);
}
}
5.负载均衡配置类MyselfRule
package com.example.consumerclient81.springcloud.config;
import com.netflix.loadbalancer.*;
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;
/**
* @description:
* @author: xiaYZ
* @createDate: 2021/11/16
* @version: 1.0
*/
@Configuration
public class MyselfRule {
//设置RestTemplate
@Bean
//调用微服务项目,并负载均衡
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
/**
* description: 随机轮训策略
* version: 1.0
* date: 2021/11/30 13:17
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
@Bean
public IRule myRule(){
return new RandomRule();
}
/**
* description: 轮询策略,Ribbon默认策略
* version: 1.0
* date: 2021/11/30 13:23
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
//@Bean
public IRule myRule2(){
return new RoundRobinRule();
}
/**
* description: 重试策略
* version: 1.0
* date: 2021/11/30 13:25
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
//@Bean
public IRule myRule3(){
return new RetryRule();
}
/**
* description: 最低并发策略
* version: 1.0
* date: 2021/11/30 13:26
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
//@Bean
public IRule myRule4(){
return new BestAvailableRule();
}
/**
* description: 可用过滤策略
* version: 1.0
* date: 2021/11/30 13:26
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
//@Bean
public IRule myRule5(){
return new AvailabilityFilteringRule();
}
/**
* description: 响应时间加权策略
* version: 1.0
* date: 2021/11/30 13:27
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
//@Bean
public IRule myRule6(){
return new WeightedResponseTimeRule();
}
/**
* description: 区域权衡策略
* version: 1.0
* date: 2021/11/30 13:27
* author: xiaYZ
* iteration: 迭代说明
* @param
* @return
*/
//@Bean
public IRule myRule7(){
return new ZoneAvoidanceRule();
}
}
6.Controller控制层代码
@Resource
RestTemplate restTemplate;
public static final String PAYMENT_URL = "http://cloud-eureka-client";
@GetMapping(value = "getMessage")
public String getMessage(){
//调用cloud-eureka-client服务中方法
String forObject = restTemplate.getForObject( PAYMENT_URL +"/paymentController/getPortMessage", String.class);
return forObject;
}