1. 需求描述

一般情况下,微服务内部调用都是通过注册中心,eureka,zookeeper,nacos等实现动态调用,但是对于一些外部http调用,对于不在同一微服务内,不在同一注册中心的服务调用,可以考虑SpringCloudOpenFeign,而且可以实现动态URL,通过参数动态控制。

2. 实现方案

服务端正常springboot项目,也不一定是springboot项目,只要正常提供RESTFul接口就可以,本次测试以springboot为例。主要是客户端调用。
1.服务端:


/**
 * (Category)控制层
 *
 * @author makejava
 * @since 2021-06-03 07:20:41
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;

    /**
     * 测试服务
     */
    @GetMapping("/test")
    public Response test() {
        return Response.createSuccessResponse("查询成功", "我是测试服务");
    }
}

2.客户端

maven依赖

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

客户端代码

/**
 * 测试
 * @author zrj
 * @since 2021-07-25
 */
@Api(value = "微盟鉴权测试")
@RestController
@RequestMapping("/weimob")
public class ActivityController {
    @Resource
    private CategoryService categoryService;
    @GetMapping("/test")
    @ApiOperation(value = "微盟获取Code")
    public Response test() throws URISyntaxException {
        System.out.println("-----测试-----");
        URI uri = new URI("http://localhost:8080/category/test");
        Response response = categoryService.test(uri);
        return Response.createSuccessResponse("查询成功", response);
    }
}

调用接口

/**
 * 测试接口Openfeign
 * @author zrj
 * @since 2021/7/25
 **/
@Service("WeimobAuthorize")
@FeignClient(url = "http://localhost:8080/category", name = "CategoryService")
public interface CategoryService {
    @GetMapping
    Response test(URI uri);
}

3. 细节分析

1.接口使用SpringMVC注解
接口方法的定义使用了SpringMVC的注解 @GetMapping、@RequestParam,其实SpringMVC的其他注解在此处都是支持的。(有其他文章提到也支持OpenFeign原有的注解@RequestLine、@Param等,但博主实测是不支持的,相关解析类为 feign.Contract,这个存疑)。

2.springcloud openfeign自动构建实现类
在使用方式上,OpenFeign需要手动构建代理对象,Spring Cloud OpenFeign 不同于 OpenFeign, Spring Cloud OpenFeign 帮我们自动生成了接口的代理对象(即实现类),并且注册到Spring中,我们可以很方便的使用 @Autowired 注入代理对象然后使用。其默认的代理对象是 LoadBalancerFeignClient。还有一个代理对象是 feign.Client.Default。两者区别在于:LoadBalancerFeignClient 通过服务名(下文提到)从Eureka查找相关的节点地址url,发起调用。feign.Client.Default 仅是简单的直接调用。

3.FeignClient属性name与URL一定要指定
@FeignClient(url = “http://localhost:8080/category”, name = “CategoryService”)
name 属性,是@FeignClient 注解必要的,不定义时会报错其默认指代Eureka上的服务名。
url 属性,一定要指定,这是重点了! url属性指定什么值其实不重要,因为最终都会被方法的URI参数值替换掉,它在这里另一个重要的作用,就是将接口的代理对象变成feign.Client.Default(上文提到默认是LoadBalancerFeignClient),这样就绕过了从Eureka取节点地址这一步,毕竟第三方的地址不可能注册到我们的Eureka上。(相关细节可自行debug FeignClientFactoryBean.getTarget())

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