iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringCloud使用Feign实现动态路由操作
  • 946
分享到

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

2024-04-02 19:04:59 946人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、理解及原理1.1理解1.2原理二、Feign搭建实现步骤三、配置文件(pom.xml)四、程序代码五、结果演示一、理解及原理 1.1理解 Feign基于接口 + 注解的方式,

一、理解及原理

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 {


@RequestMapping(value="/findPlatformInfo",method = RequestMethod.GET)
PPlatform findOnePlatformById(@RequestParam(value="platformId") String platformId);

}

五、结果演示

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

--结束END--

本文标题: SpringCloud使用Feign实现动态路由操作

本文链接: https://www.lsjlt.com/news/150509.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作