alibaba / alibaba-rsocket-broker

Alibaba RSocket Broker: Mesh, Streaming & IoT
https://alibroker.info
Apache License 2.0
761 stars 166 forks source link

在使用RSocketService做服务提供者时,我想用json进行序列化的问题 #193

Closed HrOice closed 2 years ago

HrOice commented 2 years ago

broker版本与依赖都是1.1.3

服务提供者

@RSocketService(serviceInterface = ClientService.class, encoding = "json")
@Service
public class ClientServiceImpl implements ClientService 

消费者

    @Bean
    public RSocketRequester loadBalancedRsocketRequester(UpstreamManager upstreamManager) {
        LoadBalancedRSocket loadBalancedRSocket = upstreamManager.findBroker().getLoadBalancedRSocket();
        RSocketStrategies rSocketStrategies = RSocketStrategies.builder()
                .encoder(new Jackson2JsonEncoder())
                .decoder(new Jackson2JsonDecoder())
                .build();
        loadBalancedRsocketRequester = RSocketRequester.wrap(loadBalancedRSocket,
                MimeType.valueOf("application/json"),
                MimeType.valueOf("message/x.rsocket.composite-metadata.v0"),
                rSocketStrategies);
        return loadBalancedRsocketRequester;
    }

但是调用的时候还是会用hessian序列化

具体错误如下

2022-06-09 17:42:44.963 ERROR 30998 --- [actor-tcp-nio-2] c.a.r.e.impl.RSocketEncodingFacadeImpl   : RST-700501: Failed to decode data Hessian to Hessian

com.alibaba.rsocket.encoding.EncodingException: RST-700501: Failed to decode data bytebuf to bytebuf
    at com.alibaba.rsocket.encoding.impl.ObjectEncodingHandlerHessianImpl.decodeParams(ObjectEncodingHandlerHessianImpl.java:46) ~[alibaba-rsocket-core-1.1.3.jar:na]
    at com.alibaba.rsocket.encoding.impl.RSocketEncodingFacadeImpl.decodeParams(RSocketEncodingFacadeImpl.java:68) ~[alibaba-rsocket-core-1.1.3.jar:na]
    at com.alibaba.rsocket.listen.RSocketResponderSupport.invokeLocalService(RSocketResponderSupport.java:215) [alibaba-rsocket-core-1.1.3.jar:na]
    at com.alibaba.rsocket.listen.RSocketResponderSupport.localRequestResponse(RSocketResponderSupport.java:53) [alibaba-rsocket-core-1.1.3.jar:na]
    at com.alibaba.rsocket.rpc.RSocketResponderHandler.requestResponse(RSocketResponderHandler.java:101) [alibaba-rsocket-core-1.1.3.jar:na]

是不是还有什么配置需要注意的呢,我在消费端debug的时候发现payload.metadata在经过com.alibaba.rsocket.metadata.RSocketCompositeMetadata#getDataEncodingMetadata时,返回的结果是x-hessian的

还是broker中转发就是使用的x-hessian呢。

linux-china commented 2 years ago

RSocketRequester是Spring RSocket的接口,其目前还不支持消息级别的encoding设置。 如果你要使用JSON类型的话,要使用Broker Client的代码,如下:

 @Bean
    public UserServiceExtra userServiceExtra(UpstreamManager upstreamManager) {
        return RSocketRemoteServiceBuilder
                .client(UserServiceExtra.class)
                .service(UserService.class.getCanonicalName())
                .upstreamManager(upstreamManager)
                .acceptEncodingType(RSocketMimeType.Json)
                .build();
    }