This article is a very basic integration of Dubbo and ZooKeeper. The purpose is to get a basic understanding of Dubbo and ZooKeeper!
The project download address: download.csdn.net/download/ba…
Zookeeper: distributed remote service registry.
Dubbo: Remote service scheduling framework.
Here is a very brief (and perhaps inaccurate) summary of both, simple standalone deployment steps:
- Install ZooKeeper on the server and start it.
- Dubbo and ZooKeeper dependencies are added to the project and dubbo attributes are configured.
- Create a service provider;
- Create service consumers;
First, to facilitate service consumers’ scheduling of providers, we create a moudle project (which incidentally is also a preliminary understanding of the Moudle project) :
1. Create the parent moudle, New Project, select Spring Initializr, and add the Web module:
2. After the package is created, delete the SRC folder and add the packaging attribute to pom. XML.
3. Add moudle and select Maven:
Parent Select the Parent moudle that you originally created:
The final project structure is as follows:
Open pom.xml in the root directory and find that moudle has been automatically added:
Common: some common tools or third-party libraries that implement certain functions; Dao: Persistence layer, which can be JPA or MyBatis, for manipulating databases; Service: the service layer; Web: project startup module, including Controller, Web page, function related (of course, controller can also be proposed);
At this point, there is no connection between these modules. If you need to associate these modules, you need to create dependencies. In this example, DAO depends on common, Service depends on DAO, and Web depends on Service:
Pom. XML under DAO:
<dependencies>
<dependency>
<groupId>com.blog</groupId>
<artifactId>byl-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Copy the code
Pom.xml under service:
<dependencies>
<dependency>
<groupId>com.blog</groupId>
<artifactId>byl-dao</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Copy the code
Pom.xml on the Web:
<dependencies>
<dependency>
<groupId>com.blog</groupId>
<artifactId>byl-service</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Copy the code
Create the package and start the class in the SRC folder on the web:
@SpringBootApplication(scanBasePackages = "com.blog")
public class WebApplication {
public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); }}Copy the code
Don’t forget to add scanBasePackages, com.blog is groupId!
To test whether the dependencies are correct, we can create an Utils class under the Common module and call it in the Web module:
In general, as long as the reference is successful, it indicates that there is no problem, now you can run:
OK, run successfully, we are now to deploy the zookeeper, used in this article the zookeeper version for 3.4.14 (the latest version for 3.6.2), download links: archive.apache.org/dist/zookee…
When we test, we can first deploy to the local, this example in the Windows environment:
1. Decompress the package:
2. Open the conf folder, copy the zoo_sample. CFG file, rename it zoo. CFG, and open the modification and add file.
DataDir = E: \ zookeeper - 3.4.14 \ data dataLogDir = E: \ zookeeper - 3.4.14 \ logCopy the code
The paths are the root directory of ZooKeeper, and the data and log folders are created by themselves.
3. Go to the bin directory and double-click zkServer. CMD to start ZooKeeper. (The Linux deployment steps are basically the same.)
After ZooKeeper runs successfully, we start creating service providers in the project, in this case the Service module as the service provider and the Web module as the service consumer (note: the common and DAO in the project are basically useless, just to test the multi-module project build)!
Open the root directory of the pop. XML, add dubbo and ZooKeeper related dependencies (because I am also a preliminary use of Doubbo and Zookeeper configuration, online cases are varied, various versions, all kinds of writing methods are available, no one really works… This example is also after a lot of time to explore, to run successfully) :
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
Copy the code
Curator and ZKClient are both ZooKeeper clients. Here we choose curator. In some cases, we have both Curator and ZkClient, and the version number is varied, beginners are easily dizzy!
In the resources folder, create the application. Properties configuration file. In the SRC folder, create the package and service:
interface:
public interface TestProviderService {
String testProvider(a);
}
Copy the code
impl:
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@Component
@DubboService
public class TestProviderServiceImpl implements TestProviderService {
@Override
public String testProvider(a) {
return "I'm a service provider"; }}Copy the code
Note that Service! Must be registered with the @dubboService annotation.
Properties and dubbo: (ZooKeeper has been installed and started successfully. The service address is local IP + port number 2181)
Dubbo.application. name=byl_blog_service dubbo.registry. Protocol =zookeeper Local IP: 2181 dubbo. Registry. Address = zookeeper: / / 192.168.1.95:2181 # fixed/default dubbo. Protocol. The name = # dubbo dubbo port occupy can be modified Dubbo.protocol. port=20880 # Name of the package where the remote service to be scanned dubbo.scan.base-packages=com.byl. ServiceCopy the code
To avoid conflicts with port 8080, we’d better configure the service port number:
server.port=8081
Copy the code
Create the startup class and start the service provider:
If no error is reported and the following log appears, the service provider is successfully running (the service was successfully registered)!
At this point, we can through the dubbo management background dubbo – admin to view the service information, you can download a dubbo – admin war file, or to go on running the latest official gitbub dubbo – admin, In this example, dubo-admin-2.3.3.war!
After the download is successful, deploy it to Tomcat:
Start tomcat, and open the browser input: http://localhost:8080/dubbo-admin-2.5.3/
The default user name and password are root. After successful login:
All input we create a remote service path: com. Blog. Service. TestProviderService, and search for:
Success display provider: 192.168.1.95:20880!
At this point, open the Web module, the service consumer, and prepare to test the remote service:
Create a controller:
@RestController
@RequestMapping("/test")
public class TestController {
@DubboReference
TestProviderService testProviderService;
@GetMapping("/test")
public String test(a) {
String str = testProviderService.testProvider();
returnstr; }}Copy the code
Note: you must annotate the service with @dubboReference!
Configure the application. The properties:
server.port=8082 dubbo.application.name=byl_blog_web dubbo.registry.protocol=zookeeper Dubbo. Registry. Address = zookeeper: / / 192.168.1.95:2181 dubbo. Protocol. The name = dubbo dubbo, protocol, port = 20880Copy the code
Run WebApplication and type in a browser:http://localhost:8082/test/test Successful call!
Now look at dubo-admin’s consumer:Perfect run!
It should be noted that the service provider must run first, and then run the service consumer after the successful registration of the service. In addition, the port numbers should be careful not to conflict!
The project download address: download.csdn.net/download/ba…