赞
踩
目录
二、SpringBoot整合Dubbo+zookeeper的demo
Dubbo官网地址:http://dubbo.apache.org
Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。
RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。
需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。
RPC是一个泛化的概念,严格来说一切远程过程调用手段都属于RPC范畴。各种开发语言都有自己的RPC框架。Java中的RPC框架比较多,广泛使用的有RMI、Hessian、Dubbo等。
Dubbo提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
节点角色说明:
节点 | 角色名称 |
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 |
调用关系说明:
0. 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Registry服务注册中心Zookeeper
通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官方推荐使用Zookeeper作为服务注册中心。
Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。
总的来说,dubbo就是一个远程调用服务,而zookeeper则是注册中心,就是将服务通过通过dubbo放入到zookeeper中,还能从dubbo中取出zookpper中的数据
链接:https://pan.baidu.com/s/1jD7P6qQEM4kSIbZS8B-YvQ?pwd=1234
提取码:1234
1、安装jdk(自己提前配好JDK)
2、解压zookeeper
一般安装包在 opt目录下,解压后将解压目录放在usr/local下
(1)将/usr/local/zookeeper/conf这个路径下的zoo_sample.cfg改为zoo.cfg
mv zoo_sample.cfg zoo.cfg
(2)打开zoo.cfg文件,修改dataDir路径
也就是数据存放的位置,一般在zookeeper下创建一个zkData目录
- cd /usr/local/zookeeper/
- mkdir zkData
- ll
- cd zkData
- pwd #获取全路径
- cd conf
- vim zoo.cfg
- 将文件里边的dataDir路径修改为制定路径
- dataDir=上面的路径
(1)启动Zookeeper的服务端
- cd /usr/local/zookeeper/bin
- #启动当先zookeeper
- ./zkServer.sh start
- #查看进程是否启动
- jps
- #查看状态
- ./zkServer.sh status
(2)启动zookeeper的客户端
- #还是进入zookeeper的ben目录
- #启动客户端
- ./zkCli.sh
- #查询当前的树结构
- ls /
- #quit退出
(3)停止zookeeper的服务端
- #进入zookeeper的bin
- ./zkServer.sh stop
- dataDir=/usr/local/zookeeper/zkData :保存zookeeper中的数据
-
- 注意:默认的tmp目录,是一个临时目录,一段时间里边的数据会被清空,所以不会用在tmp目录下
好了以上就是一个zookeeper的,调试通过之后就可以启动你的zookeeper啦
使用SpringBoot+dubbo+zookeeper
一台zookeeper,一个服务端9001,一个消费端8080
点击创建maven项目
- <properties>
- <dubbo.version>2.7.4.1</dubbo.version>
- <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
- </properties>
-
- <dependencyManagement>
- <dependencies>
- <!-- Spring Boot -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
- <!-- Dubbo -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-bom</artifactId>
- <version>${dubbo.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
-
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
-
- </build>
- public interface ZkService {
- public String ZkTest();
- }
- <dependencies>
-
- <!--引入自己创建的interface-->
- <dependency>
- <groupId>com.atdession</groupId>
- <artifactId>dubbo-zookeeper-interface</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-dependencies-zookeeper</artifactId>
- <version>2.7.4.1</version>
- <type>pom</type>
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- dubbo 2.7.x引入-->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.4.1</version>
- </dependency>
-
- <!-- spring boot starter -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- </dependencies>
- dubbo:
- registry:
- address: zookeeper://自己虚拟机上IP:2181 #自己的zookeeper服务器的IP:默认端口号
- application:
- name: dubbo-zookeeper-producer9001 #注册进去的名字
- protocol:
- name: dubbo #设置类型
- port: -1 #因为dubbo的服务器端口号是不能唯一的,所以,设置为-1会帮我们自动改变端口号
- config-center:
- timeout: 120000 #超时时间 (毫秒)
-
- server:
- port: 9001
- @SpringBootApplication
- @EnableDubbo(scanBasePackages = "com.atdession.service")
- public class Main9001 {
- public static void main(String[] args) {
- SpringApplication.run(Main9001.class,args);
- }
- }
ZkServiceImpl实现类
- import com.atdession.service.ZkService;
- import org.apache.dubbo.config.annotation.Service;
-
- //注意这个是dubbo下的Service
- @Service(version = "1.0.0")
- public class ZkServiceImpl implements ZkService {
-
-
- @Override
- public String ZkTest() {
- return "我是生产者";
- }
- }
-
- <dependencies>
- <!-- 自己创建的接口-->
- <dependency>
- <groupId>com.atdession</groupId>
- <artifactId>dubbo-zookeeper-interface</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <!-- web-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!-- dubbo-zookeeper-->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-dependencies-zookeeper</artifactId>
- <version>2.7.4.1</version>
- <type>pom</type>
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- dubbo 2.7.x引入-->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.4.1</version>
- </dependency>
-
-
- <!--测试-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- </dependency>
-
- </dependencies>
- dubbo:
- application:
- name: dubbo-zookeeper-consumer8080
- protocol:
- name: dubbo
- port: -1
- registry:
- address: zookeeper://自己的虚拟机上的IP:2181 #默认端口号为2181
- config-center:
- timeout: 12000 #超时时间
- server:
- port: 8080
- @SpringBootApplication
- @EnableDubbo(scanBasePackages = "com.atdession.controller")
- public class Main8080 {
- public static void main(String[] args) {
- SpringApplication.run(Main8080.class,args);
- }
- }
- @RestController
- public class SumerController {
-
- //注意:是dubbo中的Reference
- @Reference(version = "1.0.0")
- ZkService zkService;
-
-
- @RequestMapping("/zkTest")
- public String zkTest(){
-
- String s = zkService.ZkTest();
- System.out.println(s);
- return "是生产者吗?"+s;
-
- }
-
-
- }
注意:先开启生产者,在开启消费者,否则会报错:在zookeeper找不到该接口的错误
好啦,详细的整已经实现啦,快去努力研究专研你的项目去吧,加油!
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。