dubbo+zookeeper+springboot远程连接,虚拟机和主机分布式操作
dubbo+zookeeper+springboot远程连接,虚拟机和主机分布式操作
springboot版本:阿里云2.3.7
实现目标
在主机上的消费者可以调用虚拟机中生产者的接口方法
项目目录
pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.2</version>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
生产者
生产者部署在服务器端/ubuntu20.04虚拟机
虚拟机开放2181端口与21880端口
sudo iptables -I INPUT -p tcp --dport 21880 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 2181 -j ACCEPT
官网下载zookeeper的压缩包,压缩包名字中要带bin,如apache-zookeeper-3.7.1-bin.tar.gz。
解压文件,进入apache-zookeeper-3.7.1-bin\conf,
在当前目录复制一个zoo_sample.cfg,并重命名为zoo.cfg。
进入apache-zookeeper-3.7.1-bin\bin目录,
运行zookeeper服务
appliacation.yml
# 应用名称
spring.application.name=provider-server
# 应用服务 WEB 访问端口
server.port=8281
# 注册 服务应用/ 中心 的地址
dubbo.application.name=provider-server
# 服务应用的名称
dubbo.registry.address=zookeeper://192.168.241.128:2181
# 哪些包要被注册
dubbo.scan.base-packages=com.jupiter.service
dubbo.protocol.port=21880
service
TicketService接口
public interface TicketService {
public String getTicket();
}
实现类
@DubboService(interfaceName = "com.jupiter.service.TicketService")//项目 一启动项目就注册到注册中心
@Component
public class TicketServiceImpl implements TicketService , Serializable {
@Override
public String getTicket() {
return "获得一张票"+ ", 消费者的请求: " + RpcContext.getContext().getRemoteAddress();
}
}
在目录
.......\provider-server路径下打开cmd使用mvn进行打包
mvn clean package
打包成功在文件路径下会出现一个target文件夹,
将其复制到虚拟机
进入虚拟机的target文件夹
在当前目录打开terminal,输入以及命令运行jar包,打开了生产者的服务
java -jar provider-server-0.0.1-SNAPSHOT.jar
消费者
消费者部署在主机端
application.yml
# 应用名称
spring.application.name=consumer-server
# 应用服务 WEB 访问端口
server.port=8182
# 消费者去哪里拿,需要暴露自己的名字
dubbo.application.name=consumer-server
# 注册中心的地址
dubbo.registry.address=zookeeper://192.168.241.128:2181
dubbo.protocol.port=21880
dubbo.consumer.check=false
application.properties
dubbo:
registry:
timeout: 200000
service:
timeout: 200000
consumer:
timeout: 200000
provider:
timeout: 200000
service
TicketService接口
如果要使@Reference注解生效,就需要有与被引用包的相同目录与接口
public interface TicketService {
public String getTicket();
}
消费方法
@Service
public class UserService {
//想拿到provider提供的票 要去注册中心拿到
@DubboReference(url = "dubbo://192.168.241.128:21880/")//引用,Pom坐标,路径相同的接口命
TicketService ticketService;
public String buyTicket(){
String ticket = ticketService.getTicket();
System.out.println("在注册中心获取==>"+ticket);
return ticket;
}
}
测试
@SpringBootTest
class ConsumerServerApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}
}
运行测试方法
运行结果