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出现下图:

SpringCloud使用Feign实现服务调用

- 提升使用,带参数的请求
在具体业务中的接口会比之前程序的复杂得多,这里介绍一下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;
}

启动以后访问浏览器:

SpringCloud使用Feign实现服务调用

来源:https://blog.csdn.net/Hzt_fighting_up/article/details/78248720

标签:SpringCloud,Feign
0
投稿

猜你喜欢

  • Android来电拦截的实现方法

    2023-10-09 16:42:20
  • Spring Boot Actuator自定义健康检查教程

    2022-06-12 14:54:59
  • Java SpringBoot整合SpringCloud

    2022-11-11 15:07:46
  • Java switch关键字原理及用法详解

    2023-04-16 23:29:49
  • Android可筛选的弹窗控件CustomFiltControl

    2023-01-21 09:01:46
  • SpringBoot自定义启动器Starter流程详解

    2022-08-25 08:28:19
  • JavaWeb工程中集成YMP框架快速上手

    2023-11-24 12:15:12
  • BaseJDBC和CRUDDAO的写法实例代码

    2022-09-03 14:13:33
  • springMvc注解之@ResponseBody和@RequestBody详解

    2022-10-09 17:57:19
  • 解析Spring事件发布与监听机制

    2022-09-01 09:52:19
  • Spring 注入static属性值方式

    2022-07-21 12:40:18
  • Spring集成Swagger常见错误及解决办法

    2023-07-10 05:01:17
  • Android仿支付宝支付从底部弹窗效果

    2022-04-30 10:37:13
  • Jmeter配置代理实现录制过程图解

    2022-01-15 20:25:32
  • c#爬虫爬取京东的商品信息

    2022-12-03 14:38:11
  • Maven中央仓库地址配置大全

    2022-06-14 02:42:48
  • 超炫酷的WPF实现Loading控件效果

    2023-11-19 23:53:20
  • 详解WPF中用户控件和自定义控件的使用

    2023-07-25 12:20:26
  • java实现通讯录管理系统

    2021-07-02 00:19:53
  • 学习C#静态函数及变量的一个精典例子与代码

    2021-10-08 18:53:52
  • asp之家 软件编程 m.aspxhome.com