070_Ribbon 负载均衡
ribbon是什么
ribbon能干嘛
服务消费者子模块集成Eureka和Ribbon
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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-dept-80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--springcloud-api模块,实体类-->
<dependency>
<groupId>com.qing</groupId>
<artifactId>springcloud-api</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-devtools</artifactId>
</dependency>
<!--服务提供者和服务消费者使用eureka,eureka服务端使用eureka-server-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
</project>
application.yml 配置文件
server:
port: 80
# Eureka
eureka:
client:
register-with-eureka: false # 不向eureka注册自己
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
启动类,添加开启Eureka注解
package com.qing.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class, args);
}
}
配置类,配置负载均衡实现RestTemplate
package com.qing.springcloud.config;
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 ConfigBean {
/**
* 注册RestTemplate对象到Spring容器
* @return
*/
@Bean
@LoadBalanced // 配置负载均衡实现RestTemplate Ribbon
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
接口类,通过服务提供者服务名称确定访问的url
// 通过服务提供者服务名称确定访问的url
private static final String REST_URL_PREFIX = "http://springcloud-provider-dept";
package com.qing.springcloud.controller;
import com.qing.springcloud.pojo.Dept;
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.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptConsumerController {
@Autowired
private RestTemplate restTemplate; // 提供多种便捷访问远程http服务的方法,简单的restful服务模板
// private static final String REST_URL_PREFIX = "http://localhost:8001";
// 通过服务提供者服务名称确定访问的url
private static final String REST_URL_PREFIX = "http://springcloud-provider-dept";
@PostMapping("/consumer/dept/add")
public boolean add(Dept dept) {
// url 请求参数 返回值类型
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
}
@GetMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id) {
// url 请求参数(可以不传) 返回值类型
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}
@GetMapping("/consumer/dept/listAll")
public List<Dept> listAll() {
// url 请求参数(可以不传) 返回值类型
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/listAll", List.class);
}
}
启动测试
开启3个Eureka服务端,1个服务提供者,1个服务消费者
复制创建2个服务提供者子模块
复制创建2个数据库
部门表中字段数据库名称db_source数据不同
复制创建2个服务提供者子模块
复制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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-provider-dept-8002</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--springcloud-api模块,实体类-->
<dependency>
<groupId>com.qing</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--服务提供者使用eureka,eureka服务端使用eureka-server-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--完善Eureka监控信息 actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
复制Mapper、Service、ServiceImpl、Controller

package com.qing.springcloud.mapper;
import com.qing.springcloud.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface DeptMapper {
public boolean add(Dept dept);
public Dept queryById(Long id);
public List<Dept> queryAll();
}
package com.qing.springcloud.service;
import com.qing.springcloud.pojo.Dept;
import java.util.List;
public interface DeptService {
public boolean add(Dept dept);
public Dept queryById(Long id);
public List<Dept> queryAll();
}
package com.qing.springcloud.service.impl;
import com.qing.springcloud.mapper.DeptMapper;
import com.qing.springcloud.pojo.Dept;
import com.qing.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public boolean add(Dept dept) {
return deptMapper.add(dept);
}
@Override
public Dept queryById(Long id) {
return deptMapper.queryById(id);
}
@Override
public List<Dept> queryAll() {
return deptMapper.queryAll();
}
}
package com.qing.springcloud.controller;
import com.qing.springcloud.pojo.Dept;
import com.qing.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 提供服务
*/
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient client;
@PostMapping("/dept/add")
public boolean add(Dept dept) {
return deptService.add(dept);
}
@GetMapping("/dept/get/{id}")
public Dept get(@PathVariable("id") Long id) {
return deptService.queryById(id);
}
@GetMapping("/dept/listAll")
public List<Dept> listAll() {
return deptService.queryAll();
}
@GetMapping("/dept/discovery")
public Object discovery() {
// 获取微服务列表的清单
List<String> services = client.getServices();
System.out.println("discovery=>services:" + services);
// 得到一个具体的微服务信息,通过微服务的applicationName获取
List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept");
for (ServiceInstance instance : instances) {
System.out.println(
"serviceId=》" + instance.getServiceId() + "\n"
+ "host=》" + instance.getHost() + "\n"
+ "port=》" + instance.getPort() + "\n"
+ "uri=》" + instance.getUri() + "\n"
+ "instanceId=》" + instance.getInstanceId() + "\n"
);
}
return this.client;
}
}
复制mybatis配置文件和mapper文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qing.springcloud.mapper.DeptMapper">
<insert id="add" parameterType="com.qing.springcloud.pojo.Dept">
insert into dept (dname, db_source)
values (#{dname}, database())
</insert>
<select id="queryById" parameterType="Long" resultType="com.qing.springcloud.pojo.Dept">
select * from dept where deptno = #{id}
</select>
<select id="queryAll" resultType="com.qing.springcloud.pojo.Dept">
select * from dept
</select>
</mapper>
复制application.yml配置文件
修改:server.port=8002
修改:spring.datasource.url=db02
注:spring.application.name不能修改,因为服务消费者通过服务名称访问服务提供者
server:
port: 8002
mybatis:
type-aliases-package: com.qing.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
# 开启驼峰
# configuration:
# map-underscore-to-camel-case: true
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db02?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# Eureka
eureka:
client:
service-url:
# 单机:defaultZone: http://localhost:7001/eureka/ # 使用Eureka服务端配置的注册url
# 集群:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ # 使用Eureka服务端配置的注册url
# instance:
# instance-id: springcloud-provider-dept-zdy # 修改Eureka监控页面上服务默认描述
# 监控信息 actuator-info配置
info:
app.name: qing-springcloud
company.name: 清风阁
复制启动类
package com.qing.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 启动类
*/
@EnableDiscoveryClient // 开启服务发现
@EnableEurekaClient // 在服务启动后自动注册到Eureka中
@SpringBootApplication
public class DeptProvider_8002 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8002.class, args);
}
}
启动测试
启动3个Eureka服务端,3个服务提供者,1个服务消费者






