赞
踩
案例采用环境
一、本地zookeeper注册中心
采用本地的原因是通过docker pull下来的zookeeper个人不会使用,通过idea连接一直是没办法连接上的,通过docker logs查看日志信息,发现很多拒绝连接的情况,但是通过本地使用ZooInspector却能连接上,但是用idea本地连接没办法顺利连接,目前没有排错出来,后续如果有解决会放出解决方法,也看看有没有大神遇到同样的问题请教一下。
步骤:
1.首先启动本地zookeeper服务中心,文件夹路径的bin启动zkServer.cmd
二、linux下使用的dubbo-admin
步骤:
1.打开虚拟机进行docker操作(一定要先启动zookeeper后才使用dubbo-admin)
- #拉取镜像
- docker pull apache/dubbo-admin
-
- #运行镜像
- #{zookeeper_ip}:${zookeeper_port}输入自己服务所在的ip与端口 如:192.168.1.1:2181
- #映射到虚拟机的9000端口
- docker run -p 9000:8080 -d --name dubbo-admin \
- -e "admin.registry.address=zookeeper://{zookeeper_ip}:{zookeeper_port}" \
- -e "admin.config-center=zookeeper://{zookeeper_ip}:{zookeeper_port}" \
- -e "admin.metadata-report.address=zookeeper://{zookeeper_ip}:{zookeeper_port}" apache/dubbo-admin
-
- #查看运行状况
- docker ps
结果:访问虚拟机ip:9000
三、使用idea编写程序
1、文件结构
common | 放置domain、util等共同工具 |
dubbo-api | 需要暴露的服务接口 |
dubbo-donsumer | 消费者,测试远程服务接口 |
dubbo-provider | 服务提供者 |
2、pom依赖说明
父pom
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
- <modules>
- <module>service</module>
- <module>dubbo-api</module>
- <module>common</module>
- </modules>
- <groupId>com.fine</groupId>
- <artifactId>Spring-boot-dubbo-zookeeper-test02</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>Spring-boot-dubbo-zookeeper-test02</name>
- <description>springboot整合dubbo与zookeeper方案测试</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
- <dubbo.version>2.7.8</dubbo.version>
- <dubbo-zookeeper.version>2.7.8</dubbo-zookeeper.version>
-
- </properties>
- <dependencyManagement>
- <dependencies>
- <!-- springboot的依赖管理 -->
- <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-dependencies-bom</artifactId>
- <version>${dubbo.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- dubbo的zookeeper的依赖管理 -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-dependencies-zookeeper</artifactId>
- <version>${dubbo-zookeeper.version}</version>
- <type>pom</type>
- </dependency>
-
- <!--dubbo依赖-->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo</artifactId>
- <version>${dubbo.version}</version>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- <exclusion>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- </exclusion>
- <exclusion>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- <!--如果报错使用此镜像(官方说明) -->
- <repositories>
- <repository>
- <id>apache.snapshots.https</id>
- <name>Apache Development Snapshot Repository</name>
- <url>https://repository.apache.org/content/repositories/snapshots</url>
- <releases>
- <enabled>false</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- </repositories>
- </project>
service模块的pom(对于服务进行统一管理)
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>Spring-boot-dubbo-zookeeper-test02</artifactId>
- <groupId>com.fine</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>service</artifactId>
- <packaging>pom</packaging>
- <modules>
- <module>dubbo-provider</module>
- <module>dubbo-consumer</module>
- </modules>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <!-- web项目-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- dubbo-快速启动springboot -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>2.7.8</version>
- </dependency>
- <!-- 配置zookeeper-->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-dependencies-zookeeper</artifactId>
- <type>pom</type>
- </dependency>
- <dependency>
- <groupId>com.fine</groupId>
- <artifactId>common</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <!-- 自己配置需要暴露的api接口-->
- <dependency>
- <groupId>com.fine</groupId>
- <artifactId>dubbo-api</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- </dependencies>
-
- </project>
3、配置
provider配置(application.yml)
- spring:
- application:
- name: dubbo-provider
- dubbo:
- #服务所在包
- scan:
- base-packages: com.fine.dubbo.api
- #dubbo协议
- protocol:
- name: dubbo
- port: -1
- #dubbo注册中心
- registry:
- # client: curator
- address: 127.0.0.1:2181
- protocol: zookeeper
- application:
- name: dubbo-provider
- server:
- port: 11000
consumer配置
- spring:
- application:
- name: dubbo-consumer
- server:
- port: 12000
- dubbo:
- registry:
- protocol: zookeeper
- address: 127.0.0.1:2181
4、代码实现
api模块
provider模块
comsuner模块
4、启动服务
查看dubbo-admin的监控信息
测试访问
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。