Build Dubbo + ZooKeeper with Mav

2018-07-05  本文已影响0人  猪迹

Step 1 Make the service a Dubbo service

Open the pom.xm that defines the project and see its initial status Add a dependency section & add the dependency to dubbo

Here, we need to add an exclusion section inside the dependency-secion, to strike with some pitfall from dubbo

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
Put cursor above the declaration of the service class Start typing 'Service' and we can see the hints below The IDE imports the class automatically for us

Step 2 Import Spring and make use of its annotation

Open 'pom.xml' again, and add the below dependency block into 'dependencies' section.

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>
Add dependency to spring-webmvc

Open HelloService.java, cursor above the declaration of the method and start typing @Autowired

See prompt from the IDE, which means the dependency works The class is automatically imported

Step 3 Take a break here and compile, to see it works.

Compile success

Step 4 Add a main-entry to the project

Till now, we didn't have a main-entry for the project, now let's add it.


Select to create a new Java class Specify the name and type Check to see its initial status Create a context and then start it

We specified a dubbo-provider.xm to create the context, which we never see before. We will create it now.

Step 5 Create the XML file to define the usage of ZooKeeper server

Select to create a new file

It should be an XML file, and its name should be the same as the one in code.


Specify the name

Declare that it uses dubbo-protocol to work with zookeeper


dubbo to zookeeper Define a service Define the concrete class that associated with the service

Step 6 Try to run it

Find the main method & run it Check the exception output

From the exception message, we know that it relates to something about ZooKeeper.
The reason is that we didn't add a dependency to ZooKeeper.
In the next article, we will fix it.

上一篇 下一篇

猜你喜欢

热点阅读