2024.6.11 Tuesday
Following 【WEEK15】 【DAY4】Dubbo and Zookeeper Integration Part 1【English Version】
Contents
18. Dubbo and Zookeeper Integration
18.3. Setting Up the Test Environment
18.3.4. Installing dubbo-admin on Windows
Dubbo itself is not a service software. It is actually a jar package that helps your Java program connect to Zookeeper and use Zookeeper to consume and provide services. However, to better manage and monitor numerous Dubbo services, the official team provides a visual monitoring program, dubbo-admin. Though, the monitoring program is optional.
18.3.4.1. Download dubbo-admin
https://github.com/apache/dubbo-admin/tree/develop
Now the official website only has the develop version. The master version can be downloaded from this link: https://github.com/wikerx/dubbo-admin-master
Proceed with the subsequent operations using the master version.
18.3.4.2. Extract and Enter the Directory
Modify the dubbo-admin\src\main\resources\application.properties file to specify the Zookeeper address (if not modified earlier, no need to modify it now).
server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest
# Zookeeper address for the registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
18.3.4.3. Package dubbo-admin in the Project Directory
Run:
mvn clean package -Dmaven.test.skip=true
18.3.4.4. Execute dubbo-admin-0.0.1-SNAPSHOT.jar in dubbo-admin\target Directory
Run:
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
At this point, the Zookeeper service must be running, otherwise it cannot be queried.
After execution, visit http://localhost:7001/, enter the default username and password (both are root).
After clicking “Login”:
You can check the system logs:
18.4. SpringBoot + Dubbo + Zookeeper
18.4.1. Framework Setup
18.4.1.1. Create an Empty Project Dubbo_zookeeper
18.4.1.2. Create a New Module provider-server
Add web support:
Delete unnecessary files:
18.4.1.2.1. Create a service folder and add TicketService.java and TicketServiceImpl.java
18.4.1.2.2. TicketService.java
package com.P60.service;
public interface TicketService {
public String getTicket();
}
18.4.1.2.3. TicketServiceImpl.java
package com.P60.service;
public class TicketServiceImpl implements TicketService {
@Override // Override method using alt+insert
public String getTicket() {
return "Dubbo+Zookeeper";
}
}
18.4.1.3. Create a New Module consumer-server
Add web support:
Delete unnecessary files:
18.4.1.3.1. Create a service folder and add UserService.java
package com.P60.service;
public interface UserService {
// Need to retrieve the ticket from the provider-server in the registry center
}
18.4.2. Modify provider-server (Service Provider)
18.4.2.1. Import Dependencies
To register the service provider to the registry center, we need to integrate Dubbo and Zookeeper, so we need to import the necessary packages.
18.4.2.1.1. Dubbo Spring Boot Starter
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter/2.7.3
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
18.4.2.1.2. ZooKeeper Client
https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
18.4.2.2. Modify pom.xml
As usual, modify the Maven, JDK, and Java version settings in the Project Structure. Change the springframework version to 2.7.13 in the pom.xml file and reload Maven.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>provider-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-server</name>
<description>provider-server</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!--Dubbo+zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper client-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--zookeeper server and framework packages-->
<!-- Include zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--Exclude slf4j-log4j12 to resolve logging conflicts-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
18.4.2.3. Modify application.properties
# Refer to the configuration file in D:\dubbo-admin-master-master\dubbo-admin\src\main\resources\application.properties
spring.application.name=provider-server
server.port=8001
# Current application name
dubbo.application.name=provider-server
# Registry center address
dubbo.registry.address=zookeeper://127.0.0.1:2181
# Scan services in the specified package (need to register)
dubbo.scan.base-packages=com.P60.service
18.4.2.4. Modify TicketServiceImpl.java
package com.P60.service;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
// Avoid using the Service annotation due to name conflict between Spring and Dubbo
@Service // Makes the project scan and automatically register to the registry
@Component // Store in the container
public class TicketServiceImpl implements TicketService {
@Override // alt+insert to override method
public String getTicket() {
return "Dubbo+Zookeeper";
}
}
18.4.2.5. Run ProviderServerApplication
Remember to run zkServer.cmd first, and it can be accessed through dubbo-admin-0.0.1-SNAPSHOT.jar.
Visit http://localhost:7001/
Select “Service Governance” -> “Providers”
See “Providers”:
Click 172.18.20.16:20880 to view the provider
18.4.3. Service Consumer
18.4.3.1. Modify pom.xml (similar to provider)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consumer-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-server</name>
<description>consumer-server</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!--Dubbo+zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper client-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--zookeeper server and framework packages-->
<!-- Include zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--Exclude slf4j-log4j12 to resolve logging conflicts-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
18.4.3.2. Modify application.properties
spring.application.name=consumer-server
server.port=8002
# Consumers need to expose their names to get services
dubbo.application.name=consumer-server
# Address of the registry center, can be on any computer
dubbo.registry.address=zookeeper://127.0.0.1:2181
18.4.3.3. Modify UserService.java
package com.P60.service;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service // Just call the interface and inject it into the container
public class UserService { // Cannot be written as an Interface because interfaces are abstract and cannot call abstract classes
// Need to get the ticket from provider-server in the registry center -> Remote reference to the specified service, match by full class name, see who registered this full class name to the registry center
@Reference // Reference: two solutions (1. use pom coordinates; 2. define the same interface name path)
// Here we use the second method: directly copy TicketService from provider-server to consumer-server
TicketService ticketService;
public void buyTicket(){
String ticket = ticketService.getTicket();
System.out.println("Retrieved from registry center: " + ticket);
}
}
18.4.3.4. Modify and run ConsumerServerApplicationTests.java
package com.P60;
import com.P60.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerServerApplicationTests {
@Autowired // Inject into local application using Autowired instead of Reference
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}
}
This is the application of SpringBoot + dubbo + zookeeper to achieve distributed development, essentially a service splitting concept.