SpringCloud之Feign示例详解
作者:Bob_F 时间:2023-03-16 06:14:32
Feign简介
Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign.
声明式REST客户端:Feign
先要启动eureka_register_service工程(注册中心)和biz-service-0工程(服务生产者)
创建一个maven工程eureka_feign_client
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在应用主类中通过@EnableFeignClients注解开启Feign功能
启动文件FeignApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
定义服务接口类UserClient.java
使用@FeignClient("biz-service-0")注解来绑定该接口对应biz-service-0服务
@FeignClient("biz-service-0")
public interface UserClient {
@RequestMapping(method = RequestMethod.GET, value = "/getuser")
public User getuserinfo();
@RequestMapping(method = RequestMethod.GET, value = "/getuser")
public String getuserinfostr();
@RequestMapping(method = RequestMethod.GET, value = "/info")
public String info();
}
在web层中调用上面定义的UserController,具体如下
@RestController
public class UserController {
@Autowired
UserClient userClient;
@RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
public User getuserinfo() {
return userClient.getuserinfo();
}
@RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)
public String getuserinfostr() {
return userClient.getuserinfostr();
}
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info() {
return userClient.info();
}
}
application.properties配置变量
spring.application.name=feign-consumer
server.port=8004
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
访问 http://127.0.0.1:8004/getuserinfo
总结:
其实通过Feign封装了HTTP调用服务方法,使得客户端像调用本地方法那样直接调用方法,类似Dubbo中暴露远程服务的方式,区别在于Dubbo是基于私有二进制协议,而Feign本质上还是个HTTP客户端
来源:http://www.cnblogs.com/xiaojunbo/p/7094377.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot数据层测试事务回滚的实现流程
![](https://img.aspxhome.com/file/2023/6/75776_0s.png)
C#使用itextsharp生成PDF文件的实现代码
浅试仿 mapstruct实现微服务编排框架详解
![](https://img.aspxhome.com/file/2023/0/63420_0s.png)
springboot加载复杂的yml文件获取不到值的解决方案
SpringBoot2零基础到精通之映射与常用注解请求处理
![](https://img.aspxhome.com/file/2023/1/62141_0s.jpg)
springboot整合solr的方法详解
![](https://img.aspxhome.com/file/2023/0/61580_0s.png)
C#向线程中传递多个参数的解决方法(两种)
Java 自定义注解的魅力
![](https://img.aspxhome.com/file/2023/6/65776_0s.png)
Android中Fragment与Activity的生命周期对比
![](https://img.aspxhome.com/file/2023/4/137524_0s.jpg)
利用C#实现网络爬虫
![](https://img.aspxhome.com/file/2023/6/72476_0s.jpg)
C#中string.Empty和null的区别详解
![](https://img.aspxhome.com/file/2023/8/68088_0s.png)
JDK8中新增的原子性操作类LongAdder详解
![](https://img.aspxhome.com/file/2023/3/76303_0s.jpg)
C#中winform控制textbox输入只能为数字的方法
Android高手进阶教程(二十六)之---Android超仿Path菜单的功能实现!
TC 集群Seata1.6高可用架构源码解析
![](https://img.aspxhome.com/file/2023/1/75801_0s.png)
详解Struts2中json 相互引用死循环解决办法
Android Studio配置(Android Studio4.1为例)
![](https://img.aspxhome.com/file/2023/7/137597_0s.png)
C#简单的向量用法实例教程
C#/VB.NET 在PDF中添加文件包(Portfolio)的方法
![](https://img.aspxhome.com/file/2023/5/94555_0s.png)