分布式:Dubbo+Zookeeper
分布式简单来说就是
利用更多的机器,来处理更多的数据
RPC是什么?
RPC指的是:远程过程调用
比如说:你在外面突然想到家里的煤气没关,于是你就打电话给家里人,叫他们关煤气。这一过程就是远程过程调用
Dubbo的使用
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向 接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
Dubbo环境搭建
http://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
配置过程可以看
(15条消息) Windows下安装Zookeeper教程, 解决安装的所有问题, 附3.4.14bin.tar.gz下载地址_MINKI_W的博客-CSDN博客
接下来,下载dubbo-admin-0.0.1-SNAPSHOT.jar
(15条消息) dubbo-admin-0.0.1-SNAPSHOT.jar 下载百度网盘自取_最佳听众!的博客-CSDN博客
先打开apache-zookeeper-3.5.10-bin\bin\zkservice.cmd
再打开dubbo-admin-0.0.1-SNAPSHOT.jar
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
再访问localhost:7001账号密码是root
就好了
springboot整合zookeeper+Dubbo实战
先创建provider-server(提供者)
pom.xml
<!-- zookeeperAndDubbo--> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- 引入zookeeper --> <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> <!--排除这个slf4j-log4j12--> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
package com.xflsh.service; public interface TicketService { public String getTicket(); }
package com.xflsh.service; import org.apache.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; @Service //将服务发布出去 @Component //放在容器中 public class TicketServiceImpl implements TicketService { @Override public String getTicket() { return "hello world.....dubbo"; } }
在springboot配置文件中配置dubbo相关属性
#当前应用名字 dubbo.application.name=provider-server #注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 #扫描指定包下服务 dubbo.scan.base-packages=com.xflsh.service server.port=8081
运行后,访问http://localhost:7001/ 的提供者
点击192.168.79.1:20880
就可以查看各种信息
接下来编写consumer-server(消费者)
pom.xml
<!--dubbo--> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!--zookeeper--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- 引入zookeeper --> <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> <!--排除这个slf4j-log4j12--> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
package com.xflsh.service; public interface TicketService { public String getTicket(); }
package com.xflsh.service; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service; @Service public class UserService { //我们需要去拿去注册中心的服务 @Reference//远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名 TicketService ticketService; public void bugTicket(){ String ticket = ticketService.getTicket(); System.out.println("在注册中心拿到===》" + ticket); } }
提示:这里的TicketService的地址一定要和provider-server(提供者)的包地址一样,不然匹配不到
在springboot配置文件中配置dubbo相关属性
#当前应用名字 dubbo.application.name=consumer-server #注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 server.port=8082
接下来写测试类
package com.xflsh; import com.xflsh.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.bugTicket(); } }
测试结果: