SpringCloud使用Feign实现动态路由操作

目录
  • 一、理解及原理
    • 1.1理解
    • 1.2原理
  • 二、Feign搭建实现步骤
  • 三、配置文件(pom.xml)
  • 三、程序代码
  • 四、结果演示

一、理解及原理

1.1理解

Feign
基于接口 + 注解的方式,一个http请求调用的轻量级框架

Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。

Feign是一种声明式、模板化的HTTP客户端(仅在Application Client中使用)。声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求

1.2原理

二、Feign搭建实现步骤

  • 创建Springboot基础项目
  • 在注册中心(Eureka)配置的基础上,进行配置Feign

三、配置文件(pom.xml)

基础配置:

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

整体:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.7</version>
</dependency>
<!--添加lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--pagehelper分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

<!--验证码https://blog.csdn.net/qq_41853447/article/details/105893567-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!--security权限管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

</dependencies>

三、程序代码

在启动类上加上@EnableFeignClients,开启Feign的应用

@EnableEurekaServer
@EnableSwagger2
@SpringBootApplication
@EnableFeignClients(basePackages = "com.personal.pserver")
public class PserverApplication {

public static void main(String[] args) {
SpringApplication.run(PserverApplication.class, args);
System.out.println("========================person-server已启动========================");
}

}

启动类添加完成之后,在指定需要访问的service注册使用,

见其他博主讲解:

在通过Feign来实现远程服务调用时,需要提供一个本地接口来继承服务标准工程提供的服务接口。这个本地接口不需要给予任何实现,在底层Spring容器会为这个接口提供一个基于JDK实现的代理对象,这个代理对象由Feign技术提供具体的HandlerInterceptor逻辑,实现远程的调用。实现过程类似通过代码调用LoadBalancerClient实现的Rest远程访问。
  而本地接口继承服务标准接口后,需要提供注解@FeignClient,注解的属性name代表当前接口要调用的远程服务的应用命名。

@RestController
@Api(tags = "平台基本信息管理")
@RequestMapping("/v1/pserver/platform/manager")
public class PlatformController {

@Autowired
private PPlatformService platformService;

@ApiOperation(value = "获取平台基本信息", notes = "获取平台基本信息", httpMethod = "GET")
@RequestMapping(value = "/findPlatformInfo",method = RequestMethod.GET)
public PPlatform findPlatformInfo(@RequestParam("platformId") String platformId) {
PPlatform pPlatform = platformService.findOnePlatformById(platformId);
return pPlatform;
}
}
@FeignClient(name="p-platform-service")
public interface PPlatformService {

/***
* 获取平台基本信息
* @param platformId
* @return
*/
@RequestMapping(value="/findPlatformInfo",method = RequestMethod.GET)
PPlatform findOnePlatformById(@RequestParam(value="platformId") String platformId);

}

四、结果演示

