Learn the basic usage of the Dubbo ALL framework through the example code
Dubbo ALL is a high -performance, lightweight distributed service framework. It provides powerful functions such as service governance, service registration, and remote calls, which greatly simplifies the development and deployment of distributed systems.The basic usage of the Dubbo ALL framework is learned by the example code.
First, we need to create a Dubbo service provider.The following is a simple example code:
package com.example.dubbo.provider;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
public class ServiceProvider {
public static void main(String[] args) {
// 1. Create an application configuration
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("dubbo-provider-example");
// 2. Create a registered center configuration
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://localhost:2181");
// 3. Create an agreement configuration
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
// 4. Create a service configuration
ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<>();
serviceConfig.setApplication(applicationConfig);
serviceConfig.setRegistry(registryConfig);
serviceConfig.setProtocol(protocolConfig);
serviceConfig.setInterface(GreetingService.class);
serviceConfig.setRef(new GreetingServiceImpl());
// 5. Exposure service
serviceConfig.export();
// 6. Block the main thread and keep the service provider running all the time
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, we first created an application configuration, which set the name of the application.Then create a registered center configuration, specify Zookeeper as the registration center, and set the address of the registration center.Next, a protocol configuration was created, Dubbo was used as a communication protocol, and a monitoring port was set up.Finally, a service configuration is created, the service interface and specific implementation class are configured, and the previously created application configuration, registration center configuration and protocol configuration are applied to the service configuration, and finally exposed the service through the `Export ()" method.
Next we need to create a Dubbo service consumers.The following is a simple example code:
package com.example.dubbo.consumer;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.example.dubbo.provider.GreetingService;
public class ServiceConsumer {
public static void main(String[] args) {
// 1. Create an application configuration
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("dubbo-consumer-example");
// 2. Create a registered center configuration
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://localhost:2181");
// 3. Create a service reference configuration
ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(GreetingService.class);
// 4. Quote service
GreetingService greetingService = referenceConfig.get();
// 5. Call service
String message = greetingService.sayHello("Dubbo");
// 6. Print results
System.out.println("Result: " + message);
}
}
In the above code, we also first created an application configuration and registration center configuration, which is the same as the configuration of the service provider.Then create a service reference configuration, set the application configuration and registration center configuration, and specify the service interface to be quoted.Obtain instances of the service through the `Get ()` method.Finally, we can call the service method and print the result.
The above is the basic usage example of the Dubbo all framework.Through these example code, we can better understand the use of the Dubbo All framework and flexibly apply it in actual development.