Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。 简而言之:
2.创建Feign调用演示工程
2.1 准备工作
启动上篇中Eureka演示工程中Eureka Server与Eureka Client Provider服务 并在Eureka Client Provider中Controller增加一个接口用于Feign调用测试
@RestController @RequestMapping("/demo") public class DemoController { @Value("${server.port:#{null}}") private String serverPort; @GetMapping("/hello") public String hello() { return "Hello " + serverPort; } @GetMapping("/feign") public String feignTest(@RequestParam(value = "name") String name){ return "Hello Feign "+ name; } } 复制代码

2.2 创建一个服务消费者
新建一个maven工程,取名为:spring-cloud-feign-demo,添加如下依赖pom.xml中
<?xml version="1.0" encoding="UTF-8"?> <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-demo</artifactId> <groupId>com.hxmec</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-feign-demo</artifactId> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project> 复制代码
新建application.yml,增加下面的配置
server: port: 9003 spring: application: name: feign-demo logging: pattern: console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n' eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ 复制代码
创建一个启动类 FeignDemoApplication,添加Feign相关注解
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class FeignDemoApplication { public static void main(String[] args) { SpringApplication.run(FeignDemoApplication.class, args ); } } 复制代码
编写Feign接口服务
@FeignClient(value = "eureka-client-provider")//此名称为消费提供者注册的服务名称 public interface FeignService { @RequestMapping(value = "/demo/feign",method = RequestMethod.GET) String feignTest(@RequestParam(value = "name") String name); } 复制代码
编写Controller层调用Feign接口实例
@RestController @RequestMapping("/feign") @AllArgsConstructor public class FeignClientController { private final FeignService feignService; @GetMapping(value = "/test1") public String feign(@RequestParam String name) { return feignService.feignTest(name); } } 复制代码
启动项目,打开http://localhost:8888可以看到应用已经注册到Eureka注册中心

请求http://localhost:9003/feign/test1?name=Trazen验证是否成功调用服务提供者接口

原文
https://juejin.im/post/5ef2b3abe51d4573ce5a4ed0
本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » SpringCloud组件之Feign