Dubbo implicit parameter transfer-the use of Dubbo RpcContext

package com.ght.park.biz.api.filter;

import com.alibaba.fastjson.JSON;
import com.ght.park.biz.api.shiro.JwtUtil;
import com.ght.park.web.constants.ShiroConstants;
import com.ght.park.web.dto.LoginUser;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @Description dubbo拦截
 * @Author ljh
 * @Date 2020:09:27 11:47
 */
@Activate
public class DubboAccountFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		// 我这里是从JWT里拿到用户信息传递给Provider
        LoginUser user = null;
        try {
            user = JwtUtil.getLoginUser();
        } catch (Exception e) {
        }
        RpcContext context = RpcContext.getContext();
        if(user != null) {
        	// 设置参数
            context.setAttachment(ShiroConstants.ACCOUNT_INFO, JSON.toJSONString(user));
        } else {
            context.removeAttachment(ShiroConstants.ACCOUNT_INFO);
        }
        return invoker.invoke(invocation);
    }
}

Insert picture description here
Add the file com.alibaba.dubbo.rpc.Filter to the resources. Note that this stuff may be different with the change of the version. The
content of the file is:

dubboAccountFilter=com.ght.park.biz.api.filter.DubboAccountFilter

Finally, add in the configuration file application

dubbo:
  consumer:
    filter: dubboAccountFilter

Guess you like

Origin blog.csdn.net/qq_38238041/article/details/109189389