Dubbo + ZooKeeper
16 Dubbo + ZooKeeper
16.1 Dubbo下载
GigHub下载地址:https://github.com/apache/dubbo-admin/tree/master-0.2.0
下载解压,点击进入dubbo-admin文件夹,在该路径下给项目打包
mvn package -Dmaven.skip.test=true //-Dmaven.skip.test表示不打包测试包
打包成功后,在项目的target目录下有打包成功的文件:dubbo-admin-0.0.1-SNAPSHOT.jar
16.2 ZooKeeper下载安装
ZooKeeper下载地址:https://archive.apache.org/dist/zookeeper/
下载解压,点击进入得到如下文件夹
在conf目录下添加zoo.cfg文件
直接复制一份zoo_sample.cfg,把文件名改为“zoo.cfg”即可
服务端与客户端
16.3 Dubbo+ZooKeeper测试
管理员身份启动ZooKeeper服务端
运行Dubbo,运行打包好的dubbo-admin-0.0.1-SNAPSHOT.jar文件
在dubbo-admin-0.0.1-SNAPSHOT.jar路径下,直接使用命令运行
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
在浏览器中打开:http://localhost:7001/
用户名:root
密码:root
16.4 服务注册发现实战
1、创建项目
创建一个Maven的Module名为dubbo+zookeeper,删掉多余的文件,在该Module下创建两个SpringBoot项目,分别为 provider-service , sonsumer-service
2、编写provider-service项目
导入相关依赖
<dependencies>
<!-- 导入依赖 dubbo + zookeeper-->
<!-- dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--日志会冲突-->
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.0</version>
</dependency>
<!-- curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.0</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
<!--排除这个slf4j-Log4j12-->
<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>
编写配置文件 application.properties
server.port=8001
# 服务应用名字
dubbo.application.name=provider-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#哪些服务要被注册
dubbo.scan.base-packages=com.dzj.service
编写业务接口以及实现类
文件路径:com--dzj-service--TicketService.java
package com.dzj.service;
public interface TicketService {
public String getTicket();
}
文件路径:com--dzj--service--TicketServiceImpl.java
package com.dzj.service;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
//服务注册与发现
//@DubboService //可以被扫描到,在项目启动就自动注册到注册中心
@Service
@Component
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "常回家看看";
}
}
打开我们的Zookeeper服务,启动运行provider-server项目,登录Dubbo
可以看到我们的项目已经注册到Dubbo注册中心
接下来我们就看看如何在consumer-server项目中如何去使用我们注册到注册中心上的方法
3、编写consumer-server
导入相关依赖
<dependencies>
<!-- 导入依赖 dubbo + zookeeper-->
<!-- dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--日志会冲突-->
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.0</version>
</dependency>
<!-- curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.0</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
<!--排除这个slf4j-Log4j12-->
<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>
编写配置文件application.properties
server.port=8002
#消费者去哪里拿服务,需要暴露自己的名字
dubbo.application.name=consumer-server
#注册中心的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
编写接口以及实现类
文件路径:com--dzj--service--TicketService.java
package com.dzj.service;
public interface TicketService {
public String getTicket();
}
文件路径:com--dzj-service--UserService.java
package com.dzj.service;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service //这里是放到容器中
public class UserService {
//想拿到provider-server提供的票,要去我们的注册中心拿到我们的服务
@Reference //引用,Pom坐标,可以定义路径相同的接口名
TicketService ticketService;
public void buyTicket(){
String ticket = ticketService.getTicket();
System.out.println("在注册中心拿到--->"+ticket);
}
}
在SpringBoot的测试类中调用测试
package com.dzj;
import com.dzj.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
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}
}
打开我们的Zookeeper服务,启动运行provider-server项目,启动运行测试类
查看结果,调用成功
本文来自博客园,作者:小公羊,转载请注明原文链接:https://www.cnblogs.com/aadzj/p/15636911.html