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

网友投稿 756 2022-10-01

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

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)

基础配置:

org.springfcEQzHQTmNramework.cloud

spring-cloud-starter-openfeign

整体:

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-data-jpa

RELEASE

compile

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.freemarker

freemarker

2.3.30

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-devtools

runtime

io.springfox

springfox-swagger-ui

2.4.0

io.springfox

springfox-swagger2

2.4.0

com.github.xiaoymin

swagger-bootstrap-ui

1.8.7

org.projectlombok

lombok

RELEASE

compile

com.google.guava

guava

20.0

com.google.guava

guava

19.0

compile

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

org.springframework.boot

spring-boot-starter-actuator

com.github.pagehelper</groupId>

pagehelper-spring-boot-starter

1.2.11

commons-codec

commons-codec

1.11

commons-io

commons-io

2.6

com.google.code.gson

gson

2.8.6

com.github.whvcse

easy-captcha

1.6.2

org.springframework.boot

spring-boot-starter-security

org.springframework.cloud

spring-cloud-starter-openfeign

四、程序代码

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

@EnableEurekaServer

@EnableSwagger2

@SpringBootApplication

@EnableFeignClients(basePackages = "com.personal.pserver")

public class PserverApplication {

public static void main(cEQzHQTmNString[] 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 platfhttp://ormId

* @return

*/

@RequestMapping(value="/findPlatformInfo",method = RequestMethod.GET)

PPlatform findOnePlatformById(@RequestParam(value="platformId") String platformId);

}

五、结果演示

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Yolo 9000:OSError: libdarknet.so: cannot open shared object file: No such file or directory
下一篇:公众号预览多长时间失效?(微信公众号预览多久失效)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~