一、OpenFegin 介绍

Feign是一个声明性的Web服务客户端。 它使编写Web服务客户端变得更容易。 要使用Feign,请创建一个界面并对其进行注释。 它具有可插入的注释支持,包括Feign注释和JAX-RS注释。 Feign还支持可插拔编码器和解码器。 Spring Cloud增加了对Spring MVC注释的支持,并使用了Spring Web中默认使用的相同HttpMessageConverters。 Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载均衡的http客户端。

二、将OpenFegin应用到项目中

添加依赖到项目中:

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

新建一个FeignClient类

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
//@PathVariable得设置value
@GetMapping("/simple/{id}")
User findById(@PathVariable("id") Long id); //@PathVariable得设置value @PostMapping("/user")
User postUser(@RequestBody User user); // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。
@GetMapping("/get-user")
User getUser(User user);
}

在movie的controller中。使用feign调用user微服务提供的接口

 package com.zwjk.cloud.controller;

 import com.zwjk.cloud.entity.User;
import com.zwjk.cloud.fegin.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:38.
* @Description :
*/
@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/test")
public User testPost(User user) {
return this.userFeignClient.postUser(user);
} @GetMapping("/test-get")
public User testGet(User user) {
return this.userFeignClient.getUser(user);
} }
 package com.zwjk.cloud.controller;

 import com.zwjk.cloud.entity.User;
import com.zwjk.cloud.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:08.
* @Description :
*/
@RestController
public class UserController { @Autowired
private UserRepository userRepository; @GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return this.userRepository.getUserById(id);
} @PostMapping("/user")
public User postUser(@RequestBody User user) {
return user;
} // 该请求不会成功
@GetMapping("/get-user")
public User getUser(User user) {
return user;
}
}

访问地址:http://localhost:8010/movie/1

查看结果返回:

这边需要注意的是,在feignClient中,注释表明的说明。

这里,就简单实现了使用feignClient调用服务接口。

下面看一下,如何复写默认的FeignClientsConfihuration

https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.1.0.RELEASE/single/spring-cloud-openfeign.html#spring-cloud-feign-overriding-defaults

官方文档中,有这样的说明

Spring Cloud的Feign支持的核心概念是指定客户端的概念。 每个feign客户端都是一组组件的一部分,这些组件一起工作以按需联系远程服务器,并且该集合具有一个名称,您可以使用@FeignClient注释将其作为应用程序开发人员提供。 Spring Cloud使用FeignClientsConfiguration按需为每个命名客户端创建一个新的集合作为ApplicationContext。 这包含(除其他外)feign.Decoder,feign.Encoder和feign.Contract。 可以使用@FeignClient批注的contextId属性覆盖该集合的名称。

Spring Cloud允许您通过使用@FeignClient声明其他配置(在FeignClientsConfiguration之上)来完全控制假装客户端。 例:

@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}

这边有个警告,跟ribbon的自定义配置一样,自定义的配置,需要放在扫描包的外部,查看一下怎么写,https://github.com/OpenFeign/feign

下面看一下代码:

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import com.zwjk.config.UserConfiguration;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient(name = "microservice-provider-user", configuration = UserConfiguration.class)
public interface UserFeignClient {
//@PathVariable得设置value
@RequestLine("GET /simple/{id}")
User findById(@Param("id") Long id); //@PathVariable得设置value }
 package com.zwjk.config;

 import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author : Jixiaohu
* @Date : 2019-04-13.
* @Time : 10:20.
* @Description :
*/
@Configuration
public class UserConfiguration { @Bean
public Contract feignContract() {
return new feign.Contract.Default();
} @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}

其他的不需要修改,下面看一下目录结构:

UserConfiguretion需要放在外面。

从零开始学spring cloud(七) -------- Spring Cloud OpenFegin的更多相关文章

  1. Spring系列(七) Spring MVC 异常处理

    Servlet传统异常处理 Servlet规范规定了当web应用发生异常时必须能够指明, 并确定了该如何处理, 规定了错误信息应该包含的内容和展示页面的方式.(详细可以参考servlet规范文档) 处 ...

  2. spring学习七 spring和dynamic project进行整合

    spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...

  3. Spring学习(七)-----Spring Bean的5种作用域

    在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例(singleton) - 每个Spring IoC 容器返回一 ...

  4. Spring学习(七)--Spring MVC的高级技术

    一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...

  5. 从零开始学Python第七周:面向对象进阶(需修改)

    一,类的属性 (1)示例 通过属性获取已经创建对象的个数 class Plane: pCount = 0 #类属性 def __init__(self,name,category): self.nam ...

  6. spring入门(七) spring mvc+mybatis+generator

    1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...

  7. 从零开始学Kotlin第七课

    1.强制类型转换需要在后面加两个感叹号 2.如果需要在java代码调用kotlin的方法时候使用文件名+kt.方法 3.object 类名 是创建匿名内部类的写法 调用 传入class对象 4.在to ...

  8. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  9. 从零开始学spring cloud(十一) -------- hystrix监控

    一.官方文档阅读 服务启动后,可以通过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是因为spring boot版本的问题, 我在这里使用的sprin ...

随机推荐

  1. LeetCode【110. 平衡二叉树】

    对于平衡二叉树,就是左右深度相差1 就可以另外弄一个函数,计算深度,然后, 在原函数上进行比较深度是否相差1,再输出true or false. 至于迭代就可以,比较完左右节点,再比较各自的左右节点. ...

  2. 解决WCF“接收对 http://xxx.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致"

    最近在工作中新加了一个接口,本地调试的时候,直接抛出“接收对 http://xxx.svc 的 HTTP 响应时发生错误.这可能是由于服务终结点绑定未使用 HTTP 协议造成的.这还可能是由于服务器中 ...

  3. java.lang.Boolean 类源码解析

    Boolean源码比较简单. public final class Boolean implements java.io.Serializable, Comparable<Boolean> ...

  4. 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,

    解决办法:错误信息说明编译器未识别swpb汇编操作,指令集有问题.把配置命令改成: ./configure -embedded armv7 -prefix /home/thwijeth/Softwar ...

  5. linux/centos elasticsearch 环境搭建 安装 运行 使用

    环境搭建也是有些坑的存在,所以整理了一下搭建流程,安全无痛. ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例. 一.java 环境 直接apt安装火箭一 ...

  6. WebService连接winfrom简单实例

    C# 创建.部署和调用WebService的简单示例 webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互. 具体详细用法可去查询资料.下面开始创建一个简单的webservic ...

  7. python--对象实例化过程

    实例化过程: class luffy_stu: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = ...

  8. 关于Vue单页面实现微信分享的Bug

    // 问题描述在微信中分享到朋友圈或好友时,分享出去的路由被破坏,打开分享的链接,路由中的“#”会被去掉并追加?from= & Timeline= 之类的后缀参数,这就造成了分享出去的链接只能 ...

  9. JIRA日期格式设置

    https://blog.csdn.net/zj911008/article/details/48312927?utm_source=blogxgwz3 https://blog.csdn.net/z ...

  10. nginx内容

    nginx工作在7层:web server(静态内容 static contents)web reverse proxy(反向代理http,https,mail),cache(带缓存功能) proxy ...