服务消费者Feign

2023-05-16

Feign简介

 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解

  • Feign 整合了ribbon

 pom.xml添加依赖

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

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

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

启动类添加@EnableFeignClients注解开启Feign功能

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

定义feign接口

通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下: 

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

服务消费者Feign 的相关文章

随机推荐

  • JDK8新特性:接口的静态方法和默认方法

    https blog csdn net aitangyong article details 54134385 在jdk8之前 xff0c interface之中可以定义变量和方法 xff0c 变量必须是public static fina
  • 如何使用github和git进行团队合作开发(队友和owner的仓库连接)

    折腾了两天终于搞懂了一个团队开发一个项目 xff0c 在github和git下该如何操作 xff0c 本文就简单总结一下我的过程吧 xff0c 希望对大家有帮助 1 首先确定一个项目拥有者 xff0c 即你们一个团队的项目都上传到他的git
  • springboot源码阅读一

    目录 入口 SpringApplication 部分参数初始化 SpringApplication initialize方法 SpringApplication run方法 StopWatch java awt headless系统设定 入
  • idea Maven项目找不到相关依赖包(红色波浪线)

    方案一 修改pom 配置文件 xff0c 讲标红的依赖先删除 xff0c 并点击reimport 之后重新加上出错的依赖 xff0c 再reimport 方案二 从删除本地仓库中的文件目录 xff0c 强制 maven 重新下载该包
  • 自定义FailureAnalyzer

    目录 介绍 代码示例 介绍 FailureAnalyzer是一种很好的方式在启动时拦截异常并将其转换为易读的消息 xff0c 并将其包含在FailureAnalysis中 Spring Boot为应用程序上下文相关异常 JSR 303验证等
  • 自动配置故障排除

    目录 官网文档 71 2 Troubleshoot auto configuration 翻译 71 2 自动配置故障排除 官网文档 71 2 Troubleshoot auto configuration The Spring Boot
  • 在环境或应用程序上下文启动之前对其进行自定义

    目录 官网文档 71 3 Customize the Environment or ApplicationContext before it starts 翻译 71 3 在环境或应用程序上下文启动之前对其进行自定义 官网文档 71 3 C
  • 构建ApplicationContext层次结构(添加父上下文或根上下文)

    目录 官网文档 71 4 Build an ApplicationContext hierarchy adding a parent or root context 翻译 71 4 构建ApplicationContext层次结构 xff0
  • 创建非Web应用程序

    目录 官方文档 71 5 Create a non web application 翻译 71 5 创建非Web应用程序 官方文档 71 5 Create a non web application Not all Spring appli
  • Customizing the Banner

    自定义方式 1 设置banner txt文件 默认读取根路径 xff0c 也可以通过banner location属性指定文件位置 xff0c 并且可以通过banner charset 默认是UTF 8 属性设置txt文件编码 在banne
  • isAssignableFrom方法浅析

    源码 Determines if the class or interface represented by this 64 code Class object is either the same as or is a superclas
  • @SuppressWarnings("serial")

    比如有个类实现了java io Serialize接口 xff1a package com onede4 test public class TestSerial implements java io Serializable 如果代码仅仅
  • angularJs中将字符串转换为HTML格式

    首先定义一个filter xff1a filter 39 to trusted 39 39 sce 39 function sce return function text return sce trustAsHtml text 2 htm
  • 如何在IDEA启动多个Spring Boot工程实例

    目录 只需要三步走即可 在IDEA上点击Application右边的下三角 弹出选项后 xff0c 点击Edit Configuration 打开配置后 xff0c 将默认的Single instance only 单实例 的钩去掉 通过修
  • springboot学习记录一

    概述 Spring Boot可以轻松创建独立的 xff0c 生产级的基于Spring的应用程序 xff0c 您可以 只要运行 我们对Spring平台和第三方库采取了自以为是 武断 的观点 xff0c 因此您可以最少的手忙脚乱 慌乱 开始 大
  • Java8 如何正确使用 Optional

    引用 http www importnew com 26066 html Optional是Java8提供的为了解决null安全问题的一个API 善用Optional可以使我们代码中很多繁琐 丑陋的设计变得十分优雅 这篇文章是建立在你对Op
  • git清除本地账户

    删除保存在本地的git账户 git credential manager uninstall 缓存账户 git config global credential helper wincred
  • 服务注册与发现eureka

    目录 eureka server pom xml文件添加依赖 启动类添加注解 64 EnableEurekaServer appication yml配置文件 访问界面 eureka client pom xml文件添加依赖 启动类添加注解
  • 服务消费者RestTemplate+Ribbon

    目录 简介 pom xml添加依赖 通过 64 LoadBalanced注解表明这个restRemplate开启负载均衡的功能 这样 restTemplate访问接口就可以实现负载均衡功能了 简介 spring cloud有两种服务调用方式
  • 服务消费者Feign

    Feign简介 Feign是一个声明式的伪Http客户端 xff0c 它使得写Http客户端变得更简单 使用Feign xff0c 只需要创建一个接口并注解 它具有可插拔的注解特性 xff0c 可使用Feign 注解和JAX RS注解 Fe