赞
踩
目录
dubbo-admin是什么?
https://cn.dubbo.apache.org/zh-cn/overview/what/overview/ (官方文档内容,请阅读完)
maven、jdk、zookeeper、Node.js已经安装好。
1. maven需要配置环境变量,否则在 dubbo-admin\dubbo-admin-server 目录下执行 mvn 命令是会提示“'mvn' 不是内部或外部命令”。
2. Node.js官网下载:Node.js (nodejs.org)
下载好之后傻瓜式安装即可。安装好之后,任意位置打开cmd窗口执行如下两个命令进行验证是否安装成功即可(内置了npm)。
3. zookeeper启动正常运行
官方指导步骤:
1. dubbo-admin的下载:在github上搜索便可找到,然后下载zip包解压即可,或者使用git clone命令下载。下载地址如下:
GitHub - apache/dubbo-admin: The ops and reference implementation for Apache Dubbo
然后打开dubbo-admin-server/src/main/resources/application.properties这个文件修改注册中心地址与启动着的zookeeper保持一致(默认如下,不需要改):
- spring.profiles.active=dev
-
- server.port=38080
- dubbo.protocol.port=30880
- dubbo.application.qos-port=32222
-
- # centers in dubbo, if you want to add parameters, please add them to the url
- admin.registry.address=zookeeper://127.0.0.1:2181
- admin.config-center=zookeeper://127.0.0.1:2181
- admin.metadata-report.address=zookeeper://127.0.0.1:2181
-
- admin.root.user.name=root
- admin.root.user.password=root
2. 打包:进入dubbo-admin根目录后打开cmd窗口,执行如下命令进行打包。(之前踩坑了,一直在\dubbo\dubbo-admin\dubbo-admin-server这个目录下执行)
mvn clean package -Dmaven.test.skip=true
看到上面都是SUCCESS,表示打包成功了。
然后,可以进入各模块验证一下:
3. 运行jar。进入 dubbo-admin-distribution 或 dubbo-admin-server 的target目录,打开cmd窗口,执行命令:java -jar dubbo-admin-0.7.0-SNAPSHOT.jar 即可。
4. 打开浏览器,输出 http://localhost:38080 即可看到效果:
总结:
一、搭建这个浪费了不少时间,原因可能为:
(1)执行命令一直错误地在dubbo-admin-server目录下,而没有在根目录dubbo-admin下;
(2)dubbo-admin-ui模块打包一直出现问题,报错日志没看到关键位置,不仅要看cause by,更要看报错ERROR的提示信息:有个包一直下载不下来;
Central Repository: com/github/eirslett/frontend-maven-plugin/1.11.0 (apache.org)
解决方案之一:可以到上面那个仓库下载对应的jar并做替换。但是要到dubbo-admin-ui下的pom文件中注释掉下面这段代码:
<goals> <goal>npm</goal> </goals>(3)Node.js没有正确安装;
(4)下次如果再遇到问题,解决不了,可以参考以下文章:
- 解决:使用maven打包时dubbo-admin-ui构建失败 (Failed to execute goal com.github.eirslett:frontend-maven-plugin)_[error] failed to execute goal com.github.eirslett-CSDN博客
- 百度安全验证https://baijiahao.baidu.com/s?id=1763401634243321735&wfr=spider&for=pc
- dubbo-admin打包时报错的2个问题_dubbo could not extract the node archive: could no-CSDN博客
- 从零开始搭建zookeeper+dubboAdmin+dubbo遇到的情况
- dubbo-admin-ui模块 打包失败问题处理 - 简书
- 安装dubbo-admin报错node版本和test错误
- dubbo-admin安装、运行以及问题解决_启动dubbo-admin告知pid冲突-CSDN博客
- https://www.cnblogs.com/studyjobs/p/16345254.html
- 10分钟搞定Spring Boot + Dubbo + Dubbo admin UI一整套分布式解决方案 - 简书
参考官网网址:2 - Dubbo Spring Boot Starter 开发微服务应用 | Apache Dubbo
目标:从零上手开发基于 Dubbo 3.1.5 Spring Boot 的微服务开发,了解 Dubbo 3.1.5 Spring Boot 配置方式。(基于注解的方式)
环境:Windows、JDK8
项目结构:
如上所示,共有 3 个模块,其中 interface
模块被 consumer
和 provider
两个模块共同依赖,存储 RPC 通信使用的 API 接口。
具体代码编写步骤:
第一大步: 创建 dubbo-spring-boot-demo01 的maven工程作为父项目,并删除src文件夹,在父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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>org.wuya</groupId>
- <artifactId>dubbo-spring-boot-demo01</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>pom</packaging>
-
- <modules>
- <module>dubbo-spring-boot-demo-interface</module>
- <module>dubbo-spring-boot-demo-provider</module>
- <module>dubbo-spring-boot-demo-consumer</module>
- </modules>
-
- <properties>
- <dubbo.version>3.1.5</dubbo.version>
- <!-- 第一次忘记添加SpringBoot版本了,报错信息 org.springframework.boot:spring-boot-dependencies:pom:${spring-boot.version} was
- not found in https://repo.maven.apache.org/maven2 during a previous attempt. -->
- <spring-boot.version>2.7.8</spring-boot.version>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </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>
-
- <!-- Zookeeper -->
- <!-- NOTICE: Dubbo only provides dependency management module for Zookeeper, add Nacos or other product dependency directly if you want to use them. -->
- <dependency>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
- <version>${dubbo.version}</version>
- <type>pom</type>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>${spring-boot.version}</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
-
- </project>
第二大步:创建共享API模块 dubbo-spring-boot-demo-interface,创建UserService接口,UserService接口和pom文件中内容如下:
服务接口UserService 是 Dubbo 中沟通消费端和服务端的桥梁。 后续服务端发布的服务,消费端订阅的服务都是围绕着 UserService 接口展开的。
第三大步:创建服务提供方 dubbo-spring-boot-demo-provider 模块,并完善代码;
(1)pom.xml文件编写:
- <?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">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.wuya</groupId>
- <artifactId>dubbo-spring-boot-demo01</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <artifactId>dubbo-spring-boot-demo-provider</artifactId>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <!-- 记得导入依赖:这个公共API模块-->
- <dependency>
- <groupId>org.wuya</groupId>
- <artifactId>dubbo-spring-boot-demo-interface</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <!-- dubbo -->
- <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>
-
- <!-- spring boot starter -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- </dependencies>
- </project>
(2)定义服务端的实现:公共API模块中定义了服务接口UserService之后,可以在服务端这一侧定义对应的实现,这部分的实现相对于消费端来说是远端的实现,本地没有相关的信息。
- package org.wuya;
-
- import org.apache.dubbo.config.annotation.DubboService;
- import org.springframework.beans.factory.annotation.Value;
-
- import java.util.ArrayList;
- import java.util.List;
-
- @DubboService //暴露服务
- public class UserServiceImpl implements UserService{
-
- @Value("${server.port}")
- private String port;
-
- @Override
- public List<String> findAllAddrById(String userId) {
- List<String> addrList = new ArrayList<>();
- addrList.add("北京市昌平区6号");
- addrList.add("南京市钟鼓楼");
- System.out.println(this.port);//为了测试负载均衡
- return addrList;
- }
- }
(3)配置服务端 Yaml 配置文件
- server:
- port: 20880 # 默认就是20880,在dubbo-admin可视化管理中心可以看到
-
- dubbo:
- application:
- name: dubbo-spring-boot-demo-provider
-
- protocol:
- name: dubbo
- port: -1
-
- registry:
- # address: zookeeper://${zookeeper.address:127.0.0.1}:2181
- address: zookeeper://127.0.0.1:2181
(4)基于 Spring 配置服务端启动类:在这个启动类中,配置了一个 ProviderApplication
去读取我们前面定义的 application.yml
配置文件并启动应用。
- package org.wuya;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication()
- @EnableDubbo()
- public class ProviderApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProviderApplication.class,args);
- }
- }
第四大步:创建服务提供方 dubbo-spring-boot-demo-consumer 模块,并完善代码;
(1)编写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">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.wuya</groupId>
- <artifactId>dubbo-spring-boot-demo01</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <artifactId>dubbo-spring-boot-demo-consumer</artifactId>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <!-- 记得导入依赖:这个公共API模块-->
- <dependency>
- <groupId>org.wuya</groupId>
- <artifactId>dubbo-spring-boot-demo-interface</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <!-- dubbo -->
- <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>
-
- <!-- spring boot starter -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
-
- <!-- 除了这个依赖,其他的都与provider中的一模一样 -->
- <!-- web依赖,通过浏览器发http请求进行服务调用测试 -->
- <!-- @ResponseBody、@GetMapping、@RequestMapping 这些注解都是导入这个依赖才能使用 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.1.10.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
web依赖是多个依赖的集成,如下图:
(2)编写配置文件
server: port: 8081 dubbo: application: name: dubbo-spring-boot-demo-consumer protocol: name: dubbo port: -1 registry: # address: zookeeper://${zookeeper.address:127.0.0.1}:2181 address: zookeeper://127.0.0.1:2181 #关闭启动时检查 consumer: check: false
(3)编写 Service 和 controller 的代码
- package org.wuya.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.wuya.service.OrderService;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- //这种写法错误哦,会报错:javax.servlet.ServletException: No adapter for handler [org.wuya.controller.OrderController@6f7004d9]:
- // The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
- //@Controller("/order")
- @Controller
- @RequestMapping("/order")
- public class OrderController {
-
- @Resource
- private OrderService orderService;
-
- @GetMapping("/userId") //用@RequestMapping也行
- @ResponseBody
- public List<String> selectAddrByUserId(String userId){
- return orderService.selectAddrByUserId(userId);
- }
- }
通过@DubboReference
从 Dubbo 获取了一个 RPC 订阅,这个 userService
可以像本地调用一样直接调用。
(4)编写消费端启动类
- package org.wuya;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @EnableDubbo
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class,args);
- }
- }
至此,搭建完毕。
第五大步:验证。首先启动zk server端,接下来依次启动服务提供方启动类ProviderApplication 和 服务消费方启动类ConsumerApplication;然后在浏览器端输入以下url回车进行访问,查看效果:
完整访问路径:localhost:8081/order/userId?userId=3
只有一个参数时可以简写为:localhost:8081/order/userId?3
切记url格式为:(url与参数之间用?连接,参数名和参数值用=连接,多个参数用&连接)
协议名://ip+port+请求url?param1=value¶m2=value¶m3=value
效果:
D:\JavaTools\dubbo\dubbo-admin\dubbo-admin-distribution\target>java -jar dubbo-admin-0.7.0-SNAPSHOT.jar
通过上面命令行的方式启动dubbo-admin项目后,在浏览器端(http://localhost:38080/)打开控制台后,可以看到注册的服务。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。