在微服务架构中,服务之间的调用往往依赖 Feign,而服务调用的稳定性又至关重要。本文将介绍如何将 Feign 与 Sentinel 结合使用,实现服务的容错保护(如降级与熔断),提升系统的健壮性与可用性。
我们创建一个新的微服务,作为 Feign 调用方。pom.xml
中添加如下依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>2.2.2.RELEASEversion>
dependency>
application.yml
在配置文件中开启 Feign 对 Sentinel 的支持,并配置基本信息:
server:
port: 8380
feign:
sentinel:
enabled: true # 开启 Sentinel 对 Feign 的支持
spring:
application:
name: feign
cloud:
sentinel:
transport:
dashboard: localhost:8080 # Sentinel 控制台地址
nacos:
discovery:
server-addr: 127.0.0.1:8848 # Nacos 注册中心地址
我们通过 Feign 调用名为 provider
的服务:
package com.southwind.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("provider")
public interface ProviderFeign {
@GetMapping("/index")
String index();
}
通过注入 Feign 接口,实现接口调用:
package com.southwind.controller;
import com.southwind.feign.ProviderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
@Autowired
private ProviderFeign providerFeign;
@GetMapping("/index")
public String index(){
return providerFeign.index();
}
}
当服务不可用或 Sentinel 触发熔断时,我们希望返回友好的降级提示,这就需要自定义 fallback 类:
package com.southwind.fallback;
import com.southwind.feign.ProviderFeign;
import org.springframework.stereotype.Component;
@Component
public class ProviderFeignFallback implements ProviderFeign {
@Override
public String index() {
return "index-fallback";
}
}
package com.southwind.feign;
import com.southwind.fallback.ProviderFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "provider", fallback = ProviderFeignFallback.class)
public interface ProviderFeign {
@GetMapping("/index")
String index();
}
在微服务架构中,服务之间的调用往往依赖 Feign,而服务调用的稳定性又至关重要。本文将介绍如何将 Feign 与 Sentinel 结合使用,实现服务的容错保护(如降级与熔断),提升系统的健壮性与可用性。
我们创建一个新的微服务,作为 Feign 调用方。pom.xml
中添加如下依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>2.2.2.RELEASEversion>
dependency>
application.yml
在配置文件中开启 Feign 对 Sentinel 的支持,并配置基本信息:
server:
port: 8380
feign:
sentinel:
enabled: true # 开启 Sentinel 对 Feign 的支持
spring:
application:
name: feign
cloud:
sentinel:
transport:
dashboard: localhost:8080 # Sentinel 控制台地址
nacos:
discovery:
server-addr: 127.0.0.1:8848 # Nacos 注册中心地址
我们通过 Feign 调用名为 provider
的服务:
package com.southwind.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("provider")
public interface ProviderFeign {
@GetMapping("/index")
String index();
}
通过注入 Feign 接口,实现接口调用:
package com.southwind.controller;
import com.southwind.feign.ProviderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
@Autowired
private ProviderFeign providerFeign;
@GetMapping("/index")
public String index(){
return providerFeign.index();
}
}
当服务不可用或 Sentinel 触发熔断时,我们希望返回友好的降级提示,这就需要自定义 fallback 类:
package com.southwind.fallback;
import com.southwind.feign.ProviderFeign;
import org.springframework.stereotype.Component;
@Component
public class ProviderFeignFallback implements ProviderFeign {
@Override
public String index() {
return "index-fallback";
}
}
package com.southwind.feign;
import com.southwind.fallback.ProviderFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "provider", fallback = ProviderFeignFallback.class)
public interface ProviderFeign {
@GetMapping("/index")
String index();
}
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
provider
。feign
。http://localhost:8380/index
provider
服务,再次访问,即返回 fallback 内容:index-fallback
。模块 | 说明 |
---|---|
Feign | 简化 HTTP 请求调用 |
Sentinel | 实现限流、熔断与降级保护 |
fallback | 实现服务不可用时的降级逻辑 |
配置关键点 | 开启 feign.sentinel.enabled=true 并配置 fallback |
通过本文的实践,我们实现了 Feign + Sentinel 的无缝集成,使服务调用具备更强的容错能力。建议大家在生产环境中为每个 Feign 接口都配置 fallback,防止服务雪崩。
provider
。feign
。http://localhost:8380/index
provider
服务,再次访问,即返回 fallback 内容:index-fallback
。模块 | 说明 |
---|---|
Feign | 简化 HTTP 请求调用 |
Sentinel | 实现限流、熔断与降级保护 |
fallback | 实现服务不可用时的降级逻辑 |
配置关键点 | 开启 feign.sentinel.enabled=true 并配置 fallback |
通过本文的实践,我们实现了 Feign + Sentinel 的无缝集成,使服务调用具备更强的容错能力。建议大家在生产环境中为每个 Feign 接口都配置 fallback,防止服务雪崩。
如有帮助,欢迎点赞、评论、收藏!
后续将分享更多微服务高可用相关内容,敬请关注。