javaspringserializationkryoapache-dubbo

How to set dubbo serialization on consumer side?


For my dubbo based system, I use the API configuration as described here: https://dubbo.apache.org/en/docs/v2.7/user/configuration/api/

Now if I want to set e.g. kryo serialization on the provider side, I can easily do so when configuring the protocol:

// Protocol
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setSerialization("kryo");  // here
protocol.setThreads(200);

Now my question is: how can I do the same thing on the consumer side?

The ReferenceConfig does only provide a .setProtocol(String) method and has no way to set a ProtocolConfig in the same way possible for ServiceConfig which has the .setProtocol(ProtocolConfig) method.

Thanks


Solution

  • The ReferenceConfig offers setParameters. Use the serialization key to set the protocol. For kryo:

    ReferenceConfig referenceConfig = ...;
    
    HashMap<String, String> parameter = new HashMap<>();
    parameter.put("serialization", "kryo");
    referenceConfig.setParameters(parameter);
    

    It is necessary to set the consumer serialization type for direct connections (that is if no registry server is in use). If a registry is used, the serialization type is negotiated automatically.