[dubbo] 3. Using maven to build dubbo environment (demo)

Article directory

1 create provider

Build with idea.

1-1 create maven project


Demand analysis:
The demo of hello, the consumer transfers the user name, and the provider decomposes the user name and returns hello + name.

2 create provider

Create user entity (provider\src\main\java\entity\userentity.java)

package entity;

public class userentity {
    private String name;
    private  String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

Create an interface (provider\src\main\java\service\userService.java):

package service;

import entity.userentity;

public interface userService {

    public String SayHello(userentity user) ;
}

Create an interface implementation class (provider\src\main\java\service\impl\userserviceimpl.java):

package service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import entity.userentity;
import service.userService;


public class userserviceimpl implements userService {
    public String SayHello(userentity user) {
        return "Hello" + user.getName() + "Gender:" + user.getSex();
    }
}


/*
* 1,Register the server provider with the registry (expose service)
*       1)Introducing dubbo dependency and zookeeper client (cursor)
* 2,Let the service consumer go to the registry to subscribe to the service address of the service provider.
* */



//Error reporting:
//        1,java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup
//            This is because there is a version conflict between dubbo and zookeeper, and the client version of zookeeper needs to be updated together
/*
2,java.lang.IllegalStateException: Failed to register dubbo://192.168.25.10:20880/service.userService?anyhost=true&application=user-service-provider&bean.name=service.userService&dubbo=2.0.2&generic=false&interface=service.userService&methods=SayHello&pid=14916&side=provider&timestamp=1558431972456 to registry 192.168.25.128:2181, cause: Failed to register dubbo://192.168.25.10:20880/service.userService?anyhost=true&application=user-service-provider&bean.name=service.userService&dubbo=2.0.2&generic=false&interface=service.userService&methods=SayHello&pid=14916&side=provider&timestamp=1558431972456 to zookeeper zookeeper://192.168.25.128:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-provider&dubbo=2.0.2&interface=com.alibaba.dubbo.registry.RegistryService&pid=14916&timestamp=1558431972444, cause: KeeperErrorCode = Unimplemented for /dubbo/service.userService/providers/dubbo%3A%2F%2F192.168.25.10%3A20880%2Fservice
 This is because the version of Dubbo is too low, while the version of zookeeper and its client is higher. Upgrade the version of Dubbo, and now the version of apache.dubbo is higher.
3,Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener
* */

At this point, the service of the provider has been written.
Let me configure the consumer.

3 configuration provider

In pom file, add maven dependency.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <dependencies>
        <!-- Introduce dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <!-- Because we use zookeeper As a registry, so you need to operate zookeeper
    dubbo 2.6 Introduction of previous versions zkclient operation zookeeper
    dubbo 2.6 And later version introduction curator operation zookeeper
    The next two zk Client based on dubbo Select 1 for version 2
    -->
        <!--<dependency>-->
            <!--<groupId>com.101tec</groupId>-->
            <!--<artifactId>zkclient</artifactId>-->
            <!--<version>0.10</version>-->
        <!--</dependency>-->
        <!-- curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>


      
    </dependencies>


</project>

Add the configuration file in the resources folder. 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:cache="http://www.springframework.org/schema/cache"
       xmlns:tx="http://code.alibabatech.com/schema/dubbo" 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://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--1,Make the name of the current service (the same service name item, do not have the same name as other services)-->
<dubbo:application name="provider"></dubbo:application>
    <!--2/ Establish the location of the registry-->
    <dubbo:registry address="zookeeper://192.168.25.128:2181"></dubbo:registry>
    <!--<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" />-->
    <!--3/Specify communication rules-->
    <dubbo:protocol name="dubbo" port="20880" />

    <!--4/ Declare the service interface to be exposed -->
    <dubbo:service interface="service.userService" ref="userserviceimpl" />

    <!--5/And local bean Same implementation services -->
    <bean id="userserviceimpl" class="service.impl.userserviceimpl" />

 <!--Note: in steps 4 and 5 ref and id Correspondingly. -->

</beans>

To configure the provider's startup:
Create \ provider\src\main\java\Provider.java

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.in.read(); // press any key to exit
    }
}

At the end of this configuration, start the project:

We can see in dubbo monitoring center:

The structure of the provider project is as follows:

4 configure consumers

Also create a maven project:
Copy the entity and service interfaces in the provider project to this project, and most of the work is finished.
Here is the configuration and call.

5 configure consumers

Introduce dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
  
</dependencies>
</project>

Create the configuration file consumer.xml file

<?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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


    <!-- Application name -->
    <dubbo:application name="consumer12"></dubbo:application>
    <!-- Specify registry address -->
    <dubbo:registry address="zookeeper://192.168.25.128:2181" />
    <!-- Generate remote service agent, which can be connected with local bean Same use demoService -->
    <dubbo:reference interface="service.userService" id="userservice"></dubbo:reference>



<!--Be careful: dubbo:reference Medium interface ,Use provider Ports exposed in interface -->
</beans>

To create a launcher:

package testmain;


import entity.userentity;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import service.userService;

import java.io.IOException;

@Service
public class main {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        userentity userentity=new userentity();
        userentity.setName("2");
        userentity.setSex("22");
        userService demoService =  context.getBean(userService.class); // Get remote service agent
        String hello = demoService.SayHello(userentity); // Execute remote method
        System.out.println( hello ); // Show call results
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Run the consumer program:

At this point, the demo has been set up successfully.
Source code download:
https://download.csdn.net/download/aiming66/11192954

Keywords: Dubbo Java Zookeeper Apache

Added by designedfree4u on Wed, 06 Nov 2019 22:55:04 +0200