Spring Cloud配置负载均衡

在上一篇文章中已经实现了服务的发现,使用feign实现服务的发现,而feign除了实现服务发现之外,还可以用来做负载均衡,在Spring Cloud中负载均衡的实现主要有两种方式,一种是:feign;一种是:ribbon

feign对比ribbon:

Ribbon是一个基于HTTP和TCP客户端的负载均衡器,
Feign其实也使用了ribbon,只要使用@FeignClient时,ribbon就会自动使用。

所以使用Feign是一个采用基于接口的注解的编程方式,更加简便。

以下是这两种方式实现负载示例:

一、Feign负载实现:

在上一篇文章中已经创建了一个生产者service-producer-A和一个消费者service-consumer-feign,

为了演示负载,因此再创建一个生产者service-producer-B,端口为8012,配置和A的一样,只要修改端口即可,

application.yml 如下:

spring:
? application:
? ? name: service-producer
?
server:
? port: 8812
?
eureka:
? client:
? ? serviceUrl:
? ? ? ? ? defaultZone: http://localhost:8010/eureka/ #eureka服务注册地址

新增完成后启动service-producer-B,查看已经启动的服务,可以看到同一个Application下已经有两个不同端口的服务注册了

,如下:

访问消费者测试:http://localhost:8911/hi?id=123刷新,可以看到两个服务就交替实现,实现负载,如下:

二、Ribbon负载实现:

在根目录spring_cloud中创建Maven Moudle模块:service-consumer-ribbon

pom.xml

<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>spring-cloud</artifactId>
?? ??? ?<groupId>com.sam</groupId>
?? ??? ?<version>0.0.1</version>
?? ?</parent>
?? ?<modelVersion>4.0.0</modelVersion>
?? ?<artifactId>service-consumer-ribbon</artifactId>
?
?? ?<dependencies>
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.cloud</groupId>
?? ??? ??? ?<artifactId>spring-cloud-starter-ribbon</artifactId>
?? ??? ?</dependency>
?? ??? ?<!-- 短路器依赖 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.cloud</groupId>
?? ??? ??? ?<artifactId>spring-cloud-starter-hystrix</artifactId>
?? ??? ?</dependency>
?? ?</dependencies>
?
</project>

创建Controller来测试:TestController

package com.sam.service.consumer.ribbon;
?
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
?
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
?
/**
?* @ClassName: TestController
?* @Description: Ribbon+RestTemplate方式
?* @author sam
?* @date 2018年8月9日 下午4:17:10
?*/
@RestController
public class TestController {
?
?? ?@Autowired
?? ?RestTemplate restTemplate;
?
?? ?@RequestMapping("/hi")
?? ?@HystrixCommand(fallbackMethod = "hiFallback")
?? ?public String hi(@RequestParam String id) {
?? ??? ?return restTemplate.getForObject("http://service-producer/hi?id=" + id, String.class);
?? ?}
?
?? ?// 断路器回调方法
?? ?public String hiFallback(String id) {
?? ??? ?return "hi, " + id + ", error!";
?? ?}
}

创建消费者服务启动类:ClientApplicationRibbon

package com.sam.service.consumer.ribbon;
?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
?
/**
?* @ClassName: ClientApplicationRibbon?
?* @Description: 消费者服务(使用Ribbon实现)
?* @author sam?
?* @date 2018年8月10日 下午4:01:21
?*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ClientApplicationRibbon {
?
?? ?public static void main(String[] args) {
?? ??? ?SpringApplication.run(ClientApplicationRibbon.class, args);
?? ?}
?
?? ?@Bean
?? ?@LoadBalanced
?? ?RestTemplate restTemplate() {
?? ??? ?return new RestTemplate();
?? ?}
}

创建配置文件:application.yml

server:
? port: 8910
?
eureka:
? client:
? ? serviceUrl:
? ? ? ? ? defaultZone: http://localhost:8010/eureka/
?
spring:
? application:
? ? ? name: service-consumer-ribbon

以上代码配置完成后!在启动新增的这个消费者服务:service-consumer-ribbon,

可以看到消费者和生产者都可以在注册中心看到,如下图:

访问消费者测试:http://localhost:8910/hi?id=123刷新,可以看到两个服务就交替实现,实现负载,如下:

PS:

除了feign对比ribbon可以实现负载均衡之外,Zuul也可以实现负载,

不过二者有区别:

ribbon/feign是对服务之间调用做负载,是服务之间的负载均衡,zuul是可以对外部请求做负载均衡。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值