本文将以Zookeeper作为服务的注册中心并使用dubbo进行服务通信实现一个简单的微服务demo:
Zookeeper搭建
本文仅使用Windows环境在本地搭建一个zookeeper进行demo的运行与测试,推荐使用镜像网站进行zookeeper下载:Index of /dist/zookeeper
- 下载zookeeper 3.6.4版本
- 下载后解压并将conf文件夹下面的zoo_sample.cfg复制一份改名为zoo.cfg,否则无法启动zookeeper
- 切换到bin目录下,双击zkServer.cmd文件即可启动zookeeper
初始化demo项目
- 如图所示,可以建立一个基础的项目。
搭建了基础项目之后,我们还需要创建
dubbo-interface
、dubbo-provider
和dubbo-consumer
三个子模块。
-
在初始化完项目以后,我们需要先添加 Dubbo 相关的 maven 依赖。
对于多模块项目,首先需要在父项目的
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>
<groupId>你的项目groupId</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-interface</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
<properties>
<dubbo.version>3.2.10</dubbo.version>
<spring-boot.version>2.7.8</spring-boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo -->
<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>${dubbo.version}</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>
然后在provider和consumer的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>你的项目groupId</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>你的项目groupId</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 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>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
- 在interface模块中定义demo服务接口
- 在provider模块中编写服务具体实现
- 在消费者模块编写服务调用任务
package com.liqi.consumer.task;
import com.liqi.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.UUID;
@Component
public class DemoTask implements CommandLineRunner {
@DubboReference
private DemoService demoService;
@Override
public void run(String... args) {
String providerResult = demoService.sayHello("服务提供者调用成功") + UUID.randomUUID().toString();
System.out.println(providerResult);
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(3000);
System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));
} catch (RpcException e) {
e.printStackTrace();
System.out.println("服务调用异常,线程终止");
Thread.currentThread().interrupt();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
}
- 分别在provider和consumer模块中添加springboot启动类(注意要添加@EnableDubbo注解)
package com.liqi.provider.impl;
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);
}
}
package com.liqi.consumer;
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);
System.out.println("消费者启动成功......");
}
}
- 在provider和consumer模块中的resource目录下添加springboot配置类application.yml
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
server:
# 注意消费者和服务提供者端口不能使用同一个
port: 自定义端口
- 先启动服务提供者provider,在启动服务消费者consumer(因为dubbo默认配置消费者启动会检查服务提供者是否可用不可用报错启动失败 ,顺序不能乱不然启动失败或者自己去修改配置),启动后可查看结果如下图,消费者成功调用了对应的实现并在控制台进行输出~
provider:
consumer: