Multiple Protocols
Configuring Multiple Protocols in Dubbo
Unlike ordinary RPC frameworks, Dubbo, as a microservices framework, provides very flexible protocol support and does not bind to a single communication protocol. Therefore, you can publish multiple RPC protocols simultaneously in one process and call different RPC protocols. Next, we will detail the specific use cases and methods for multiple protocols.
Use Cases
There are many scenarios where different protocols may be needed, including security, performance, and interaction with third-party systems. This article does not analyze specific business requirements but instead explores the multi-protocol capabilities provided by the Dubbo framework:
- As a service provider, the same service can be published under multiple protocols for different consumers.
- As a service provider, multiple services can be published under different protocols for different consumers.
- As a service consumer, a specific protocol can be designated to call a service.
Usage
Publish the Same Service under Multiple Protocols
If using Spring Boot, you can modify application.yml or application.properties as follows:
dubbo:
protocols:
dubbo-id:
name: dubbo
port: 20880
tri-id:
name: tri
port: 50051
For Spring XML:
<dubbo:protocol id="dubbo-id" name="dubbo" port="20880"/>
<dubbo:protocol id="triple-id" name="tri" port="50051"/>
Next, configure the service (by default, the service will be published to all protocol configurations above):
@DubboService(protocol="dubbo-id,triple-id")
private DemoServiceImpl implements DemoService {}
Publish Multiple Services under Different Protocols
If using Spring Boot, modify application.yml or application.properties as follows:
dubbo:
protocols:
dubbo-id:
name: dubbo
port: 20880
tri-id:
name: tri
port: 50051
Next, configure different protocol references for different services:
@DubboService(protocol="dubbo-id")
private DemoServiceImpl implements DemoService {}
@DubboService(protocol="triple-id")
private GreetingServiceImpl implements GreetingService {}
Specify Protocol to Call Services
For consumers, simply specify the protocol keyword when declaring the reference:
@DubboReference(protocol="dubbo")
private DemoService demoService;
@DubboReference(protocol="tri")
private GreetingService greetingService;
Different Implementation Methods
Multi-Port Multi-Protocol
Multi-protocol publishing means providing multiple protocol access methods for the same service. Multi-protocol can be any combination of two or more protocols, such as the configuration below which simultaneously publishes both dubbo and triple protocols:
dubbo:
protocols:
tri:
name: tri
port: 50051
dubbo:
name: dubbo
port: 20880
Based on the configuration above, if the application has the service DemoService, it can be accessed via both the dubbo and triple protocols. The working principle diagram is as follows:
- The provider instance simultaneously listens on both ports 20880 and 50051.
- The same instance registers two address URLs in the registration center.
- Different consumers can choose to call the services published by the same provider using different protocols.
For consumers, if the user has not specified, the framework will automatically select the dubbo
protocol by default. The Dubbo framework supports configuring which protocol accesses the service, such as @DubboReference(protocol="tri")
, or specifying a global default in the application.yml configuration file:
dubbo:
consumer:
protocol: tri
Single-Port Multi-Protocol
In addition to the methods of publishing multiple ports and registering multiple URLs to the registration center, for the built-in dubbo and triple protocols, the framework provides the ability to publish both protocols on a single port simultaneously. This is an important capability for long-time users, as it allows users utilizing the dubbo protocol to additionally publish the triple protocol without adding any burden. Once all applications realize multi-protocol publishing, we can set consumers to initiate calls via the triple protocol.
The basic configuration for single-port multi-protocol is as follows:
dubbo:
protocol:
name: dubbo
ext-protocol: tri
Feedback
Was this page helpful?
Yes No
Last modified November 7, 2024: fix multi-protocols docs (#3058) (de52c77177a)