Dubbo introduction

Dubbo is an open source high-performance service framework of Alibaba, which enables applications to realize the output and input functions of services through high-performance RPC, and can be seamlessly integrated with the Spring framework.

Remote Procedure Call (RPC) is a protocol that requests services from Remote computer programs over the network without understanding the underlying network technology. The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans both the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprograms. RPC uses client/server mode. The requester is a client and the service provider is a server. First, the client calling process sends an invocation message with process parameters to the server process and then waits for the reply message. On the server side, the process stays asleep until the call message arrives. When a call message arrives, the server gets the process parameters, calculates the result, sends the reply message, and waits for the next call message. Finally, the client calling process receives the reply message, gets the process result, and the call execution continues.Copy the code

A profound

  • Maven Project Structure
Springboot - dubbo | - dubbo - API defines the interfaces / / | - dubbo - consumer / / consumer | dubbo - the provider / | - pom/service providersCopy the code
  • Dubbo supports many kinds of registries, such as Multicast, Redis, and ZooKeeper. Currently, ZooKeeper is the most popular one, and is strongly recommended by dubbo’s official website. This article also uses ZooKeeper as the registry for Dubbo. The installation process of ZooKeeper is not shown here. Zookeeper is released 3.4.12 and Dubbo is the latest version

       <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.5</version>
        </dependency>
    Copy the code
  • Defining service Interfaces

    • The project structure
      dubbo-api
          |--src
              |--main
                  |--java
                      |--com.fanhq.dubbo.api.IUserService
          |--pom
      Copy the code
    • User service interface
      public interface IUserService {
          String sayHello(String hello);
      }
      Copy the code
  • The service registry

    • The project structure
      dubbo-provider
          |--src
              |--main
                  |--java
                      |--com.fanhq.dubbo.provider
                          |--config
                          |--service
          |-- pom
      Copy the code
    • Service Provisioning Configuration
      @Configuration
      public class DubboConfiguration {
      
          @Bean
          public ApplicationConfig applicationConfig(a) {
              ApplicationConfig applicationConfig = new ApplicationConfig();
              applicationConfig.setName("provider-demo");
              return applicationConfig;
          }
      
          @Bean
          public RegistryConfig registryConfig(a) {
              RegistryConfig registryConfig = new RegistryConfig();
              registryConfig.setAddress(Zookeeper: / / "127.0.0.1:2181");
              registryConfig.setClient("curator");
              registryConfig.setProtocol("dubbo");
              returnregistryConfig; }}Copy the code
    • The service implementation
      @Service(timeout = 5000)
      public class UserServiceImpl implements IUserService {
      
          @Override
          public String sayHello(String hello) {
              System.out.println(hello);
              return "hello world"; }}Copy the code

      Note here @ service annotations are com. Alibaba. Dubbo. Config. The annotation. Service

  • Consumer testing

    • The project structure
      dubbo-consumer
          |--src
              |--main
                  |--java
                      |--com.fanhq.dubbo.consumer
                          |--config
                  |--test
                      |--com.fanhq.dubbo.consumer 
          |--pom   
      Copy the code
    • Consumption Item allocation
      @Configuration
      public class DubboConfiguration {
      
          @Bean
          public ApplicationConfig applicationConfig(a) {
              ApplicationConfig applicationConfig = new ApplicationConfig();
              applicationConfig.setName("consumer-demo");
              return applicationConfig;
          }
      
          @Bean
          public ConsumerConfig consumerConfig(a) {
              ConsumerConfig consumerConfig = new ConsumerConfig();
              consumerConfig.setTimeout(3000);
              return consumerConfig;
          }
      
          @Bean
          public RegistryConfig registryConfig(a) {
              RegistryConfig registryConfig = new RegistryConfig();
              registryConfig.setAddress(Zookeeper: / / "127.0.0.1:2181");
              registryConfig.setClient("curator");
              returnregistryConfig; }}Copy the code
    • Unit testing
      @RunWith(SpringRunner.class)
      @SpringBootTest(classes = ConsumerApplication.class)
      public class UserServiceTest {
      
          @Reference
          public IUserService userService;
      
          @Test
          public void userSeiviceTest(a){
              String result = userService.sayHello("hello"); System.out.println(result); }}Copy the code

The attached

  • Dubbo’s official website
  • Project gitbub address