介绍
-
Dubbo
是阿里巴巴公司开源的一个高性能、轻量级的Java RPC
框架。 -
Zookeeper
是一个开源的分布式的,为分布式应用提供协调服务的 Apache 项目。
这里是需要使用到Zookeeper
的,可以理解为Nacos
和OpenFeign
,需要把不同的服务注册到Zookeeper
然后不同服务之间需要相互调用,则需要使用Dubbo
。
下载安装
-
安装Zookeeper
-
下载链接:zookeeper
-
配置文件:
解压运行需要在
conf
目录下创建zoo.cfg
文件,其内容为:# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sakes.dataDir=D:/application/apache-zookeeper-3.8.0-bin/data# the port at which the clients will connectclientPort=10011admin.serverPort=2182
最主要修改的是
clientPort
,要指定其端口号 -
启动服务:
zkCli.cmd
-
-
安装dubbo-admin
-
下载链接:dubbo-admin
-
修改配置:
修改\dubbo-admin-server\src\main\resources 指定zookeeper地址
-
打包dubbo-admin:
mvn clean package -Dmaven.test.skip=true
-
运行jar包和前端
-
账号密码默认为root
-
代码部分
目录结构
pom内容
-
内容:
<properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.7.6</spring-boot.version><dubbo-version>3.2.0-beta.4</dubbo-version></properties><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><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>${dubbo-version}</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo-version}</version><type>pom</type></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper-curator5</artifactId><version>${dubbo-version}</version><type>pom</type></dependency></dependencies></dependencyManagement>
gmall-interface
public interface DemoService {String sayInfo(String name);
}
dubbo-service-provider
-
pom.xml
<dependencies><!-- spring boot starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>com.dragon</groupId><artifactId>gmall-interface</artifactId><version>0.0.1-SNAPSHOT</version><scope>compile</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper-curator5</artifactId><type>pom</type><exclusions><exclusion><artifactId>slf4j-reload4j</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency></dependencies>
-
application.properties
server.port=8080 # 注册中心的服务名 dubbo.application.name=dubbo-springboot-service-provider dubbo.protocol.name=dubbo dubbo.protocol.port=30881 dubbo.application.qos-port=32223# 注册中心地址 #zookeeper.address = 127.0.0.1dubbo.registry.address=zookeeper://${zookeeper.address:127.0.0.1}:10011 #端口为zookeeper设置的端口
-
具体代码
@DubboService //添加了 @DubboService 注解,通过这个配置可以基于 Spring Boot 去发布 Dubbo 服务 public class ServiceImpl implements DemoService {@Overridepublic String sayInfo(String name) {return "hello" + name;} }
注意:启动类需要添加注解:@EnableDubbo
dubbo-service-consumer
-
pom.xml
<dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper-curator5</artifactId><type>pom</type><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-reload4j</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>com.dragon</groupId><artifactId>gmall-interface</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
-
application.properties
server.port=8081 # 在注册中心的服务名 dubbo.application.name=dubbo-springboot-service-consumer dubbo.protocol.name=dubbo dubbo.protocol.port=-1 # 注册中心地址 dubbo.registry.address=zookeeper://${zookeeper.address:127.0.0.1}:10011
-
具体代码
@Component
public class Task implements ConsumerTask {@DubboReferenceprivate DemoService demoService;@Overridepublic void Task(){String result = demoService.sayInfo("Dubbo!");System.out.println("result =======> " + result);new Thread(() -> {while (true){try{Thread.sleep(1000);System.out.println("Thread Result ======> " + result);}catch (Exception e){e.printStackTrace();Thread.currentThread().interrupt();}}}).start();}
}
注意:启动类需要添加注解:@EnableDubbo