一、下载apache-zookeeper-3
1、下载地址:https://zookeeper.apache.org/releases.html 2、启动 找到bin文件下的zkServer.cmd,双击即可(保持运行)
二、下载dubbo管理页面(可有可无)
1、地址:https://github.com/apache/dubbo-admin/tree/master-0.2.0 2、解压,用idea打开下载相对应的依赖,然后打包 在dubbo-admin的target可找到打包好的jar包 3、命令行运行启动
打开命令行,切换到jar包所在路径,输入执行命令
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
4、访问路径
localhost:7001
用户名:root
密码:root
三、建立provider项目,提供接口
1、建立maven项目,勾选web依赖 2、引入dubbo和zookeeper依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
3、编写配置文件
server.port=8001
dubbo.application.name=provider-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=nuc.ss.service
4、service层
public interface TicketService {
String getTicket();
}
5、实现类impl 这里必须使用dubbo的service,否则dubbo上查找不到该provider
@Service
@Component
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "《dubbo+zookeeper测试,从服务端获取》";
}
}
6、启动provider项目 刷新localhost:7001,可以看到服务已被注册进去
四、customer项目
1、建立maven项目,勾选web依赖 2、引入dubbo+zookeeper依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
3、编写配置文件application.properties
server.port=8002
dubbo.application.name=consumer-server
dubbo.registry.address=zookeeper://127.0.0.1:2181
4、构造一个与provider完全相同service(包括包路径和命名)
public interface TicketService {
String getTicket();
}
5、编写userService调用ticketService,注意使用指引注入
@Service
public class UserService {
@Reference
TicketService ticketService;
public void bugTicket(){
String ticket = ticketService.getTicket();
System.out.println("在注册中心买到"+ticket);
}
}
6、测试类测试
@SpringBootTest
class ConsumerServerApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
userService.bugTicket();
}
}
看到如下输出,即构造成功
|