zoukankan      html  css  js  c++  java
  • Feign整合测试

    1、测试使用

    (1)服务调用方引入依赖

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.netflix.feign/feign-okhttp -->
            <dependency>
                <groupId>com.netflix.feign</groupId>
                <artifactId>feign-okhttp</artifactId>
            </dependency>

     (2)服务调用方启动类添加注解

        @EnableFeignClients

    (3)定义FeiClient接口

    在服务调用方创建client包,专门用于定义FeiClient接口

    @FeignClient("user-service-id")
    public interface UserClient {
        @GetMapping("/user/get/{id}")
        public User findById(@PathVariable("id") String id);
    }

     (3)测试

    启动注册中心Eureka、服务提供方工程

         @Autowired
        private UserClient userClient;
        
        @Test
        public void testFeign(){
            User user= userClient.findById("5a754adf6abb500ad05688d9");
            System.out.println("testFeign:::::::::::::::::::"+JSONObject.toJSONString(user));
        }

    2、测试分析

    (1)Feign 是netflix 开源的一个rest 客户端,在这里替代了前面的RestTemplate + okhttp 

    (2)Feign同样集成了Rebbion,实现了客户端负载均衡,ribbon充当了一个负载均衡器

    (3)使用体现: 在本地定义远程接口、实现了像调用本地方法一样进行远程调用

    3、工作原理:

    (1)在启动类上添加@EnableFeignClients 注解、spring 会扫描@FeignClient注解的接口、并生成其代理对象

    (2)FeignClient 的value属性指定了服务提供方服务名称、Feign会从Eureka上获取服务列表、通过负载均衡算法进行服务调用(Rebbion负载均衡器工作原理)

    (3)springClould 对Feign进行了增强、使其兼容了SpringMvc注解,在接口方法上可以通过@GetMapping等注解进行url远程调用,但是注意两点

    ①参数必须使用@pathVariable、@RequestParam注解声明、不能省略;

    ②返回值是对象类型、无参构造不能省略

  • 相关阅读:
    Java&Go三种HTTP服务端端性能测试
    利用闭包实现自定义等待方法
    LevelDB在测试中应用应用
    利用Java反射处理private变量
    FunTester2021年总结
    推倒重来的觉悟
    SpringMVC项目依赖和静态资源导出
    DES算法详解
    5G AKA协议详解
    RSA算法加解密证明过程
  • 原文地址:https://www.cnblogs.com/dehigher/p/10136988.html
Copyright © 2011-2022 走看看