本文主要是介绍springcloud+Ribbon负载均衡,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SpringCloud + Ribbon负载均衡
1.Ribbon简介
1.Ribbon主要实现客户端负载均衡
2.客户端负载均衡Ribbon与服务端负载均衡Nginx的区别:
Ribbon:服务实例在客户端,客户端从Eureka Server获取服务清单以后,通过负载均衡算法,在多个服务器中选择一个访问;
Nginx:服务实例在服务端,服务器进行负载均衡算法分配。
2.Ribbon使用
2.1.前置工作:
需要搭建Eureka server,我们以一台Eureka Server为例。详细配置见上一篇博客。
https://blog.csdn.net/u013071014/article/details/111031306
2.2.创建服务提供者项目
1.在parent项目的pom.xml文件中添加web依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.6.RELEASE</version>
</dependency>
2.在parent项目右键,New—> Maven Module,名称命名为springcloud-provider1
3.修改pom.xml文件
<packaging>jar</packaging><dependencies><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>
4.配置application.yml
spring:application:name: springcloud-provider1server:port: 8001eureka:client:service-url:defaultZone: http://localhost:8761/eureka/
5.添加主启动类SpringCloudProviderApplication,使用@EnableEurekaClient注解
package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class SpringCloudProviderApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudProviderApplication.class, args);}}
6.添加controller
package com.springcloud.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping(path = "/provider")
public class ProviderController {@RequestMapping(path = "/getProvider", method = RequestMethod.GET)public String getProvider() {return "Provider1";}
}
7.为方便测试,我们依此步骤,再创建两个服务提供者,project名称分别为springcloud-provider2和springcloud-provider3,但是application.yml文件中配置和的服务名都设置为springcloud-provider1,端口分别为8002和8003。ProviderController中API返回值分别设置为Provider2和Provider3
8.项目结构如下
9.先启动Eureka Server,再分别启动三个Provider,进入Eureka管理页面查看服务是否注册成功。
2.3.创建服务消费者
1.在parent项目右键,New—> Maven Module,名称命名为springcloud-consumer1
2.修改pom.xml,由于Eureka已经集成了Ribbon,所以我们无需单独引入Ribbon依赖。
<packaging>jar</packaging><dependencies><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>
3.配置application.yml
spring:application:name: springcloud-consumer1server:port: 8090eureka:client:service-url:defaultZone: http://localhost:8761/eureka/
4.创建主启动类SpringCloudConsumerApplication,使用@EnableEurekaClient注解,并添加RestTemplate。
注:使用RestTemplate开启负载均衡,在调用服务时,也可以直接使用服务名调用,避免IP访问
package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
public class SpringCloudConsumerApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApplication.class, args);}@Bean@LoadBalanced //开启负载均衡public RestTemplate restTemplate() {return new RestTemplate();}}
5.添加controller
package com.springcloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping(path = "/consumer")
public class ConsumerController {@Autowiredprivate RestTemplate restTemp;@RequestMapping(path = "/getConsumer", method = RequestMethod.GET)public String getConsumer() {String result = restTemp.getForObject("http://springcloud-provider1/provider/getprovider", String.class);return result;}
}
6.多次call consumer的API访问测试,我们会发现返回值Provider1、Provider2、Provider3以轮询的方式出现。
7.注意:
我在测试的时候,开始有如下报错
在我本地maven仓库中也确实没找到对应的jar包,于是,在consumer的pom文件中新增了如下依赖:
<dependency><groupId>com.liferay</groupId><artifactId>org.apache.commons.configuration</artifactId><version>1.10.LIFERAY-PATCHED-2</version>
</dependency>
3.负载均衡策略
3.1.默认负载均衡策略
通过上述步骤,我们发现返回值以轮询的方式出现,也就说明consumer在调用provider的时候,是以轮询的方式访问。事实确实如此,Ribbon默认的负载均衡策略是轮询。
框架自带7种实现:com.netflix.loadbalancer.RoundRobinRule - 轮询com.netflix.loadbalancer.RandomRule - 随机com.netflix.loadbalancer.RetryRule - 重试,先按RoundRobinRule进行轮询,如果失败就在指定时间内进行重试com.netflix.loadbalancer.WeightedResponseTimeRule - 权重,响应速度越快,权重越大,越容易被选中。com.netflix.loadbalancer.BestAvailableRule - 先过滤掉不可用的处于断路器跳闸转态的服务,然后选择一个并发量最小的服务com.netflix.loadbalancer.AvailabilityFilteringRule - 先过滤掉故障实例,再选择并发量较小的实例com.netflix.loadbalancer.ZoneAvoidanceRule - 默认规则,复合判断server所在区域的性能和server的可用性进行服务的选择。
3.2.修改Ribbon负载均衡策略
1.新建一个不会被@ComponentScan组件扫描到的包
2.修改负载均衡策略
package com.springcloud.myRule;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;@Configuration
public class myRule {@Beanpublic IRule ribbonRule() {return new RandomRule();}}
3.在主启动类添加@RibbonClient注解
package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;import com.springcloud.myRule.myRule;@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "springcloud-consumer1", configuration = myRule.class)
public class SpringCloudConsumerApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApplication.class, args);}@Bean@LoadBalanced //开启负载均衡public RestTemplate restTemplate() {return new RestTemplate();}}
4.重新启动consumer,我们可以发现返回值不再是以轮询的方式出现。
5.也可以参照此方法自定义负载均衡策略
4.源码地址
https://github.com/DamonLiu666/springcloud_test
这篇关于springcloud+Ribbon负载均衡的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!