SpringCloud使用Feign实现服务调用

所属分类: 软件编程 / java 阅读数: 42
收藏 0 赞 0 分享

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;
 }

启动以后访问浏览器:

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

更多精彩内容其他人还在看

java实现背单词程序

这篇文章主要为大家详细介绍了java实现背单词程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java实现单词查询小程序

这篇文章主要为大家详细介绍了java实现单词查询小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java程序开发环境配置图文教程

这篇文章主要为大家详细介绍了Java程序开发环境配置图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解ssh框架原理及流程

在本文中小编给大家整理的是关于ssh框架原理及流程的相关知识点内容,有此需要的朋友们可以学习下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解springmvc常用5种注解

在本篇里我们给大家总结了关于springmvc常用5种注解相关知识点以及实例代码,需要的朋友们参考下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作(2)

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot假死诊断实战记录

这篇文章主要给大家介绍了关于Spring Boot假死诊断的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java计时新姿势StopWatch详解

这篇文章主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现点击按钮弹出新窗体功能

这篇文章主要为大家详细介绍了java实现点击按钮弹出新窗体功能,旧窗体不进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多