下面是 Spring Cloud OpenFeign 远程调用的完整攻略以及两条示例说明。
什么是 Spring Cloud OpenFeign?
Spring Cloud OpenFeign 是 Spring Cloud 生态圈中的一款轻量级的 HTTP 客户端组件,它可以用来简化 HTTP 请求客户端的开发。Spring Cloud OpenFeign 基于 Netflix 开源的 Feign 库进行了拓展,可以让开发人员使用注解的方式来定义和维护 HTTP 接口。除此之外,它还提供了负载均衡和服务降级等功能。
如何使用 Spring Cloud OpenFeign?
1. 引入 Spring Cloud OpenFeign 依赖
在项目的 pom.xml 文件中加入 Spring Cloud OpenFeign 的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. 开启 Feign 支持
在 Spring Boot 启动类中添加@EnableFeignClients
注解开启 Feign 支持,并在@FeignClient
注解中指定要调用的服务名。
@SpringBootApplication
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3. 定义 Feign 接口
使用@FeignClient
注解定义一个 Feign 接口并编写访问远程服务的方法,其中value
属性指定服务名,fallback
属性指定服务降级的实现类。
@FeignClient(value = "service-a", fallback = ServiceAFeignClientFallback.class)
public interface ServiceAFeignClient {
@GetMapping("/hello")
String hello();
}
@Component
class ServiceAFeignClientFallback implements ServiceAFeignClient {
@Override
public String hello() {
return "Service A is down";
}
}
4. 注入 Feign 接口
将定义的 Feign 接口注入到需要访问远程服务的类中,然后即可直接调用定义的方法访问服务。
@RestController
public class TestController {
@Autowired
private ServiceAFeignClient serviceAFeignClient;
@GetMapping("/test")
public String test(){
return serviceAFeignClient.hello();
}
}
示例 1:服务间调用
下面以两个微服务 service-a 和 service-b 之间互相调用为例:
- service-a 启动时需要指定服务端口号,在 application.yml 中配置:
server:
port: 8080
spring:
application:
name: service-a
- service-b 启动时需要指定服务端口号、服务注册中心地址和 Feign 接口扫描包名,在 application.yml 中配置:
server:
port: 8081
spring:
application:
name: service-b
cloud:
discovery:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
feign:
client:
base-package: com.example.servicea.feign
- 在 service-a 中定义 Feign 接口用于调用 service-b:
@FeignClient(value = "service-b", fallback = ServiceBFeignClientFallback.class)
public interface ServiceBFeignClient {
@GetMapping("/hello")
String hello();
}
@Component
class ServiceBFeignClientFallback implements ServiceBFeignClient {
@Override
public String hello() {
return "Service B is down";
}
}
- 在 service-b 中定义 Feign 接口用于调用 service-a:
@FeignClient(value = "service-a", fallback = ServiceAFeignClientFallback.class)
public interface ServiceAFeignClient {
@GetMapping("/hello")
String hello();
}
@Component
class ServiceAFeignClientFallback implements ServiceAFeignClient {
@Override
public String hello() {
return "Service A is down";
}
}
- 在 service-a 中注入 service-b 的 Feign 接口,并调用 hello 方法:
@RestController
public class TestController {
@Autowired
private ServiceBFeignClient serviceBFeignClient;
@GetMapping("/test")
public String test(){
return serviceBFeignClient.hello();
}
}
- 在 service-b 中注入 service-a 的 Feign 接口,并调用 hello 方法:
@RestController
public class TestController {
@Autowired
private ServiceAFeignClient serviceAFeignClient;
@GetMapping("/test")
public String test(){
return serviceAFeignClient.hello();
}
}
示例 2:服务调用外部 API
下面以一个服务调用外部 API 为例:
- 在 application.yml 文件中配置外部 API 的地址和 Feign 接口扫描包名:
spring:
cloud:
feign:
client:
base-package: com.example.service.feign
external:
api:
url: http://api.example.com
- 在服务中定义 Feign 接口用于访问外部 API:
@FeignClient(value = "external-api", url = "${spring.external.api.url}")
public interface ExternalApiFeignClient {
@GetMapping("/hello")
String hello();
}
- 在需要访问外部 API 的类中注入 Feign 接口,并调用 hello 方法:
@RestController
public class TestController {
@Autowired
private ExternalApiFeignClient externalApiFeignClient;
@GetMapping("/test")
public String test(){
return externalApiFeignClient.hello();
}
}
这里的@FeignClient(url="${spring.external.api.url}")
注解中的 url 属性可以指定用于调用外部 API 的地址,${spring.external.api.url}
则是从 application.yml 文件中获取该属性的值。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring Cloud OpenFeign 远程调用 - Python技术站