通过Feign实现Spring Cloud微服务调用

我们在上一篇文章通过restTemplate实现Spring cloud微服务的调用中介绍了spring cloud微服务的一种调用方式,本文我们介绍另一种调用spring cloud微服务的方式——Feign。
一、什么是Feign
Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口并插入注解,就可以完成HTTP请求的参数、格式、地址等信息的声明。通过Feign代理HTTP请求,我们只需要像调用方法一样调用它就可以完成微服务请求及相关处理。
Feign整合了Ribbon和Hystrix,可以让我们不再需要显式地使用这两个组件。此外,Spring Cloud还对Feign提供了Spring MVC注解的支持,也使得我们在Web中可以使用同一个HttpMessageConverter。
总的来说,Feign具有如下特性:
·可插拔的注解支持,包括Feign注解和JAX-RS注解。
·支持可插拔的HTTP编码器和解码器。
·支持Hystrix和它的回退功能。
·支持Ribbon的负载均衡。
·支持HTTP请求和响应的压缩处理。
二、使用Feign调用Spring Cloud微服务
Eureka服务注册发现实例的基础上,这里只需再构建一个服务消费者工程。具体构建过程如下:
1新建一个spring boot工程(不再叙述),引入feign依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   <version>2.1.0.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
   <version>2.1.0.RELEASE</version>
</dependency>

2修改配置文件

#服务端口
server.port=2300
#服务名
spring.application.name=feign
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:8260/eureka/

3修改启动类
在启动类上加上@EnableDiscoveryClient、@EnableFeignClients注解

package com.yimapingchuan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class FeignApplication {

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

}

4新增一个ModuleService
应用启动时Feign就会使用动态代码机制根据我们所定义的模块服务接口生成相应的类实例,并注入到Spring的应用上下文中。在使用方式上,可以像使用普通Bean一样使用该服务。新增ModuleService接口代码如下:

package com.yimapingchuan.service;

import com.yimapingchuan.model.ModuleDO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by yuxl on 2019/3/1.
 */
//这里注意 一定要给该接口增加@FeignClient注解,wojia是被调用的服务实例名称
@FeignClient("wojia")
@Service
public interface ModuleService {
   //请求的地址一定要与wojia提供的服务的地址一致
   @RequestMapping(value = "/module/list",method = RequestMethod.GET)
   List<ModuleDO> getModuleListByCondition();
}

@FeignClient注解中的name属性值设置为被调用的微服务名称: wojia,这样Feign就可以通过Eureka服务器获取wojia微服务实例,并进行调用。而所定义的接口getModuleListByCondition()方法都是使用@RequestMapping进行注解的,这个是Spring MVC的注解。

5编写一个FeignController类,调用微服务接口

package com.yimapingchuan.controller;

import com.yimapingchuan.model.ModuleDO;
import com.yimapingchuan.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * Created by yuxl on 2019/3/1.
 */
@RestController
@RequestMapping("/module")
public class FeignController {
   @Autowired
   private ModuleService moduleService;
   @RequestMapping(value = "/list",method = RequestMethod.GET)
   public List<ModuleDO> getModuleList(){
      return moduleService.getModuleListByCondition();
   }
}

6运行与调试
依次启动上一篇文章Eureka服务注册发现实例中提到的Eureka注册中心和微服务提供者wojia,再启动本文中的消费者服务consumer,在浏览器中输入请求http://localhost:2300/module/list,返回结果如下:
在这里插入图片描述
微信扫下面二维码添加公众号议码评川,可获取java web、大数据、人工智能等相关学习资料。
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值