Dubbo uses multiple protocols and serialization
1. Maven coordinates of dependent class libraries:
- group id: org.apache.dubbo
- artifact id: dubbo
2. Briefly introduce the Dubbo class library:
Dubbo is a high-performance, lightweight Java RPC framework. It supports multiple network protocols and serialization methods, and provides an integrated solution, including service management, service invocation, service Publish–subscribe pattern, etc.
The protocols supported by Dubbo include: Dubbo, rmi, Hessian, HTTP, webservice, etc. Among them, dubbo is the default protocol.
The serialization methods supported by Dubbo include: Java, Hessian2, JSON, protobuf, etc. Among them, Java is the default serialization method.
3. Complete Java code example:
package com.example;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
public class DubboExample {
public static void main(String[] args) {
//Set application configuration
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("dubbo-example");
//Set Registry Configuration
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://localhost:2181");
//Set Protocol Configuration
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(12345);
//Set Service Configuration
ServiceConfig<HelloService> serviceConfig = new ServiceConfig<>();
serviceConfig.setApplication(applicationConfig);
serviceConfig.setRegistry(registryConfig);
serviceConfig.setProtocol(protocolConfig);
serviceConfig.setInterface(HelloService.class);
serviceConfig.setRef(new HelloServiceImpl());
//Publishing Services
serviceConfig.export();
//Set reference configuration
ReferenceConfig<HelloService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(HelloService.class);
//Reference Service
HelloService helloService = referenceConfig.get();
//Call Service
String result = helloService.sayHello("Dubbo");
System.out.println(result);
//Close Dubbo Bootstrap
DubboBootstrap.getInstance().shutdown();
}
}
The 'HelloService' in the example is a simple service interface, specifically defined as follows:
package com.example;
public interface HelloService {
String sayHello(String name);
}
The 'HelloServiceImpl' in the example is the implementation class of the 'HelloService' interface, which is defined as follows:
package com.example;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
4. Summary:
Dubbo is a rich Java RPC framework that supports multiple network protocols and serialization methods. By using Dubbo's dependency class library, it is easy to publish and call services. In the example code, we successfully published a simple service by setting application configuration, registry configuration, protocol configuration, and service configuration, and called the service through reference configuration and reference service. Finally, we closed Dubbo Bootstrap to free up resources. By using Dubbo, it is easy to build distributed systems and improve system performance and scalability.