Although micro services have become popular, but because many of the company’s architecture is still using dubbo+ ZooKeeper with a combination of distributed project architecture, so here is a symbolic note, can be used for aftertaste, the blog was based on the latest stable version at that time, there are changes in subsequent versions, please pay attention to.(Springboot 2.3.4 + dubbo2.7 + zookeeper3.6.2)

After successfully deploying the following environment, there is so much sample code that it is placed directly on the code cloud :gitee.com/zhang-xiao-…

Set up the ZooKeeper service registry to prepare for receiving dubbo service registration

Go to the official website and choose download. The Windows and Linux versions are mixed in one package, so kill two birds with one stone. My Version of ZooKeeper is 3.6.2

Download address: www.apache.org/dyn/closer….

CFG configuration file (default zoo_sample. CFG, need to change the name to zoo.cfg), configure the data path and log path (the Windows version is used as an example, so change it).

# config file dataDir=.. /data dataLogDir=.. /logCopy the code

Start the way

Successful startup as shown in figure

Note that the client connection is successful (note that the client connection will occupy port 8080, so pay attention to either test the client connection, and then close the CMD window, because the default port of Dubbo is 8080, of course, the back port of 8080 can also be modified, see your choice)

Install dubbo monitoring console to monitor your own service

Go to github.com/apache/dubb… , it is recommended to download the idea git method. The initial knowledge process is shown in the figure (the reason for packaging is that the front-end project needs to be packaged). The version here is ** dubo-admin ** 2.7.7

Then start DubboAdminApplication (zooKeeper also has a console, but it is not easy to use, Ali has provided monitoring services, will be enough to complete, and nacos registry, Eureka registry console style is basically the same), test access to the monitoring console http://localhost:8081/#/

Write project code (just look at the flow, otherwise it gets a little messy)

As projects may not be split or structured exactly the same, so just for reference, if too complicated is not good for getting started, my project structure is roughly as follows

A: public module (write some public module)

Two: Service interface module (a proposed module, basically do not need to configure)

1: Adds dependencies

<! Dubbo -> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> The < version > 2.7.7 < / version > < / dependency > <! --> <dependency> <groupId>com.zhang</groupId> <artifactId> zxx-dubo-common </artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency >Copy the code

2: Write several interfaces

Three: Service stop module (namely service interface implementation module)

1: Add major dependencies

        <!-- 添加dubbo依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
        </dependency>

        <!--zookeeper 注册中心客户端引入 使用的是curator客户端 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.4.1</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加公共依赖-->
        <dependency>
            <groupId>com.zhang</groupId>
            <artifactId>zxx-dubbo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
Copy the code

2: Write a configuration file

Name =zxx-dubbo-provider server.port=7001 # dubbo scan package, scan service implementation class Dubbo. Scan. Base - packages = com. Zhang. ZXX. Dubbo. Provider. # serviceimpl dubbo application name, the default name and Spring boot Application. Name =${spring.application. Name} # Dubbo protocol and port dubbo https://github.com/apache/dubbo-admin/wiki/Dubbo-Admin%E9%85%8D%E7%BD%AE%E8%AF%B4%E6%98%8E # # new version 2.7 Admin. Config - center = "zookeeper: / / 127.0.0.1:2181 dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 Dubbo. Metadata - report. Address = zookeeper: / / 127.0.0.1:2181Copy the code

3: Add annotations (note that some annotations have been updated in the new version, such as @service updated to @dubboService, which is not confused with the @service provided by Spring). Add @dubboService to the Service implementation interface and @enableDubbo to the startup class

 

Three: service consumption module

1: Add major dependencies

<!-- 添加dubbo依赖 -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.7</version>
		</dependency>
		<!--zookeeper 注册中心客户端引入 使用的是curator客户端 -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-dependencies-zookeeper</artifactId>
			<version>2.7.4.1</version>
			<type>pom</type>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
			</exclusions>

		</dependency>

		<!--添加公共依赖-->
		<dependency>
			<groupId>com.zhang</groupId>
			<artifactId>zxx-dubbo-interface</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
Copy the code

2: Write a configuration file

Name =zxx-dubbo-consumer server.port=7002 # dubbo application Dubo.application. name=${spring.application.name} ## https://github.com/apache/dubbo-admin/wiki/Dubbo-Admin%E9%85%8D%E7%BD%AE%E8%AF%B4%E6%98%8E Admin. Config - center = "zookeeper: / / 127.0.0.1:2181 dubbo. Registry. Address = zookeeper: / / 127.0.0.1:2181 Dubbo. Metadata - report. Address = zookeeper: / / 127.0.0.1:2181Copy the code

3: Add annotations (start class @enableDubbo and control layer call @dubboReference, this annotation can invoke the exposed service)

The above steps basically complete the construction of the project, start the ZooKeeper registry and Dubbo Admin console project, and then start the service provision module and access call module to test

Test the sample

The above is the whole process, of course, can continue to optimize, in the purpose of entry, for the sake of simplicity, so built out, if the feeling of the code is a bit messy, you can download the code cloud project to test gitee.com/zhang-xiao-…