Forest调用注册到nacos服务中心的服务

背景:forest在开发的时候已经使用了很久,但是好像目前还不支持调用注册到服务中心的服务。为了让不行变成行,特意写了这篇博客。 做法:第一步,自定义注解NacosService.

java 复制代码
import com.dtflys.forest.annotation.MethodLifeCycle;
import com.dtflys.forest.annotation.RequestAttributes;

import java.lang.annotation.*;

@Documented
/** 重点: @MethodLifeCycle注解指定该注解的生命周期类*/
@MethodLifeCycle(ServiceNameCycle.class)
// 加上 @@RequestAttributes 以标识该注解的属性可以被解析
@RequestAttributes
@Retention(RetentionPolicy.RUNTIME)
/** 指定该注解可用于类上或方法上 */
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface NacosService {
    /**
     * 自定义注解的属性:服务名
     * 所有自定注解的属性可以在生命周期类中被获取到
     */
    String serviceName();
}

定义ServiceNameCycle来解析自定义的注解

typescript 复制代码
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle;
import com.dtflys.forest.reflection.ForestMethod;

public class ServiceNameCycle implements MethodAnnotationLifeCycle<NacosService, Object> {


    /**
     * 当方法调用时调用此方法,此时还没有执行请求发送
     * 此方法可以获得请求对应的方法调用信息,以及动态传入的方法调用参数列表
     */
    @Override
    public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
        Object serviceName = getAttribute(request, "serviceName");
        request.setHost(serviceName.toString());
    }

    /**
     * 发送请求前执行此方法,同拦截器中的一样
     */
    @Override
    public boolean beforeExecute(ForestRequest request) {
        return true;
    }

    /**
     * 此方法在请求方法初始化的时候被调用
     */
    @Override
    public void onMethodInitialized(ForestMethod method, NacosService annotation) {
    }
}

第三步,需要在拦截器里面解析这个。这里插播一条小知识:通过serviceName获取对应的链接信息。

java 复制代码
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URI;
import java.util.Objects;

@Slf4j
@Component
public class ForestInterceptor<T> implements Interceptor<T> {

    @Resource
    private LoadBalancerClient loadBalancerClient;
    /**
     * 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
     * @Param request Forest请求对象
     */
    @Override
    public boolean beforeExecute(ForestRequest req) {
        log.info("invoke Simple beforeExecute");
        URI newUri = getUriByServiceName(req.getHost());
        if(Objects.nonNull(newUri)){
            req.setHost(newUri.getAuthority());
        }
        return true;  // 继续执行请求返回true
    }

    private URI getUriByServiceName(String serviceName){
        ServiceInstance choose = loadBalancerClient.choose(serviceName);
        if(Objects.nonNull(choose)){
            return choose.getUri();
        }
        return null;
    }
}

最后一步记得在配置文件加上一行配置

yaml 复制代码
forest:
  interceptors:
    - com.szah.flow.interceptor.ForestInterceptor

接下来展示怎么调用

less 复制代码
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.annotation.Post;
import com.dtflys.forest.annotation.Query;

@NacosService(serviceName = "smarthub-rest")
public interface Restful {

    @Post("/event/getSatgeMatters")
    JSONObject getSatgeMatters(@Query("situationResultId") String situationResultId, @Query("eventId")String eventId);
}

通过上述几步,就可以实现forest调用注册到nacos的服务。 参考资料:🥪 拦截器 | Forest (dtflyx.com)

相关推荐
一 乐6 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
码事漫谈7 小时前
Protocol Buffers 编码原理深度解析
后端
码事漫谈7 小时前
gRPC源码剖析:高性能RPC的实现原理与工程实践
后端
踏浪无痕9 小时前
AI 时代架构师如何有效成长?
人工智能·后端·架构
程序员小假9 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端
武子康10 小时前
大数据-209 深度理解逻辑回归(Logistic Regression)与梯度下降优化算法
大数据·后端·机器学习
maozexijr10 小时前
Rabbit MQ中@Exchange(durable = “true“) 和 @Queue(durable = “true“) 有什么区别
开发语言·后端·ruby
源码获取_wx:Fegn089510 小时前
基于 vue智慧养老院系统
开发语言·前端·javascript·vue.js·spring boot·后端·课程设计
独断万古他化11 小时前
【Spring 核心: IoC&DI】从原理到注解使用、注入方式全攻略
java·后端·spring·java-ee
毕设源码_郑学姐11 小时前
计算机毕业设计springboot基于HTML5的酒店预订管理系统 基于Spring Boot框架的HTML5酒店预订管理平台设计与实现 HTML5与Spring Boot技术驱动的酒店预订管理系统开
spring boot·后端·课程设计