SpringCloud之Feign远程接口映射的实现

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

一.简介

SpringCloud是基于Restful的远程调用框架,引入Ribbon负载均衡组件后还需要客户端使用RestTemplate调用远程接口,操作起来还显得繁琐。SpringCloud提供了远程接口映射,将远程Restful服务映射为远程接口,消费端注入远程接口即可实现方法调用。

二.流程

1.新建远程接口映射模块service-api,并引入Feign接口映射依赖

 <dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
  </dependency>
</dependencies>

2.编写接口映射接口

package com.vincent.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("SERVICE-USER")
@RequestMapping("/service-user")
public interface IUserService {
  @GetMapping("/detail")
  Object detail(@RequestParam("id") Integer id);
}

3.配置消费端application.yml

server:
 port: 9001

eureka:
 client:
  service-url:
   defaultZone: http://localhost:7001/service-eureka/eureka
  register-with-eureka: false

4.消费端添加映射模块依赖

 <dependency>
  <groupId>com.vincent</groupId>
  <artifactId>service-api</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

5.客户端注入需要使用的服务接口映射

package com.vincent.controller;

import com.vincent.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

  @Autowired
  private IUserService userService;

  @GetMapping("/detail")
  public Object detail(Integer id){
    return this.userService.detail(id);
  }
}

5.编写消费端启动类

package com.vincent;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication
@EnableFeignClients({"com.vincent.service"})
@EnableEurekaClient
public class ConsumerApp {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApp.class,args);
  }
}

@EnableFeignClients 定义Feign接口映射扫描包,IOC容器会自动创建接口实现类

6.访问 http://localhost:9001/detail?id=1

三.总结

Feign接口映射服务端Restful接口会自动依赖Ribbon组件,实现客户端负载均衡。使用接口调用消费端远程接口就像调用本地方法一样。

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

详解Spring @Autowired 注入小技巧

这篇文章主要介绍了详解Spring @Autowired 注入小技巧,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

用java实现在txt文本中写数据和读数据的方法

今天小编就为大家分享一篇用java实现在txt文本中写数据和读数据的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java 按行读取文件按行写入文件并以空格分割字符串的方法

今天小编就为大家分享一篇Java 按行读取文件按行写入文件并以空格分割字符串的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springboot注册拦截器所遇到的问题

这篇文章主要介绍了springboot注册拦截器的方法及所遇到的问题,需要的朋友可以参考下
收藏 0 赞 0 分享

Quartz实现JAVA定时任务的动态配置的方法

这篇文章主要介绍了Quartz实现JAVA定时任务的动态配置的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java实现PDF转图片的方法

这篇文章主要为大家详细介绍了java实现PDF转图片的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

浅谈Spring Boot Web 应用性能优化

这篇文章主要介绍了浅谈Spring Boot Web 应用性能优化,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java微信开发中的地图定位功能

本文通过实例代码给大家介绍了java微信开发中的地图定位功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解springboot中的jar包部署步骤

这篇文章主要介绍了springboot中的jar包部署步骤及linux中部署项目常用指令,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现文件压缩成zip的工具类

这篇文章主要为大家详细介绍了java实现文件压缩成zip的工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多