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)

相关推荐
copyer_xyf1 天前
Python 异常处理
前端·后端·python
llz_1121 天前
web-第三次课后作业
前端·后端·web
MageGojo1 天前
天气 API 接入实战:基于 ApiZero 实现实时天气、分钟级降水和 15 天预报查询
java·后端·spring·api 接口接入·接口实战
pe7er1 天前
软件设计不要“既要又要”
前端·后端·架构
柏舟飞流1 天前
Spring Boot 深入实践指南:从入门到工程化落地
spring boot·后端·firefox
IT_陈寒1 天前
Java Stream并行流的坑:我花了3小时才找到的线程安全问题
前端·人工智能·后端
橘子海全栈攻城狮1 天前
【最新源码】鸟博士微信小程序 023
spring boot·后端·web安全·微信小程序·小程序
Hiter_John1 天前
Golang的运算符
开发语言·后端·golang
皮皮林5511 天前
Dubbo 的 SPI 和 JDK 的 SPI 有什么区别?
后端
金銀銅鐵1 天前
用 Tkinter 实现一个罗马数字转整数的简单工具
后端·python