欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

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

来源: javaer 分享于  点击 32969 次 点评:55

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动态路由内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程!

          您可能感兴趣的文章:
          • SpringCloud中的Feign远程调用接口传参失败问题
          • springcloud feign传输List的坑及解决
          • SpringCloud通过Feign传递List类型参数方式
          • springcloud feign服务之间调用,date类型转换错误的问题
          • SpringCloud Feign Jackson自定义配置方式
          • springcloud feign docker上无法通讯的问题及解决
          相关栏目:

          用户点评