dubbo Simple Implementation Using Custom Registration Center

Posted by twick100 on Tue, 18 Jun 2019 20:29:45 +0200

2017-06-25

The project GitHub address https://github.com/jiangcaijun/point2Point_dubbo

1. dubbo's Design Idea

> dubbo is a distributed service framework dedicated to providing high-performance and transparent RPC remote service invocation solutions, registering on dubbo in the form of service providers and consumers

2. Custom Implementation Registry

Three new projects, all managed by maven, are based on JDK 1.7. To achieve point-to-point production and consumption.

  • dubbo-api (registry: registry for service registration and discovery)
  • Dubbo-provider (producer: service provider of exposed services)
  • Dubbo-consumer (consumer: service consumer invoking remote services)

2.1 dubbo-api (registry)

The pom of the new project is as follows

<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>

Create a new interface, the producer will be responsible for the implementation of the interface, and the consumer will call the interface.

package com.dubbo.demo;

public interface IHelloWorld {
    public String sayHello(String name);

    public Object doSomeThing();
}

Then, it is published to the local warehouse using using maven install, and both producers and consumers will introduce this dependency in pom.xml.

Its project structure is as follows:

2.2 dubbo-provider (producer)

The pom of the new project is as follows.

<groupId>com.dubbo.demo.provider</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
    <slf4j.version>1.7.21</slf4j.version>
</properties>
<dependencies>
    <dependency>
        <groupId>com.dubbo.demo</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>

The producer introduces dubbo-api (registry) and Dubbo dependencies, and adds log-related dependencies to facilitate debugging.

Implementing the interface of dubbo-api (registry)

package com.dubbo.demo.provider;

import com.dubbo.demo.IHelloWorld;

public class HelloWorldServiceImpl implements IHelloWorld {

    public String sayHello(String name) {
        return "Hello World  " + name + ", I come from dubbo-api " ;
    }

    public Object doSomeThing() {
        return "i am doSomeThing, I come from dubbo-api " ;
    }
}

Create a new provider.xml in the resource directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- Providers apply information for computing dependencies -->
    <dubbo:application name="hello_world_provider" owner="jcj" />

    <!--Represents where our services are registered-->
    <dubbo:registry address="N/A"></dubbo:registry>

    <!-- use dubbo Protocol Exposure Service at Port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

    <!-- Specific realization bean -->
    <bean id="helloService" class="com.dubbo.demo.provider.HelloWorldServiceImpl" />

    <!-- Declare service interfaces that need to be exposed -->
    <dubbo:service interface="com.dubbo.demo.IHelloWorld" ref="helloService" />

</beans>

At the same time, a new dubboTest method is created to start dubbo-provider.

public class dubboTest {

    public static void main(String []args) throws IOException {

        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(new String("provider.xml"));

        classPathXmlApplicationContext.start();

        System.in.read();
    }
}

Its project structure is shown in Figure 1 (the red circle in the figure represents the introduction of dubbo-api dependencies):

2.3 dubbo-consumer (consumer)

Its pom is similar to dubbo-provider (producer). It needs to introduce dubbo-api (registry) and Dubbo dependency. Create a new consumer.xml in the resource directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- Providers apply information for computing dependencies -->
    <dubbo:application name="hello_world_provider" owner="jcj" />

    <!--Represents where our services are registered-->
    <dubbo:registry address="N/A"></dubbo:registry>

    <!-- Declare service interfaces that need to be exposed -->
    <dubbo:reference interface="com.dubbo.demo.IHelloWorld"
                   url="dubbo://127.0.0.1:20880/com.dubbo.demo.IHelloWorld"
                   id="helloService"></dubbo:reference>

</beans>

Comparing with dubbo-provider provider.xml, they all declare that they need to expose the service interface, that is, the interface method of dubbo-api (registry).

New test method dubboConsumerTest

import com.dubbo.demo.IHelloWorld;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

public class dubboConsumerTest {

    public static void main(String []args) throws IOException {

        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(new String("consumer.xml"));

        IHelloWorld iHelloWorld = (IHelloWorld)classPathXmlApplicationContext.getBean("helloService");

        String result = iHelloWorld.sayHello("Ha ha ha");

        System.out.println("dubboConsumerTest.main: " + result);

        Object object = iHelloWorld.doSomeThing();

        System.out.println("dubboConsumerTest.main: " + object.toString());
    }
}

Its project structure is shown in Figure 1 (the red circle in the figure represents the introduction of dubbo-api dependencies):

2.4 Point-to-Point dubbo Test

Start the Dubbo test method of dubbo-provider (producer), then the service begins to be provided. At this time, the output of the producer console is as follows:

[ com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol ] - [ INFO ]  
[DUBBO] disconected from /192.168.58.1:54584,url:dubbo://192.168.58.1:20880/com.dubbo.demo.IHelloWorld?anyhost=true&application=hello_world_provider&channel.readonly.sent=true&codec=dubbo&dubbo=2.5.3&heartbeat=60000&interface=com.dubbo.demo.IHelloWorld&methods=doSomeThing,sayHello&owner=jcj&pid=15740&revision=1.0-SNAPSHOT&side=provider&timestamp=1498555464131, dubbo version: 2.5.3, current host: 127.0.0.1

Start the Dubbo Consumer Test of dubbo-consumer again and try to call this method. At this time, the consumer console output:

Refer dubbo service com.dubbo.demo.IHelloWorld from url
dubbo://127.0.0.1:20880/com.dubbo.demo.IHelloWorld?application=hello_world_provider&dubbo=2.5.3&interface=com.dubbo.demo.IHelloWorld&methods=doSomeThing,sayHello&owner=jcj&pid=8296&revision=1.0-SNAPSHOT&side=consumer&timestamp=1498555502597, dubbo version: 2.5.3, current host: 192.168.58.1
dubboConsumerTest.main: Hello World  Ha ha ha, I come from dubbo-api 
dubboConsumerTest.main: i am doSomeThing, I come from dubbo-api 

Prove that the test has passed.

3. Using zookeeper to realize registry

3.1 zookeeper Installation

  • Be sure to install the Java environment before installing zookeeper
  • Download address: http://www.apache.org/dyn/closer.cgi/zookeeper/ I downloaded zookeeper-3.4.5.tar.gz
  • After tar-zxvf zookeeper-3.4.5.tar.gz is decompressed, copy zoo_sample.cfg in conf directory, i.e. cpzoo_sample.cfg zoo.cfg. (Reason: Zookeeper will find this file as the default configuration file at startup)
  • Start zookeeper
    • . / zkServer.sh start-foreground (you can see the startup log)
    • ./zkServer.sh start
  • Test startup results
    • ./zkServer.sh status
    • The default TCP connection port used by zookeeper is 2181
      • Check whether port 61616 is open: netstat-an | grep 2181
      • Find Occupied pid Number by Netstat-tlnp
      • Find the process corresponding to zookeeper: ps-ef | grep zookeeper
  • Close zookkeeper
    • ./zkServer.sh stop

3.2 to be continued

Topics: Dubbo Zookeeper xml snapshot