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
猜你喜欢
- 首先需要建立两个库进行测试,我这里使用的是master_test和slave_test两个库,两张库都有一张同样的表(偷懒,喜喜),表结构表
- 我在5月份的时候就申请了洞态IAST企业版内测,算是比较早的一批用户了。聊聊几个我比较在意的问题,比如API接口覆盖率、第三方开源组件检测以
- 前言C# 语言一贯秉承简洁优美的宗旨,每次升级都会带来一些语法糖,让我们可以使代码变得更简洁。本文分享两个使用 C# 9.0 提升 if 语
- 本文实例讲述了java数据结构与算法之noDups去除重复项算法。分享给大家供大家参考,具体如下:public static void no
- 查看和修改线程优先级1.题目JAVA中每个线程都有优化级属性,默认情况下,新建的线程和创建该线程的线程优先级是一样的。当线程调度器选择要运行
- 前言目前Flutter三大主流状态管理框架分别是provider、flutter_bloc、getx,三大状态管理框架各有优劣,本篇文章将介
- 1、SpringSecurity 本质是一个过滤器链SpringSecurity 采用的是责任链的设计模式,它有一条很长的过滤器链。现在对这
- 在 APK 开发中,通过 Java 代码来打开系统的安装程序以安装 APK 并不是什么难事,一般的 Android 系统都有开放这一功能。但
- spring中实例化bean无效在做Struts2和Spring整合时遇到Spring实例化无效的情况,Action中代码如下public
- 1. 基本数据类型(以int为例,其他类似):Controller代码:@RequestMapping("saysth.do&qu
- 结构:安装NuGet包:using SAP.Middleware.Connector;using System.Data;namespace
- 在上面的例子中多次使用到了Thread类的join方法。我想大家可能已经猜出来join方法的功能是什么了。对,join方法的功能就是使异步执
- 动态SQL简介动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQ
- 前端使用的是vue+elementui,这款系统只适合学习巩固SpringBoot+VUE,后面还要在这上面加校园公告、校园零食等功能,后期
- 本文实例讲述了C#画笔Pen绘制光滑模式曲线的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syste
- System中的out,error都是final类型的,不能做改动。但通过setOut()可以设置新的输出流,从而实现写日志的功能。impo
- 单点登录三种方式单点登录的三种实现方式:分别为session广播机制;cookie+redis;tokensession广播机制指在一个集群
- 一、首先我们要获取Logcat中的日志如何获取呢?首先我们要先定义一个String[]数组,里面的代码是//第一个是Logcat ,也就是我
- 问题是这样的,我用eclipse发送httpclient请求如下没有问题,但是在idea中就返回400,为毛呢???excuse me?pa
- 本文实例讲述了Winform中GridView分组排序功能实现方法。分享给大家供大家参考。具体实现方法如下:一、问题:由于客户最近要扩充公司