结合Dubbo官网学习如何完成SpringBoot+Dubbo+Zookeeper集成的学习,本次采用环境如下:
- 开发工具:IntelliJ IDEA Community Edition 2023.2.5 (IDEA社区版 2023.2.5)
- 开发环境:JDK 1.8,Windows 11
- Dubbo 3.2.0-beta.4,SpringBoot 2.7.8, Zookeeper 3.8.4
- 辅助程序:dubbo-admin-develop
Zookeeper
Zookeeper 下载
下载地址:https://zookeeper.apache.org/releases.html#news
下载解压完毕后找到apache-zookeeper-3.8.4-bin/bin目录启动zkServer.cmd和zkcli.cmd文件,测试是否能够联通
出现以下效果说明zookeeper测试成功
可能出现的问题
启动zkServer.cmd时出现闪退
排除方法:使用文本编辑器打开zkServer.cmd文件,在末尾添加pause,解除闪退查看原因
截断之后发现原因时在conf文件夹下缺少zoo.cfg文件,那么复制zoo_sample.cfg文件修改成zoo.cfg即可,当然也可以自己编辑cfg的内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
admin.serverPort=8888
辅助程序
dubbo-admin-develop 主要是进入到dubbo-admin页面,可以查看服务提供者提供了什么服务,启动后页面如下:
下载dubbo-admin
下载地址:https://gitee.com/apache-dubbo/dubbo-admin
dubbo-admin环境:Node.js v16.3.0,npm 7.15.1,Maven 3.8.8,JDK 1.8,Zookeeper 3.8.4
项目打包
首先需要完成前端工程,打开项目,进入dubbo-admin-ui文件,进入命令行模式(cmd),执行npm install
下载前端node_modules,完毕后再次运行命令npm run dev
完成前端工作后,回到dubbo-admin-develop文件夹,进入命令行模式(cmd),执行mvn package -Dmaven.test.skip=true
,打包整体项目,完成之后进入到
工程打包
常见问题
常见问题见:https://blog.csdn.net/qq_43780610/article/details/136622497?spm=1001.2014.3001.5501
SpringBoot集成Dubbo
由于本人使用的社区版,所以搭建Springboot的方式是创建Maven项目,然后通过Maven配置SpringBoot依赖,觉得麻烦的可以自己去SpringBoot官网下载或者使用非社区版。
项目依赖
<?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>org.example</groupId>
<artifactId>Dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>gmall-interface</module>
<module>dubbo-service-consumer</module>
<module>dubbo-service-provider</module>
</modules>
<properties>
<dubbo.version>3.2.0-beta.4</dubbo.version>
<spring-boot.version>2.7.8</spring-boot.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<!-- 引入公共api -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>3.2.0-beta.4</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
定义服务接口
服务接口 Dubbo 中沟通消费端和服务端的桥梁。
在gmall-interface
模块的com.chen.gmall.serviceImpl
创建接口类DemoService
package com.chen.gmall.service;
public interface DemoService {
String sayHello(String name);
}
服务端实现
服务端配置依赖
pom.xml
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>Dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dubbo-service-provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.properties
server.port=8080
# 在注册中心的服务名
dubbo.application.name=dubbo-springboot-service-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
# 注册中心地址
dubbo.registry.address=zookeeper://${zookeeper.address:127.0.0
代码实现
在dubbo-service-provider
模块的com.chen.gmall.serviceImpl
实现DemoServiceImpl类,类上使用@DubboService
注解,表示此类中的方法注册到注册中心中,消费者可以通过相同注解调用此类服务
注意:该实现类实现的接口是的gmall-interface
的接口类,要想能够引入需要在pom.xml中引入依赖
DemoServiceImpl.java
package com.chen.gmall.serviceImpl;
import com.chen.gmall.service.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
//注:在DemoServiceImpl 类中添加了 @DubboService 注解,通过这个配置可以基于 Spring Boot 去发布 Dubbo 服务
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
}
启动类 ProviderApplication
package com.chen.gmall;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
消费端实现
服务端配置依赖
pom.xml
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>Dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dubbo-service-consumer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
application.properties
server.port=8081
# 在注册中心的服务名
dubbo.application.name=dubbo-springboot-service-consumer
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
# 注册中心地址
dubbo.registry.address=zookeeper://${zookeeper.address:127.0.0.1}:2181
代码实现
在dubbo-service-consumer
模块的com.chen.gmall.serviceImpl
实现Task 类,使用@DubboReference
注入服务提供方提供的实现类,消费者可以使用这个类调用服务方该类下的所有方法
package com.chen.gmall.serviceImpl;
import com.chen.gmall.service.DemoService;
import com.chen.gmall.service.consumerTask;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@Component
public class Task implements consumerTask {
@DubboReference
DemoService demoService;
public void Task(){
String result = demoService.sayHello("Dubbo!");
System.out.println("result =======> " + result);
new Thread(() -> {
while (true){
try{
Thread.sleep(1000);
System.out.println("Thread Result ======> " + result);
}catch (Exception e){
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}).start();
}
}
启动类 ConsumerApplication
package com.chen.gmall;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class, args);
}
}
Controller
创建Controller的目的为了测试用,看看是否能够调通服务类方法
package com.chen.gmall.Controller;
import com.chen.gmall.serviceImpl.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
Task task;
@GetMapping("/demoTest")
public void demoTest() {
System.out.println(" ==========================Starting demo=========================");
task.Task();
}
}
接口类 consumerTask文章来源:https://www.toymoban.com/news/detail-840314.html
package com.chen.gmall.service;
import org.springframework.stereotype.Service;
@Service
public interface consumerTask {
public void Task();
}
启动
首先,需要启动Zookeeper,在启动服务端和消费端代码,随后启动dubbo-admin,查看是否存在dubbo-service-provider
的服务,如果有说明成功了,然后调用localhost:8081/demoTest
,查看是否输出结果
文章来源地址https://www.toymoban.com/news/detail-840314.html
到了这里,关于记录 Dubbo+Zookeeper 学习Demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!