SpringCloud使用Feign实现服务调用
作者:小气鬼Sweet 发布时间:2021-11-10 05:19:20
Spring Cloud Feign简介
Spring Cloud Feign也是一个基础工具类,它整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两者的强大功能以外,它还提供了一种声明式的Web服务客户端定义方式。使用它可以进行服务的消费,但是它的客户端负载平衡仍是通过Ribbon实现的
使用Spring Cloud Feign
创建一个SpringBoot工程,作为服务调用方
1.pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
spring-cloud-starter-feign加入了feign的依赖
2.启动类
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
@EnableFeignClients:开启Spring Cloud Feign的支持功能
3.服务层
@FeignClient("hello-service")
public interface HelloService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
}
@FeignClient(“hello-service”):制定服务名来绑定服务
注:服务名不区分大小写
@RequestMapping:指定调用服务中的具体方法
4.Controller层
@Controller
public class ConsumerController {
@Autowired
private HelloService helloService;
@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
@ResponseBody
public String helloConsumer() {
return helloService.hello();
}
}
在方法中调用这个绑定了hello-service服务接口的客户端向该服务发起/hello接口的调用
5.配置类
server.port=9001
spring.application.name=feign-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
启动程序以后,在浏览器中输入http://localhost:9001/feign-consumer出现下图:
- 提升使用,带参数的请求
在具体业务中的接口会比之前程序的复杂得多,这里介绍一下Feign对集中不同形式参数的绑定方法。有@RequestParam、@RequestHeader、@RequestBody
1.改造服务提供类的Service层,添加几个方法
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
@ResponseBody
public String hello(@RequestParam String name) {
return "hello " + name;
}
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
@ResponseBody
public User hello(@RequestHeader String name, @RequestHeader Integer age) {
return new User(name, age);
}
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
@ResponseBody
public String hello(@RequestBody User user) {
return "Hello " + user.getName() + ", " + user.getAge();
}
2.添加一个javabean
public class User {
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.修改服务调用类的接口
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name);
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
这里在定义各参数绑定时@RequestParam、@RequestHeader等可以指定参数名称的注解,但它们的value不能少,否则会报错,这和SpringMVC中有一点不同
4.在服务调用类Controller层添加一个测试的接口
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
@ResponseBody
public String helloConsumer2() {
String s2 = helloService.hello("dayday");
return s2;
}
启动以后访问浏览器:
来源:https://blog.csdn.net/Hzt_fighting_up/article/details/78248720


猜你喜欢
- 虽然项目中都夹杂了Hibernate的支持,但是团队开发中,很多人为了编写特殊查询的代码时都使用了JDBC进行查询。JDBC查询后返回的是一
- 1.摘要Android手机间通过蓝牙方式进行通信,有两种常见的方式,一种是socket方式,另一种是通过Gatt Server(Androi
- 结论先行Kotlin协程中的Channel用于处理多个数据组合的流,随用随取,时刻准备着,就像自来水一样,打开开关就有水了。Channel使
- 在 .NET 5.0 的发布中,不仅统一了框架,微软还在C#9.0中推出了一些新特性。本版本中,印象深刻的功能:Init-only sett
- 在 Kotlin 中,reduce() 和 fold() 是函数式编程中常用的高阶函数。它们都是对集合中的元素进行聚合操作的函数,将一个集合
- 一、坐标分类地图坐标大致分为几种: 1、GPS、WGS84,也就是原始坐标体系,
- 适用场合: 7.3工厂模式的适用场合 创建新对象最简单的办法是使用new关键字和具体类。只有在某些场合下,创建和维护对象工厂所带来的额外复杂
- 看书的时候被一段代码能凌乱啦,代码是这样的:package 继承;abstract class People {
- 实时代码模板(Live Templates)我们先来看一个gif图:大兄弟,你看清我的操作了么?这个就是实时代码模板的功能。我们来看一下怎么
- 引出泛型我们通过如下的示例,引出为什么泛型的概念。public class Test {public static void main(St
- 一、代码实例实现功能将Array转换为List将List转换为Array将Array转换为Dictionary将Dictionary转换为A
- 本文实例讲述了java基于JDBC连接Oracle 11g Release2的方法。分享给大家供大家参考。具体如下:Oracle 11g R
- BigDecimal 类一、 概述Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运
- 一、定义实体类Person,封装生成的数据package net.dc.test;public class Person { private
- 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而
- 一、简介:开发中在用户注册或找回密码之类的功能,经常会遇到获取短信验证码,获取验证码后需要等待1分钟倒计时,这段时间是不能再次发送短信请求的
- 开发一款性能优良的应用是每一个Android开发者都必须经历的挑战。在移动端资源有限的前提下,提高应用的性能显得尤为重要。常见的提高APP性
- 本文实例为大家分享了SpringBoot Http远程调用的具体代码,供大家参考,具体内容如下一、在实现远程调用时可以使用feign与htt
- MainActivity如下: package cn.testgethandsetinfo; import android.os.Bundl
- 本文实例讲述了C#将DataTable转换成list及数据分页的方法。分享给大家供大家参考。具体如下:/// <summary>