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
注意事项:正确的版本很重要,否则会报莫名其妙的错误!!!
<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.0modelVersion>
<groupId>org.example.testDubbogroupId>
<artifactId>testDubboartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>testProvidermodule>
<module>testConsumermodule>
<module>apimodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.6version>
parent>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>3.0.7version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<version>3.0.7version>
dependency>
dependencies>
dependencyManagement>
project>
<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>testDubboartifactId>
<groupId>org.example.testDubbogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>apiartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
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);
}
<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>testDubboartifactId>
<groupId>org.example.testDubbogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>testProviderartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.example.testDubbogroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
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);
}
}
<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>testDubboartifactId>
<groupId>org.example.testDubbogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>testConsumerartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.example.testDubbogroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
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);
}
}
……
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/',
……