SpringCloud Feign的使用代码实例

1.官方文档

https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/

2.添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

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

3.添加启动类注解

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
//@MapperScan("cn.ytheng.order_service")
@EnableFeignClients
public class OrderServiceApplication {

  public static void main(String[] args) {

    SpringApplication.run(OrderServiceApplication.class, args);
  }

}

4.添加Feign接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * 商品服务客户端
 * product-service: 调用服务名称,即spring.application.name
 *
 */
@FeignClient(name = "product-service")
public interface ProductClient {

  @GetMapping("/api/v1/product/find")
  String getById(@RequestParam("id") int id);

}

5.添加Controller

import cn.theng.order_service.service.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/order")
public class ProductOrderController {

  @Autowired
  private ProductClient productClient;

  @PostMapping("/test2")
  public Object test2(@RequestParam("product_id") int productId) {

    String product = productClient.getById(productId);

    return "success";
  }
}

6.添加application.yml配置

server:
 port: 8781
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

spring:
 application:
  name: order-service

#设置调用服务超时时间
#product-service为服务名称,也可以设置为默认值default
feign:
 client:
  config:
   product-service:
    connectTimeout: 5000
    readTimeout: 11000

7.访问地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • SpringCloud Feign参数问题及解决方法

    这篇文章主要介绍了SpringCloud Feign参数问题及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 今天遇到使用Feign调用微服务,传递参数时遇到几个问题 1.无参数 以GET方式请求 服务提供者 @RequestMapping("/hello") public String Hello(){ return "hello,provider"; } 服务消费者 @GetMapping("

  • Spring Cloud Feign组成配置过程解析

    Feign的组成 接口 作用 默认值 Feign.Builder Feign的入口 Feign.Builder Client Feign底层用什么去请求 和Ribbon配合时:LoadBalancerFeignClient 不和Ribbon配合时:Fgien.Client.Default Contract 契约,注解支持 SpringMVCContract Encoder 解码器,用于将独享转换成HTTP请求消息体 SpringEncoder Decoder 编码器,将相应消息体转成对象 Res

  • Spring Cloud Feign性能优化代码实例

    1.替换 tomcat 首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍第一步,pom 修改去除tomcat <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <ex

  • SpringCloud项目集成Feign、Hystrix过程解析

    这篇文章主要介绍了SpringCloud项目集成Feign.Hystrix过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Feign的功能:这是个消费者,根据服务注册在Eureka的ID去找到该服务,并调用接口 Hystrix的功能:熔断器,假如A服务需要调用B服务的/cities接口获取数据,那就在A服务的controller里声明@HystrixCommand,如果B服务的/cities接口挂了,就返回一个自定义的值 项目结构 [r

  • Spring Cloud Feign高级应用实例详解

    这篇文章主要介绍了Spring Cloud Feign高级应用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.使用feign进行服务间的调用 Spring boot2X Consul如何使用Feign实现服务调用 2.开启gzip压缩 Feign支持对请求与响应的压缩,以提高通信效率,需要在服务消费者配置文件开启压缩支持和压缩文件的类型 添加配置 feign.compression.request.enabled=true feig

  • 如何使用Spring Cloud Feign日志查看请求响应

    这篇文章主要介绍了如何使用Spring Cloud Feign日志查看请求响应,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在使用微服务时,常常会用feign做客户端去调用别的微服务,但是在日志中很难查看到具体的请求和响应.因此,需要把feign默认的日志打开. 日志设置创建feign配置类 @Configuration public class FeignLogConfiguration { @Bean Logger.Level feign

  • Spring Cloud Feign报错问题解决

    这篇文章主要介绍了Spring Cloud Feign报错问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 如果我们使用Spring Cloud的Feign实现熔断,首先需要自定义一个熔断类,实现你的feign接口,然后实现方法,这些方法就是熔断方法,最后需要在你的feign接口中指定fallback为自定义类 但是启动过程中却出现了 org.springframework.beans.factory.BeanCreationExcept

  • SpringCloud Feign的使用代码实例

    1.官方文档 https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/ 2.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dep

  • SpringCloud Zuul自定义filter代码实例

    实现过滤器很简单,只需要继承ZuulFilter,并实现ZuulFilter中的抽象方法. filterType():定义过滤器的类型,它有4种类型,分别是pre.post.routing和error filterOrder():过滤顺序,它是一个Int类型的值,值越小,越早执行该过滤器 shouldFilter():表示该过滤器是否过滤逻辑,如果为true,则执行run方法,如果为false,则不执行run方法 Object run():写具体的过滤逻辑 注意重要说明,有些版本在转发post时

  • Springcloud RestTemplate服务调用代码实例

    1.服务productservices @RestController public class ProductController { @RequestMapping("/product/findAll") public Map findAll(){ Map map = new HashMap(); map.put("111","苹果手机"); map.put("222","苹果笔记本"); return

  • SpringCloud Ribbon负载均衡代码实例

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g

  • SpringCloud Gateway跨域配置代码实例

    这篇文章主要介绍了SpringCloud Gateway跨域配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Springboot版本:2.1.8.RELEASE SpringCloud版本:Greenwich.SR2 yml配置: spring: cloud: gateway: globalcors: cors-configurations: '[/**]': # 允许携带认证信息 # 允许跨域的源(网站域名/ip),设置*为全部

  • SpringCloud Feign请求头删除修改的操作代码

    Feign请求头修改删除操作 @Configuration public class ClientConfiguration { @Bean public RequestInterceptor headerInterceptor() { return new RequestInterceptor() { @Override public void apply(RequestTemplate template) { HttpServletRequest httpServletRequest = (

  • Java之Springcloud Feign组件详解

    一.Feign是什么? OpenFeign是Spring Cloud提供的一个声明式的伪Hltp客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可,Nacos很好的兼容了OpenFeign,OpenFeign默认集成了Ribbon, 所以在Nacos下使用OpenFeign默认就实现了负载均衡的效果. 二.使用步骤 1.消费方导入依赖 ···c org.springframework.cloud spring-cloud-starter-openfeign

  • SpringCloud Feign配置应用详细介绍

    目录 前言 1.Feign简介 2.Feign配置应用 前言 服务消费者调用服务提供者的时候使用RestTemplate技术 存在不便之处: 拼接url restTmplate.getForObJect 这两处代码都比较模板化,能不能不让我我们来写这种模板化的东西,另外来说,拼接url非常的low,拼接字符串,拼接参数,很low还容易出错 1.Feign简介 Feign是Netflix开发的一个轻量级RESTful的HTTP服务客户端(用它来发起请求,远程调用的),是以Java接口注解的方式调用

随机推荐