Solving the problem of implicit parameter loss caused by dubbo multiple calls

In the process of using dubbo, if some general parameters are explicitly passed in the interface parameters, it will be inconvenient to use them.
For example, if the user information stored in the frequently used session is passed in the parameter, it will be coupled to the business logic. In this case, implicit parameter passing is a better way to deal with it.
Not much to say, the following is an implicit parameter passing method using RpcContext provided by dubbo:
RpcContext.setAttachments(Map<String, String> attachment);

In order to better consider the problem of implicit parameter loss caused by dubbo multiple calls, we can add two filter s to each dubbo service program to solve the problem!

1. Create two filter s
Consumer side

import java.util.HashMap;
import java.util.Map;

import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.fastjson.JSONObject;

@Activate
public class DubboConsumerContextFilter implements Filter {
	
	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		Map<String, String> context = new HashMap<>();
		//Processing session
        String sessionStr = "{\"userName\":\"testUser\"}";
		context.put("session", sessionStr);
		RpcContext.getContext().setAttachments(context);
		return invoker.invoke(invocation);
	}
}

 

Service provision

import java.util.Map;

import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.fastjson.JSONObject;

@Activate
public class DubboProviderContextFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        // Processing session
        String sessionStr = RpcContext.getContext().getAttachment("session");
        //Put sessionStr into the general processing session
        //TODO processing code
        return invoker.invoke(invocation);
    }

}


2. Configure filter to META-INF
/Under src/main/resources/META-INF/dubbo /, create a new file: com.alibaba.dubbo.rpc.Filter
The contents of the document are as follows:

dubboProviderContextFilter=com.xxx.DubboProviderContextFilter
dubboConsumerContextFilter=com.xxx.DubboConsumerContextFilter


 
3. Configure filter to spring configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <dubbo:provider filter="dubboProviderContextFilter" />
    <dubbo:consumer filter="dubboConsumerContextFilter" />
</beans>


--------------------- 
Author: a490789580
Source: CSDN
Original: https://blog.csdn.net/a490789580/article/details/88174796
Copyright notice: This is the original article of the blogger. Please attach the link of the blog!

Keywords: Dubbo Session Java Spring

Added by lilsim89 on Sat, 30 Nov 2019 06:45:37 +0200