SpringCloud Feign 配置(基于Consul)

一.基础配置

  1.引入依赖

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

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

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

   2.创建主类,通过 @EnableFeginClients 注解开启 Feign 功能

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

   3.定义AService接口,通过 @FeignClient 注解指定服务名来绑定服务, 然后使用SpringMVC 的注解来绑定具体该服务提供的 REST 接口

@FeignClient("aservice")  //这里的服务名不区分大小写
public interface AService {
    @PostMapping("/hello")
    String hello();
}

    需要调用 AService 时,在类中使用 @Autowired 注解直接注入 AService 实例, 并调用 /hello 接口

@RestController
public class ConsumerController {

    @Autowired
    private AService aService;

    @RequestMapping("/test")
    public String test(){
        return aService.hello();
    }
}

二.参数绑定

@FeignClient("aservice") 
public interface AService {  
  @RequestMapping("/hello1")
   String hello1(@RequestParam("hello1") String hello1); 
  @RequestMapping("/hello2")   String hello2(@RequestHeader("hello2") String hello2)
  @RequestMapping("/hello3")   String hello2(@RequestBody User user)}

   

@RestController
public class BController{  
  @RequestMapping(value = "/hello1", method = RequestMethod.GET)
   String hello1(@RequestParam String hello1){    return "hello";  }
  @RequestMapping(value = "/hello2", method = RequestMethod.GET)     String hello2(@RequestHeader String hello2){    return "hello";  }     @RequestMapping(value = "/hello3", method = RequestMethod.POST)     String hello3(@RequestBody User user){    return "hello";  } }

三.Ribbon 配置

  由于 Feign 的客户端负载均衡是通过 Ribbon 实现的, 所以可以通过配置 Ribbon 客户端的方式来自定义各个服务客户端调用的参数.

  1.全局配置

    全局配置直接使用 ribbon.<key>=<value>的方式来设置 ribbon 的各项默认参数. 比如, 修改默认的客户端调用超时时间:  

ribbon.ReadTimeout=5000
ribbon.ConnectTimeout=500

   2.指定服务配置

     大多数情况下, 服务调用的超时时间会根据实际服务的特性做一些调整, 所以需要指定服务配置

     指定服务配置根据 <client>.ribbon.key=value 的格式进行配置

aservice.ribbon.ReadTimeout=2000
aservice.ribbon.ConnectTimeout=500

    3.重试机制

ribbon.MaxAutoRetries=1
ribbon.MaxAutoRetriesNextServer=2

     MaxAutoRetries 设置为1, 所以重试策略先尝试访问首选实例一次,失败后才会更换实例访问,而更换实例访问的次数通过 MaxAutoRetriesNextServer 参数设置为2, 所以会尝试更换两次实例进行重试.

原文地址:https://www.cnblogs.com/xiaoshouxing/p/9573887.html

时间: 2024-07-29 22:37:58

SpringCloud Feign 配置(基于Consul)的相关文章

SpringCloud Config 配置(基于Consul)

一,构建配置中心 1.在pom.xml文件中添加相关依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springf

基于consul的服务注册(含踩坑过程)

在本地的一个案例中进行修改 修改微服务的相关pom文件修改每个微服务的pom文件,添加SpringCloud提供的基于Consul的依赖 <!--SpringCloud提供的基于Consul的服务发现--> <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-starter-consul-discovery&l

基于 Consul 实现 MagicOnion(GRpc) 服务注册与发现

0.简介 0.1 什么是 Consul Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置. 这里所谓的服务,不仅仅包括常用的 Api 这些服务,也包括软件开发过程当中所需要的诸如 Rpc.Redis.Mysql 等需要调用的资源. 简而言之 Consul 就是根据 Key/Value 存储了一套所有服务的 IP/Port 集合,当你 Grpc 客户端需要请求某种服务的时候,具体的 IP 与端口不需要你自己来进行指定,而是通过与 Consul Agent 通信

032:基于Consul和MGR的MySQL高可用架构

目录 一.Consul 1.Consul简介 2.准备环境 3.Consul 安装 4.Consul配置文件 5.Consul 服务检查脚本 6.Consul启动 二.MGR搭建 1.MGR配置 2.MGR查看 三 .Consul测试 1.MGR(多主模式)+ Consul模式 1.1 .Consul UI界面 1.2.Consul 检查DNS解析 1.3.切换测试 2.MGR(单主模式)+ Consul模式 + PorxySQL 2.1.PorxySQL配置 2.2 .查看页面 2.3.检查D

SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法(2)

书接上文. 上文中描述了如何在 SpringCloud+Feign环境下上传文件与form-data同时存在的解决办法,实践证明基本可行,但却会引入其他问题. 主要导致的后果是: 1. 无法与普通Feign方法并存 2. 几率性(不确定条件下)导致其他form-data类型参数无法识别,无法正常工作,错误信息大致如下: org.springframework.web.multipart.support.MissingServletRequestPartException: Required re

个推基于Consul的配置管理

作者:个推应用平台基础架构高级研发工程师 阿飞在微服务架构体系中,由于微服务众多,服务之间又有互相调用关系,因此,一个通用的分布式配置管理是必不可少的.一般来说,配置管理需要解决配置集中管理.在系统运行期间可实现动态配置.配置修改后支持自动刷新等问题.在大多数微服务体系中,都会有一个名为配置文件的功能模块来提供统一的分布式配置管理.构建配置中心,统一对应用中各个微服务进行管理,对微服务体系的意义重大. Consul为什么适合做配置管理 Consul作为轻量级的分布式K/V存储系统,搭建方便,可用

SpringCloud Feign 之 Fallback初体验

SpringCloud Feign 之 Fallback初体验 在微服务框架SpringCloud中,Feign是其中非常重要且常用的组件.Feign是声明式,模板化的HTTP客户端,可以帮助我们更方便快捷调用HTTP API.本文主要针对Feign的熔断机制Fallback进行简单介绍.Fallback主要是用来解决依赖的服务不可用或者调用服务失败或超时,使用默认的返回值. 1.引入Feign pom依赖包 <dependency> <groupId>org.springfram

SpringCloud Feign 之 超时重试次数探究

SpringCloud Feign 之 超时重试次数探究 上篇文章,我们对Feign的fallback有一个初步的体验,在这里我们回顾一下,Fallback主要是用来解决依赖的服务不可用或者调用服务失败或超时,使用默认的返回值.实际应用中, 在Fallback之前,需要对服务配置重试机制,当多次重试服务,还是服务不可用的情况下,就触发Fallback. 这里,我们对重试机制配置以及重试次数进行一次探究. Feign的超时 Feign接口调用分两层,Ribbon(负载均衡)和Hystrix(熔断器

利用Openfiler配置基于文件系统的网络存储

一.Openfiler简介 Openfiler是一个操作系统,其提供基于文件的网络附加存储和基于块的存储区域网络功能. Openfiler支持的网络协议包括:NFS,SMB/CIFS,HTTP/WebDAV,FTP和iSCSI. Openfiler支持的网络目录包括:NIS,LDAP(支持SMB/CIFS密码加密),Active Directory(本地和混合模式),基于Windows NT的域控制器和Hesiod.认证协议包括Kerberos 5. Openfiler支持基于卷的分区技术:如本