首先,使用maven建立父工程dubbozookeeper,pom.xml配置如下:
<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">
<groupId>org.examplegroupId>
<artifactId>dubbozookeeperartifactId>
<version>1.0-SNAPSHOTversion>
<modelVersion>4.0.0modelVersion>
<packaging>pompackaging>
<modules>
<module>consumer-clientmodule>
<module>provider-servermodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.5.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
dependencies>
project>
在父工程下建立module工程provider-server,pom.xml配置如下:
<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">
<parent>
<groupId>org.examplegroupId>
<artifactId>dubbozookeeperartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>provider-serverartifactId>
<dependencies>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>5.1.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>5.1.0version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.6.2version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
新建org.example.ProviderServerApplication类
package org.example;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo(scanBasePackages = "org.example.service")
@SpringBootApplication
public class ProviderServerApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderServerApplication.class, args);
}
}
新建org.example.service.TicketService类
package org.example.service;
public interface TicketService {
String getTicket();
}
新建org.example.service.TicketServiceImpl类
package org.example.service;
import org.apache.dubbo.config.annotation.DubboService;
/**
* zookeeper:服务注册与发现
*/
@DubboService//可以被扫描到,在项目启动时就自动注册到注册中心
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "一张票";
}
}
新建application.yml文件
server:
port: 8002
#服务应用名字
dubbo:
application:
name: provider-server
#注册中心地址
registry:
address: zookeeper://127.0.0.1:2181
#被注册的服务
scan:
base-packages:
- org.example.service
接着在父工程下建立module工程consumer-client,pom.xml配置如下:
<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">
<parent>
<groupId>org.examplegroupId>
<artifactId>dubbozookeeperartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>consumer-clientartifactId>
<dependencies>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>5.1.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>5.1.0version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.6.2version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
新建org.example.ConsumerClientApplication类
package org.example;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo(scanBasePackages = "org.example.service")
@SpringBootApplication
public class ConsumerClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerClientApplication.class, args);
}
}
新建org.example.service.TicketService类
package org.example.service;
public interface TicketService {
String getTicket();
}
新建org.example.service.UserService类
package org.example.service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service//放到容器中
public class UserService {
/**
* 想拿到provider-server提供的服务,要去注册中心拿到服务
*/
@DubboReference//引用,pom坐标,可以定义路径相同的接口名
TicketService ticketService;
public void buyTicket() {
String ticket = ticketService.getTicket();
System.out.println("在注册中心获得:" + ticket);
}
}
新建application.yml文件
server:
port: 8001
#消费者取服务需要暴露自己名字
dubbo:
application:
name: consumer-client
#注册中心地址
registry:
address: zookeeper://127.0.0.1:2181
最后是测试类:test/java/org.example.ConsumerServerApplicationTest
package org.example;
import org.example.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerServerApplicationTest {
@Autowired
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}
}
执行过程:
①官网下载apache-zookeeper-3.6.2-bin.tar并解压进入apache-zookeeper-3.6.2-bin目录
②进入conf目录,复制zoo_sample.cfg到相同目录下并更名为zoo.cfg
③启动bin目录下的zkServer.cmd
④启动ProviderServerApplication的main方法
⑤启动ConsumerClientApplication的main方法
⑥启动ConsumerServerApplicationTest的contextLoads方法,控制台输出结果