Dubbo multi-protocol support

Keywords: Dubbo Attribute Jetty Netty

Dubbo version: 2.6.5

Dubbo supports dubbo, rmi, hessian, http, web service, thrift, redis and other protocols, but Dubbo's official website recommends that we use the Dubbo protocol. Dubbo can publish a single protocol, multiple protocols for the same service, multiple protocols for multiple services, and so on.

1. Publish a single protocol

(1) By configuring the service in the following way, we can publish a Dubbo protocol service; if not, Dubbo publishes the Dubbo protocol service by default.

<!-dubbo protocol: exposing services on port 20880 with Dubbo protocol - >
<dubbo:protocol name="dubbo" port="20880"/>

(2) If we need to publish a hessian protocol service, we need to introduce some Maven dependencies

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.62</version>
</dependency>
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
    <version>6.1.26</version>
</dependency>
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.32.Final</version>
</dependency>

The server publishes the service and declares the exposed service port as the hessian protocol

<!--Declare service interfaces that need to be exposed -->
<dubbo:service interface="com.test.dubbo.ISayHello" ref="sayHelloService" protocol="hessian"/>
<bean id="sayHelloService" class="com.test.dubbo.SayHelloImpl"/>

On the consumer side, no operation can be specified. If you specify that the service only receives the hessian protocol, then add the attribute protocol and specify the protocol name, then only the service provider of the specified protocol will be invoked, and other protocols will ignore.

<dubbo:reference id="sayHelloService" interface="com.test.dubbo.ISayHello" protocol="hessian"/>

2. Publishing multiple protocols for the same service

The service side publishes the services of dubbo protocol and hessian protocol

<!-- Multi-protocol configuration -->
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:protocol name="hessian" port="8080"/>

<!--Declare service interfaces that need to be exposed -->
<dubbo:service interface="com.test.dubbo.ISayHello" ref="sayHelloService" protocol="dubbo,hessian"/>
<bean id="sayHelloService" class="com.test.dubbo.SayHelloImpl"/>

If the consumer specifies that the service only receives the hessian protocol, then the attribute protocol is added and the protocol name is specified, then the service provider of the specified protocol will be invoked only, while the other protocols will be ignored. On the contrary, removing the protocol attribute, both protocols will receive.

<dubbo:reference id="sayHelloService" interface="com.test.dubbo.ISayHello" protocol="dubbo"/>

3. Different services publish different protocols

<!--Declare service interfaces that need to be exposed -->
<!-- ISayHello service,provide dubbo Agreement -->
<dubbo:service interface="com.test.dubbo.ISayHello" ref="sayHelloService" protocol="dubbo"/>
<bean id="sayHelloService" class="com.test.dubbo.SayHelloImpl"/>

<!-- IStartStudy service,provide hessian Agreement -->
<dubbo:service interface="com.test.dubbo.IStartStudy" ref="startStudyService" protocol="hessian"/>
<bean id="startStudyService" class="com.test.dubbo.StartStudyImpl"/>

END

Posted by nrussell on Thu, 01 Aug 2019 20:19:14 -0700