工程结构:
版本信息:
jdk版本:1.8
springboot-parent版本:2.6.6
springboot版本:2.6.6
dubbo版本:3.0.7
curator版本:4.2.0
dubbo-registry-nacos版本:3.0.7
注意事项:正确的版本很重要,否则会报莫名其妙的错误!!!
- 本地启动zookeeper服务/nacos服务(略)
- 新建父工程testDubbo
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.example.testDubbo</groupId>
<artifactId>testDubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>testProvider</module>
<module>testConsumer</module>
<module>api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<properties>
<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>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.7</version>
</dependency>
<!--zookeeper配置1或2都可以-->
<!--zookeeper配置1-->
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-dependencies-zookeeper</artifactId>-->
<!-- <version>3.0.7</version>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
<!--zookeeper配置2-->
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-registry-zookeeper</artifactId>-->
<!-- <version>3.0.7</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>3.0.7</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 新建子工程api
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>testDubbo</artifactId>
<groupId>org.example.testDubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api</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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
定义DTO和Provider接口:
package org.example.testDubbo.api.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.Random;
@Data
public class UserDTO implements Serializable {
private Long id;
private String name;
private Integer age;
public static UserDTO mockUser(Long id){
UserDTO userDTO = new UserDTO();
userDTO.setId(id);
userDTO.setAge(18);
userDTO.setName("张三_"+userDTO.getId());
return userDTO;
}
}
package org.example.testDubbo.api.provider;
import org.example.testDubbo.api.dto.UserDTO;
public interface UserProvider {
UserDTO getById(Long id);
}
- 新建子工程testProvider
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>testDubbo</artifactId>
<groupId>org.example.testDubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>testProvider</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>
<dependency>
<groupId>org.example.testDubbo</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-dependencies-zookeeper</artifactId>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-registry-zookeeper</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
</dependencies>
</project>
配置文件application.yml:
dubbo:
application:
name: testProvider
qos-port: 22221
registry:
# address: zookeeper://localhost:2181
address: nacos://localhost:8848
group: registry_group_testDubbo_service
protocol:
id: dubbo
name: dubbo
port: 8088
scan:
base-packages: org.example.testDubbo.testProvider.provider
实现provider接口:
package org.example.testDubbo.testProvider.provider;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.testDubbo.api.dto.UserDTO;
import org.example.testDubbo.api.provider.UserProvider;
@DubboService
public class UserProviderImpl implements UserProvider {
@Override
public UserDTO getById(Long id) {
System.out.println("我是服务提供者,被调用了");
return UserDTO.mockUser(id);
}
}
- 新建子工程testConsumer
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>testDubbo</artifactId>
<groupId>org.example.testDubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>testConsumer</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>
<dependency>
<groupId>org.example.testDubbo</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>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-spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-dependencies-zookeeper</artifactId>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo-registry-zookeeper</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
</dependencies>
</project>
配置文件application.yml:
server:
port: 80
dubbo:
application:
name: testConsumer
qos-port: 22222
registry:
# address: zookeeper://localhost:2181
address: nacos://localhost:8848
group: registry_group_testDubbo_service
protocol:
id: dubbo
name: dubbo
port: 8088
# consumer:
# check: false
测试类Controller:
package org.example.testDubbo.testConsumer.controller;
import org.apache.dubbo.config.annotation.DubboReference;
import org.example.testDubbo.api.dto.UserDTO;
import org.example.testDubbo.api.provider.UserProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@DubboReference
private UserProvider userProvider;
@GetMapping(value = "/getById")
public UserDTO getById(@RequestParam Long id){
return userProvider.getById(id);
}
}
- 部署dubbo-admin
8.1 下载源码:https://github.com/apache/dubbo-admin.git
8.2 由于本测试dubbo版本是3.0.7,因此我拉取了靠近该版本的git版本:2353c814
8.3 修改子工程dubbo-admin-server的配置文件:application.properties
……
admin.registry.address=zookeeper://localhost:2181
admin.config-center=zookeeper://localhost:2181
admin.metadata-report.address=zookeeper://localhost:2181
admin.registry.group=registry_group_testDubbo_service
admin.config-center.group=registry_group_testDubbo_service
admin.metadata-report.group=registry_group_testDubbo_service
……
8.4 启动子工程dubbo-admin-server(默认会占用端口8080,用于ui工程访问,可修改该端口)
8.5 启动ui工程dubbo-admin-ui,启动命令:
npm run dev
启动成功后会打印访问地址:http://localhost:8082/
8.6 如需修改dubbo-admin-server端口,需要同时修改2处:
dubbo-admin-server的配置文件application.properties:
server.port=8989
dubbo-admin-ui的配置文件vue.config.js:
module.exports = {
……
proxy: {
'/': {
target: 'http://localhost:8989/',
……