到此这篇关于SpringCloud使用Feign实现动态路由操作的文章就介绍到这了,更多相关Feign动态路由内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringCloud Feign Jackson自定义配置方式

    目录 FeignJackson自定义配置 出现的问题 解决思路 解决方法 Feign自定义配置应用 环境 自定义Feign的配置 Feign中记录日志 测试Feign自定义的配置 查看日志 Feign Jackson自定义配置 Spring Cloud Feign 默认支持Spring MVC的注解 使用相同的HttpMessageConverters类转换 官方文档说明: Spring Cloud adds support for Spring MVC annotations and for

  • springcloud feign docker上无法通讯的问题及解决

    目录 feign docker上无法通讯问题 最终解决办法 springcloud不同服务器上docker无法互通解决 feign docker上无法通讯问题 feign请求远程接口时报错: Caused by: java.net.UnknownHostException 本地环境可以跑 放到docker环境中,A容器feign调用B容器,调用不通. 最终解决办法 1.eureka 加上 2.feign客户端A的eureka配置也加上 3.调用的B容器上的服务也加上上面配置 springclou

  • SpringCloud通过Feign传递List类型参数方式

    目录 通过Feign传递List类型参数 1.单个List实体传递 2.基本类型传递 3.实体类型传递 Feign在参数为List时的坑 错误写法 正确写法 通过Feign传递List类型参数 首先明确一点,SpringCloud通过Fegin如果是多个参数,其中一个参数是List,那么是传不过去的,单个List是可以的. 1.单个List实体传递 @RequestMapping("/secret/batchInsert") public int batchInsert(@Reques

  • springcloud feign传输List的坑及解决

    目录 feign传输List的坑 错误方法1 错误方法2 错误方法3 feign调用传List接不到值 feign传输List的坑 无法直接传输List 错误方法1 @RequestMapping(value = "/stat/merchant/get_merchant_compare_info", method = RequestMethod.POST) @ResponseBody MerchantCompareTotalInfo getMerchantCompareInfo(  

  • springcloud feign服务之间调用,date类型转换错误的问题

    目录 feign服务之间调用,date类型转换错误 自定义feign请求头 通过判断是否为feign请求 OpenFeign服务间调用时日期格式异常 异常为 原因 解决方法 feign服务之间调用,date类型转换错误 最近尝试换springcloud开发,原先是springboot,每次的返回值的Date类型都通过@ControllerAdvice格式化yyyy-MM-dd HH:mm:ss然后返回的.这次用feign之后,2个服务之间调用,一直报错查了好久百度都搞不定,后面灵光一闪...不多

  • SpringCloud中的Feign远程调用接口传参失败问题

    目录 Feign远程调用接口传参失败 这是调用者 这是feign的client 这是被调者 Feign远程调用的注意点 定义的做远程调用的api接口 service微服务中的Controller的参数绑定 Feign远程调用接口传参失败 我在做一个微服务调用的时候出现了被调接口传参失败问题 Feign是通过http协议调用服务的,后来发现是因为Gep和Maping不一致,还有使用feign时要记得给实体类加无参构造注解 同时这些注解都尽量一致,不然微服务调bug很麻烦. 这是调用者 这是feig

  • SpringCloud使用Feign实现动态路由操作

    目录 一.理解及原理 1.1理解 1.2原理 二.Feign搭建实现步骤 三.配置文件(pom.xml) 三.程序代码 四.结果演示 一.理解及原理 1.1理解 Feign基于接口 + 注解的方式,一个http请求调用的轻量级框架 Feign是Netflix开发的声明式.模板化的HTTP客户端, Feign可以帮助我们更快捷.优雅地调用HTTP API. Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程

  • vue一步到位的实现动态路由

    目录 静态路由的回顾 动态路由的配置 说一些笔者遇到的问题 后记 最近在写vue项目,需要由后台传来当前用户对应权限的路由表,前端通过调接口拿到后处理(后端处理路由),就是配置vue动态路由啦. 由于错信了一些网上的文章:(,导致在这个问题上耗费了不少时间,想想,还是自己写一篇文章来记录一下那些遇到的坑吧. 接下来手把手记录一下,如何从零开始配置vue动态路由. 首先呢,先看看静态路由的配置,简单预览一下,熟悉的可以直接跳过,说这部分,是为了熟悉一下路由的配置,配置动态路由其实就是把静态路由存到

  • springcloud Zuul动态路由的实现

    前言 Zuul 是Netflix 提供的一个开源组件,致力于在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.也有很多公司使用它来作为网关的重要组成部分,碰巧今年公司的架构组决定自研一个网关产品,集动态路由,动态权限,限流配额等功能为一体,为其他部门的项目提供统一的外网调用管理,最终形成产品(这方面阿里其实已经有成熟的网关产品了,但是不太适用于个性化的配置,也没有集成权限和限流降级). 不过这里并不想介绍整个网关的架构,而是想着重于讨论其中的一个关键点,并且也是经常在交流群中听人说起的:

  • SpringCloud Zuul实现动态路由

    前言 Zuul 是在Spring Cloud Netflix平台上提供动态路由,监控,弹性,安全等边缘服务的框架,是Netflix基于jvm的路由器和服务器端负载均衡器,相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门.本文基于上篇(SpringCloud系列--Ribbon 负载均衡)实现Zuul动态路由 GitHub地址:https://github.com/Netflix/zuul 官方文档:https://cloud.spring.io/spring-cloud-

  • SpringCloud Gateway 利用 Mysql 实现动态路由的方法

    需求描述 标准网关动态路由功能是重要的一环,将路由.断言以及过滤器信息,持久化到 Mysql 中,通过配置后台页面实现路由.断言.以及过滤器等配置的增删改查. Spring Cloud Gateway 路由及黑白名单实现背景 Spring Cloud 路由API Spring Cloud Gateway 通过定义 RouteDefinitionRepository 来实现动态路由. //保存路由缓存 public interface RouteDefinitionWriter { Mono<Vo

  • vue路由结构可设一层方便动态添加路由操作

    动态添加路由基本功能 let routes=[{ path: '/login', name: 'login', component: () => import('../components/Login.vue') }] this.$router.addRoutes(routes) 涉及多层路由嵌套 如图 单纯使用addRoutes 层级结构不同 修改路由结构 例: { name:'account', path: '/account/account', meta: { title: '个人中心',

  • Nuxt的动态路由和参数校验操作

    其实动态路由就是带参数的路由.比如我们现在新闻模块下面有很多新闻详情页,这时候就需要动态路由的帮助了. 新闻详细页面 我们在news文件夹下面新建了_id.vue的文件,以下划线为前缀的Vue文件就是动态路由,然后在文件里边有$route.params.id来接收参数. /pages/news/_id.vue <template> <div> <h2>News-Content{{$route.params.id}}</h2> <ul> <l

  • SpringCloud Gateway使用redis实现动态路由的方法

    1. 将 actuator 端点暴露出来 management: endpoints: web: exposure: include: "*" 2. redis 配置 https://www.jb51.net/article/203766.htm 3. 将原内存路由持久化到 redis @Component public class RedisRouteDefinitionRepository implements RouteDefinitionRepository { /** * h

  • SpringCloud Gateway动态路由配置详解

    目录 路由 动态 路由模型实体类 动态路径配置 路由模型JSON数据 路由 gateway最主要的作用是,提供统一的入口,路由,鉴权,限流,熔断:这里的路由就是请求的转发,根据设定好的某些条件,比如断言,进行转发. 动态 动态的目的是让程序更加可以在运行的过程中兼容更多的业务场景. 涉及到两个服务,一个是门户服务(作用是提供给运营人员管理入口--包括:管理路由.绑定路由),一个是网关服务(gateway组件,为门户服务提供:查询路由信息.添加路由.删除路由.编辑路由接口). 路由模型实体类 /*

  • Spring Cloud Gateway + Nacos 实现动态路由

    本节开始介绍 SpringCloud Gateway 中动态路由的实现方法,包括: Nacos 集成动态路由配置,更新配置文件即自动更新路由 MySQL + 二级缓存实现,主要基于 Gateway 的一些特性进行重写,实现路由信息的自动更新 这篇文章主要介绍第一种方式:将配置文件放到 Nacos 进行托管,网关服务通过引入 Nacos 而自动更新路由配置信息.实现较为简单. 本节代码在:https://github.com/laolunsi/spring-boot-examples,参考例 23

随机推荐