3个服务提供者注册到了Eureka,3次访问,负载均衡默认采用轮询策略,分别访问3个服务提供者
自定义负载均衡算法
源码
几种策略
设置负载均衡策略为随机
编写自定义策略Rule
注:不要和主启动类同级,同级会被主启动类扫描到

package com.qing.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RuleConfig {
/**
* 负载均衡策略默认为轮询,现在设置为随机
* @return
*/
@Bean
public IRule rule() {
return new RandomRule();
}
}
启动类,添加注解@RibbonClient
package com.qing.springcloud;
import com.qing.myrule.RuleConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
// 在微服务启动的时候就去加载自定义策略Rule类的Ribbon类
@RibbonClient(name = "springcloud-provider-dept", configuration = RuleConfig.class)
@EnableEurekaClient
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class, args);
}
}
启动测试
启动3个Eureka服务端,3个服务提供者,1个服务消费者



负载均衡策略已改为随机,随机访问服务提供者
自定义负载均衡算法
随机策略源码参考

/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.loadbalancer;
import com.netflix.client.config.IClientConfig;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* A loadbalacing strategy that randomly distributes traffic amongst existing
* servers.
*
* @author stonse
*
*/
public class RandomRule extends AbstractLoadBalancerRule {
/**
* Randomly choose from all living servers
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers(); // 获得活着的服务
List<Server> allList = lb.getAllServers(); // 获得全部的服务
int serverCount = allList.size();
if (serverCount == 0) {
/*
* No servers. End regardless of pass, because subsequent passes
* only get more restrictive.
*/
return null;
}
int index = chooseRandomInt(serverCount); // 根据全部服务的数量生成区间随机数
server = upList.get(index); // 从活着的服务中,随机获取一个
if (server == null) {
/*
* The only time this should happen is if the server list were
* somehow trimmed. This is a transient condition. Retry after
* yielding.
*/
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
// Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
// TODO Auto-generated method stub
}
}
复制RandomRule修改为自己的Rule
package com.qing.myrule;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class MyRule extends AbstractLoadBalancerRule {
/*
每个服务,访问3次,换下一个服务
*/
private int total = 0; // 被调用的次数
private int currentIndex = 0; // 当前被调用的服务
// @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null;
while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers(); // 获得活着的服务
List<Server> allList = lb.getAllServers(); // 获得全部的服务
int serverCount = allList.size();
if (serverCount == 0) {
return null;
}
// int index = chooseRandomInt(serverCount); // 根据全部服务的数量生成区间随机数
// server = upList.get(index); // 从活着的服务中,随机获取一个
// =====================================================
/*
每个服务,访问3次,换下一个服务
*/
if (total < 3) {
server = upList.get(currentIndex);
total++;
} else {
total = 0;
currentIndex++;
if (currentIndex >= upList.size()) {
currentIndex = 0;
}
server = upList.get(currentIndex); // 从活着的服务中,获取指定的服务
}
// ========================================================
if (server == null) {
Thread.yield();
continue;
}
if (server.isAlive()) {
return (server);
}
server = null;
Thread.yield();
}
return server;
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
@Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
// TODO Auto-generated method stub
}
}
配置类,修改负载均衡策略为自定义策略
package com.qing.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RuleConfig {
/**
* 负载均衡策略默认为轮询,现在设置为随机
* @return
*/
@Bean
public IRule rule() {
// return new RandomRule();
return new MyRule();
}
}
启动测试
负载均衡策略为:每个服务,访问3次,换下一个服务


















浙公网安备 33010602011771号