Spring Cloud(Greenwich.SR1) - 服务负载均衡feign

本文介绍在Spring Cloud中使用Feign实现服务的负载均衡与熔断。新建feign-service服务,Feign集成了Ribbon和Hystrix功能,通过配置可实现负载均衡,使用熔断处理类绑定熔断逻辑。不过Spring Cloud某版本后Feign默认关闭熔断能力,需配置打开。

前言

我们已经集成ribbon与hystrix,实现服务的负载均衡与熔断,在Spring Cloud中,使用feign已经集成这两种功能。新建一个module实现feign-service服务,pom依赖如下

        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

1. feign实现ribbon

application.yml可以保持不变,这里修改application-name的值

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8082/eureka/
server:
  port: 8205
spring:
  application:
    name: feign-service

在main方法上注上@EnableFeignClients注解

package com.spring.cloud.feign.servce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignServiceMain {
    public static void main(String[] args) {
        SpringApplication.run(FeignServiceMain.class, args);
    }
}

restTemplate已经不需要我们注入bean了,feign已经实现,并且通过配置文件可配置负载均衡的模式。

我们要改造的是service服务bean,使用@FeignClient(value = "服务名称,注册中心注册的application name")来实现,此时仅需要接口就可以了,feign会动态生成代理类。

package com.spring.cloud.feign.servce.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

@FeignClient(value = "RIBBON-SERVICE")
public interface ConsumerService {

    @RequestMapping(value = "/mark",method = RequestMethod.GET)
    Map consumerMark();
}

接口方法有参数,可使用@RequestParam绑定,同controller的RequestMapping写法一样。 

启动main方法,测试负载均衡,访问http://localhost:8205/consumer/

交替打印,说明负载均衡成功。

2. feign熔断

熔断需要我们写熔断处理逻辑,并绑定处理方法,在feign中,是通过熔断处理类实现的,只需要绑定熔断处理类就可以了,feign熔断后会自动去熔断处理类,通过相应的方法处理。

建立熔断处理类

package com.spring.cloud.feign.servce.service;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ConsumerServiceHystrix implements ConsumerService{

    @Override
    public Map consumerMark() {
        Map<String, String> map = new HashMap<>();
        map.put("error", "service is not used");
        return map;
    }
}

绑定熔断处理逻辑,在FeignClient注解加上参数 fallback = ConsumerServiceHystrix.class

package com.spring.cloud.feign.servce.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

@FeignClient(value = "RIBBON-SERVICE", fallback = ConsumerServiceHystrix.class)
public interface ConsumerService {

    @RequestMapping(value = "/mark",method = RequestMethod.GET)
    Map consumerMark();
}

当发生熔断,feign会自动去找本地实现处理逻辑。

改造后启动main服务

遗憾没有熔断功能,原因是Spring Cloud在某个版本后feign自动是默认关闭熔断能力的,需要配置打开

feign:
  hystrix:
    enabled: true

访问:http://localhost:8205/consumer/

服务熔断了

总结

feign继承了ribbon与hystrix的功能,其中hystrix默认是关闭的,需要配置打开,负载均衡可以均匀的负载服务器的流量,熔断可以防止流量击穿当前服务,进入调用链的下游。 

<think>好的,我现在需要解决用户无法拉取spring-cloud-starter-alibaba-nacos-config依赖的问题。用户提到他们使用的是Spring Cloud Hoxton.SR1Spring Cloud Alibaba 2.1.0.RELEASE版本。首先,我应该检查这两个版本是否兼容。 根据引用[4],用户之前遇到版本兼容性问题,官方推荐升级到更高版本。比如,Hoxton.SR1对应的Spring Cloud Alibaba版本可能不是2.1.0.RELEASE。我需要确认官方文档中的版本对应关系。通常,Spring CloudSpring Cloud Alibaba有特定的版本匹配要求。例如,Hoxton.SR3可能对应Alibaba的2.2.x版本。 接着,检查依赖管理配置是否正确。用户可能在dependencyManagement中没有正确引入BOM,导致Maven或Gradle无法解析正确的版本。引用[1]和[2]展示了如何正确添加依赖管理,尤其是使用正确的groupId和artifactId。用户需要确保在pom.xml中导入了spring-cloud-alibaba-dependencies,并且版本与Spring Cloud版本匹配。 另外,仓库配置可能有问题。Nacos的依赖可能不在Maven中央仓库,或者用户的项目没有配置正确的仓库地址。需要检查是否添加了阿里云的Maven仓库,或者Spring的仓库,确保能够下载到所需的jar包。 还有可能是本地仓库的缓存问题,有时候旧的版本或损坏的缓存会导致拉取失败。建议用户清理本地Maven仓库并重新构建项目。 最后,参考引用[4]中的建议,如果版本确实不兼容,可能需要升级Spring Cloud到Hoxton.SR3及以上,并对应使用Alibaba的2.2.0.RELEASE版本,如引用[1]提到的示例。这样能确保依赖关系正确,解决拉取失败的问题。</think>根据您使用的Spring Cloud Hoxton.SR1Spring Cloud Alibaba 2.1.0.RELEASE版本,无法拉取依赖的排查步骤如下: ### 一、验证版本兼容性 1. **官方版本对照表** Spring Cloud Alibaba 2.1.0.RELEASE官方推荐搭配Spring Cloud Greenwich,与Hoxton.SR1存在兼容性问题[^4]。正确组合应为: $$ Hoxton.SR3 + Spring Cloud Alibaba 2.2.1.RELEASE $$ 或 $$ Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE $$ 2. **依赖树分析** 执行`mvn dependency:tree`查看是否引入冲突的传递依赖,例如旧版本nacos-client ### 二、检查依赖配置 1. **BOM管理** 确保在`<dependencyManagement>`中明确定义版本(参考引用[1]): ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.0.RELEASE</version> <!-- 需与Hoxton匹配 --> <type>pom</type> <scope>import</scope> </dependency> ``` 2. **具体依赖声明** 检查是否正确定义nacos-config依赖(参考引用[2]): ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` ### 三、仓库配置验证 1. **阿里云仓库配置** 在pom.xml中添加镜像仓库: ```xml <repositories> <repository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> ``` ### 四、强制清理缓存 执行以下命令清理Maven缓存: ```bash mvn dependency:purge-local-repository -DactTransitively=false ``` ### 五、推荐升级方案 建议采用稳定版本组合(参考引用[4]): ```xml <spring-cloud.version>Hoxton.SR9</spring-cloud.version> <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值