SpringCloud系列:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)
2.1、Feign 基本使用
为了方便起见现在将“microcloud-consumer-80”模块复制为了“microcloud-consumer-feign”模块。
1、 【microcloud-consumer-feign】为了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相关依赖包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
feign 包含了 Ribbon 支持,所以导入了以上的依赖包之后就表示项目之中已经存在有了 ribbon 相关支持库。
2、 【microcloud-service】建立一个新的模块,这个模块专门负责客户端接口的定义;
3、 【microcloud-service】修改 pom.xml 配置文件,引用“microcloud-api”模块,这样就可以使用到 VO 类了;
<dependency> <groupId>cn.study</groupId> <artifactId>microcloud-api</artifactId> </dependency>
4、 【microcloud-service】此时如果要通过 Feign 进行远程 Rest 调用,那么必须要考虑服务的认证问题。
· 此时可以删除原始的 RestConfig 进行的配置处理,然后添加feign的认证配置类
package cn.study.commons.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.auth.BasicAuthRequestInterceptor; @Configuration public class FeignClientConfig { @Bean public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("studyjava", "hello"); } }
5、 【microcloud-service】建立一个 IDeptClientService 接口;
package cn.study.service; import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import cn.study.commons.config.FeignClientConfig; import cn.study.vo.Dept; /** * 通过注解@FeignClient添加接口对应的远程微服务名称value="MICROCLOUD-PROVIDER-DEPT"和 * 服务的认证configuration=FeignClientConfig.class * */ @FeignClient(value="MICROCLOUD-PROVIDER-DEPT",configuration=FeignClientConfig.class) public interface IDeptClientService { @RequestMapping(method=RequestMethod.GET,value="/dept/get/{id}") public Dept get(@PathVariable("id") long id) ; @RequestMapping(method=RequestMethod.GET,value="/dept/list") public List<Dept> list() ; @RequestMapping(method=RequestMethod.POST,value="/dept/add") public boolean add(Dept dept) ; }
6、 【microcloud-consumer-feign】修改 pom.xml 配置文件,引入 microcloud-service 开发包:
<dependency> <groupId>cn.study</groupId> <artifactId>microcloud-service</artifactId> </dependency>
7、 【microcloud-consumer-feign】修改 ConsumerDeptController 控制器程序类;
package cn.study.microcloud.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.study.service.IDeptClientService; import cn.study.vo.Dept; @RestController public class ConsumerDeptController { @Resource private IDeptClientService deptService ; @RequestMapping(value = "/consumer/dept/get") public Object getDept(long id) { return this.deptService.get(id); } @RequestMapping(value = "/consumer/dept/list") public Object listDept() { return this.deptService.list(); } @RequestMapping(value = "/consumer/dept/add") public Object addDept(Dept dept) throws Exception { return this.deptService.add(dept); } }
8、 【microcloud-consumer-feign】修改程序启动主类,追加操作处理。
package cn.study.microcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages={"cn.study.service"})//进行接口IDeptClientService的扫描生成使得可以注入到ConsumerDeptController里面 public class Consumer_80_StartSpringCloudApplication { public static void main(String[] args) { SpringApplication.run(Consumer_80_StartSpringCloudApplication.class, args); } }
9、 启动测试:http://client.com/consumer/dept/get?id=1
· 可以发现 Feign 在处理的时候自带有负载均衡的配置项
2.2、Feign 相关配置
1、 【microcloud-consumer-feign】Feign 之中最为核心的作用就是将 Rest 服务的信息转换为接口,但是在实际的使用之中也需要考虑到一些配置情况,例如:数据压缩,Rest 的核心本质在于:JSON 数据传输(XML、文本),于是就必须思考一种情况,如果用户发送的数据很大,这个时候可以考虑修改 application.yml 配置文件对传输数据进行压缩;
feign: compression: request: mime-types: # 可以被压缩的类型 - text/xml - application/xml - application/json min-request-size: 2048 # 超过2048的字节进行压缩
2、 如果有需要则可以在项目之中开启 feign 的相关日志信息(默认不开启):
· 【microcloud-consumer-feign】修改 application.yml 配置文件,追加日志追踪:
logging: level: cn.study.service: DEBUG
· 【microcloud-service】修改 FeignClientConfig,开启日志的输出:
package cn.study.commons.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; @Configuration public class FeignClientConfig { @Bean public Logger.Level getFeignLoggerLevel() { return feign.Logger.Level.FULL ; } @Bean public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("studyjava", "hello"); } }
再次运行程序现在可以观察到如下的流程:
· 当使用 Feign 要通过接口的方法访问 Rest 服务的时候会根据设置的服务类型发出请求,这个请求是发送给 Eureka(地址: “http://MICROCLOUD-PROVIDER-DEPT/dept/list”);
· 随后由于配置了授权处理,所以继续发送授权信息(“Authorization”);
· 在进行服务调用的时候 Feign 融合了 Ribbon 技术,所以也支持有负载均衡的处理;
**总结:**Feign = RestTempate + HttpHeader + Ribbon + Eureka 综合体 = 业务接口的自动实例化
3ff7- SpringCloud系列六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)
- 使用springcloud的feign调用服务时出现的错误:关于实体转换成json错误的介绍
- Spring Cloud学习系列第二章:使用Feign调用服务
- SpringCloud(第 013 篇)电影微服务使用定制化 Feign 在客户端进行负载均衡调度并为 Feign 配置帐号密码登录认证 Eureka
- 微服务笔记之Spring Cloud 中使用Feign调用接口服务(Finchley)
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- Springcloud踩坑记---使用feignclient远程调用服务404
- SpringCloud(第 013 篇)电影微服务使用定制化 Feign 在客户端进行负载均衡调度并为 Feign 配置帐号密码登录认证 Eureka
- 基于spring-cloud相关技术整合,实现接口调用、服务容错、动态路由配置等
- SpringCloud 使用feign调用服务
- SpringCloud系列七:Hystrix 熔断机制(Hystrix基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)
- 语义Web服务的API使用实例:OWL-S API结合matchmaker、推理机(Jena、Pellet)进行语义转换、匹配、组合及调用web服务
- sshd服务基本配置和使用(201407改)
- WCF系列(一) -- 完全不使用配置文件构建和使用WCF服务
- Gulp系列教程:使用BrowserSync浏览及相关配置
- SpringCloud-----2、Eureka实现服务治理(Feign客户端声明式负载均衡调用服务)
- SpringBoot使用FeignClient进行服务间的调用,传递headers信息
- 9.5 远程服务调用框架-远程服务调用协议-RPC基本协议配置
- Tomcat系列之服务器的基本配置及Nginx反向代理tomcat服务