首页 技术 正文
技术 2022年11月15日
0 收藏 584 点赞 2,917 浏览 3275 个字

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

12345678910111213141516171819202122232425262728293031323334353637383940 <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

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

定义服务接口类UserClient.java

使用@FeignClient(“biz-service-0”)注解来绑定该接口对应biz-service-0服务

12345678910111213 @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,具体如下

1234567891011121314151617181920212223 @RestControllerpublic 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配置变量

123 spring.application.name=feign-consumerserver.port=8004eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

访问 http://127.0.0.1:8004/getuserinfo

总结:

其实通过Feign封装了HTTP调用服务方法,使得客户端像调用本地方法那样直接调用方法,类似Dubbo中暴露远程服务的方式,区别在于Dubbo是基于私有二进制协议,而Feign本质上还是个HTTP客户端

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295