dubbo Learning - From demo: hello world

Keywords: Java Dubbo xml Spring

dubbo Learning - From demo: hello world

Label (Space Separation): dubbo demo

[TOC]

0. outline

In the previous article, we have turned the dubbo project into a gradle build tool and imported it into eclipse. In this article, we start with the demo of the source code and build a working environment. Next, I will analyze the demo source code and begin to analyze the dubbo framework.

1. Define service interfaces

dubbo-demo-api: This module defines a service interface: DemoService Function: sayHello

> DemoService.java

package com.alibaba.dubbo.demo;

public interface DemoService {

    String sayHello(String name);

}

The above service interfaces need to be packaged separately and shared between service providers and consumers.

2. Implementing Service Interface

2.1. Implementing service interfaces on service providers

> DemoServiceImpl.java

package com.alibaba.dubbo.demo.provider;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.dubbo.demo.DemoService;
import com.alibaba.dubbo.rpc.RpcContext;

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }

}

2.2. Exposure services

> dubbo-demo-provider.xml

<?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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />

    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

</beans>

2.3. Demo Server Start

Start the service provider using dubbu's container tool class Main:

> DemoProvider.java

package com.alibaba.dubbo.demo.provider;

public class DemoProvider {

    public static void main(String[] args) {
        com.alibaba.dubbo.container.Main.main(args);
    }

}

The dubbo configuration file is as follows:

> dubbo.properties

dubbo.container=log4j,spring
dubbo.application.name=demo-provider
dubbo.application.owner=william
dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.service.loadbalance=roundrobin

3. Consumer-side use of services

3.1. Writing Service Consumers

> DemoAction.java

package com.alibaba.dubbo.demo.consumer;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.dubbo.demo.DemoService;

public class DemoAction {

    private DemoService demoService;

    public void setDemoService(DemoService demoService) {
        this.demoService = demoService;
    }

    public void start() throws Exception {
        // Loop call to sayHello service
        for (int i = 0; i < Integer.MAX_VALUE; i ++) {
            try {
                String hello = demoService.sayHello("world" + i);
                System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Thread.sleep(2000);
        }
    }

}

3.2. Reference to remote services

> dubbo-demo-consumer.xml

<?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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />

</beans>

3.3. Demo consumer start-up

Start the service provider using dubbu's container tool class Main:

> DemoConsumer.java

package com.alibaba.dubbo.demo.consumer;

public class DemoConsumer {

    public static void main(String[] args) {
        com.alibaba.dubbo.container.Main.main(args);
    }

}

The dubbo configuration file is as follows:

> dubbo.properties

dubbo.container=log4j,spring
dubbo.application.name=demo-consumer
dubbo.application.owner=
dubbo.registry.address=multicast://224.5.6.7:1234

4. Start Demo

4.1. Start the server first, then the consumer.

  • Execute the main method of DemoProvider and pop up the following prompt:

  • Regardless of the determination of execution, the console log is as follows:

    Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:242)
    at com.alibaba.dubbo.common.extension.ExtensionLoader.loadFile(ExtensionLoader.java:627)
    ... 32 more

    Nani ~! Abnormal! What happened? Then execute the consumer end test.

  • Execute DemoConsumer's main method and pop up the following prompt:
  • Regardless of the determination of execution, the console log is as follows:
    Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:242)
    at com.alibaba.dubbo.common.extension.ExtensionLoader.loadFile(ExtensionLoader.java:627)
    ... 42 more

    Or abnormal...!! What happened?

4.2. Demo exception resolution

Looking at the exception above, the compiled class file is incompatible with jre According to the official documents, the dependencies are as follows:

Look at the specific module code and find an error.

  • PojoUtils.java in dubbo-common has an error and can't find Concurrent SkipListMap, which does not exist in JDK5
  • dubbo-admin, dubbo-rpc-thift, dubbo-remoting-grizzly modules have similar errors
  • Modify the contents of the build script file build.gradle in the dubbo root directory:
subprojects {
    apply plugin: 'java'
    sourceCompatibility = 1.5
    targetCompatibility = 1.5

    tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

    dependencies {
        testCompile group: 'junit', name: 'junit', version:'4.10'
        testCompile group: 'org.easymock', name: 'easymock', version:'3.0'
        testCompile group: 'org.easymock', name: 'easymockclassextension', version:'3.0'
        testCompile group: 'com.googlecode.jmockit', name: 'jmockit', version:'0.999.8'
    }

}
  • Replace with the following:
subprojects {
    apply plugin: 'java'
    sourceCompatibility = 1.6  //Modify here
    targetCompatibility = 1.6  //Modify here

    tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }

    dependencies {
        testCompile group: 'junit', name: 'junit', version:'4.10'
        testCompile group: 'org.easymock', name: 'easymock', version:'3.0'
        testCompile group: 'org.easymock', name: 'easymockclassextension', version:'3.0'
        testCompile group: 'com.googlecode.jmockit', name: 'jmockit', version:'0.999.8'
    }

}
  • refresh gradle project waits for execution to end, and there are no errors in the project.

4.3. Run Demo again

4.3.1. Service Provider End and Consumer End

  • After executing the main method of DemoProvider:

    Dubbo service server started! 
  • After executing DemoConsumer's main method, Consumer:

    [21:19:18] Hello world0, response form provider: 192.168.56.1:20880
    [21:19:20] Hello world1, response form provider: 192.168.56.1:20880
  • After executing DemoConsumer's main method, Provider:
    [21:19:18] Hello world0, response form provider: 192.168.56.1:20880
    [21:19:20] Hello world1, response form provider: 192.168.56.1:20880

4.3.2. Consumer end and service end

  • After executing DemoConsumer's main method:
    Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.alibaba.dubbo.demo.DemoService. No provider available for the service com.alibaba.dubbo.demo.DemoService from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&loadbalance=roundrobin&methods=sayHello&owner=william&pid=7232&side=consumer&timestamp=1489411728629 to the consumer 192.168.56.1 use dubbo version 2.0.0
    at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)
    at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)
    at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)
    at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:166)
    ... 22 more

So multicast can't start consumers first.

So far, dubbo's own demo of multicast mode has been demonstrated. In the next chapter, we'll look at some of the things used in the demo source code.

Posted by hanhao on Wed, 09 Jan 2019 13:30:17 -0800