kwsc98 / fusen-rs

Fusen-rs is a lightweight, high-performance microservice framework based on Tokio's asynchronous threads, compatible with Dubbo and SpringCloud, supporting service registration and discovery through protocol support, and can implement WebService by exposing HTTP interfaces.
Apache License 2.0
220 stars 12 forks source link

支持AOP环绕式通知的多层嵌套 #25

Open kwsc98 opened 1 month ago

kwsc98 commented 1 month ago

之前我们实现的环绕式通知组件,只支持单层处理,如果你既需要做链路追踪,还需要对请求进行加签验签,和统一异常捕获等多种需求时,只能写在一个Aspect组件中,导致灵活性很差,此次更新我们支持了多Aspect的组合和排序,可以根据配置灵活的组装多种自定义组件。 示例

//时间组件
#[handler(id = "TimeLogAspect")]
impl Aspect for TimeLogAspect {
    async fn aroud(
        &self,
        join_point: ProceedingJoinPoint,
    ) -> Result<fusen_common::FusenContext, fusen_rs::Error> {
        let start_time = get_now_date_time_as_millis();
        info!("开始处理时间 : {:?}", get_now_date_time_as_millis());
        let context = join_point.proceed().await;
        let end_time = get_now_date_time_as_millis();
        info!(
            "结束处理时间 : {:?} , 处理耗时 : {}",
            end_time,
            end_time - start_time
        );
        context
    }
}
//服务状态组件
#[handler(id = "StateLogAspect")]
impl Aspect for StateLogAspect {
    async fn aroud(
        &self,
        join_point: ProceedingJoinPoint,
    ) -> Result<fusen_common::FusenContext, fusen_rs::Error> {
        info!("服务开始处理");
        let context = join_point.proceed().await;
        info!("服务处理结束");
        context
    }
}

我们定义了两种不同的Aspect组件,我们可以根据配置进行组装

handler_infos:
  - id: org.apache.dubbo.springboot.demo.DemoService
    handlers_id:
      - TimeLogAspect
      - StateLogAspect

可以看到先进行了时间组件处理,然后进行了服务状态组件处理

2024-10-12T10:09:23.971444Z  INFO ThreadId(16) server: 37: 开始处理时间 : 1728727763971
2024-10-12T10:09:23.971492Z  INFO ThreadId(16) server: 21: 服务开始处理
2024-10-12T10:09:23.971668Z  INFO ThreadId(16) sayHelloV2-http: server: 63: 开始处理 sayHelloV2-http
2024-10-12T10:09:23.971689Z  INFO ThreadId(16) sayHelloV2-http: server: 64: 接收消息 : ReqDto { str: "world" }
2024-10-12T10:09:23.971714Z  INFO ThreadId(16) sayHelloV2-http:get_user_info_by_db: server: 66: get_user_info_by_db : selet * from user where id = $1
2024-10-12T10:09:23.971780Z  INFO ThreadId(16) server: 23: 服务处理结束
2024-10-12T10:09:23.971789Z  INFO ThreadId(16) server: 40: 结束处理时间 : 1728727763971 , 处理耗时 : 0

然后我们还可以修改配置

handler_infos:
  - id: org.apache.dubbo.springboot.demo.DemoService
    handlers_id:
      - StateLogAspect
      - TimeLogAspect

可以看到先进行了服务状态组件处理,然后进行了时间组件处理

2024-10-12T10:19:54.255463Z  INFO ThreadId(16) server: 21: 服务开始处理
2024-10-12T10:19:54.255534Z  INFO ThreadId(16) server: 37: 开始处理时间 : 1728728394255
2024-10-12T10:19:54.255731Z  INFO ThreadId(16) sayHelloV2-http: server: 63: 开始处理 sayHelloV2-http
2024-10-12T10:19:54.255754Z  INFO ThreadId(16) sayHelloV2-http: server: 64: 接收消息 : ReqDto { str: "world" }
2024-10-12T10:19:54.255781Z  INFO ThreadId(16) sayHelloV2-http:get_user_info_by_db: server: 66: get_user_info_by_db : selet * from user where id = $1
2024-10-12T10:19:54.255859Z  INFO ThreadId(16) server: 40: 结束处理时间 : 1728728394255 , 处理耗时 : 0
2024-10-12T10:19:54.255869Z  INFO ThreadId(16) server: 23: 服务处理结束