其他文章
Nacos介绍和安装:https://blog.csdn.net/xyjcfucdi128/article/details/108355876
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wangmx.base</groupId>
<artifactId>dubbo-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-base</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>com.wangmx.base</groupId>
<artifactId>dubbo-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Nacos依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--服务接口-->
<dependency>
<groupId>com.wangmx.base</groupId>
<artifactId>dubbo-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Nacos依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#空
server:
port: 8862
dubbo:
# 配置服务信息
application:
name: dubbo-consumer
# 禁用QOS同一台机器可能会有端口冲突现象
qos-enable: false
qos-accept-foreign-ip: false
# 配置注册中心
registry:
address: nacos://127.0.0.1:8848
# 设置超时时间
consumer:
timeout: 4000
spring:
main:
# 解决Bean重复定义问题
allow-bean-definition-overriding: true
server:
port: 8861
dubbo:
# 配置服务信息
application:
name: dubbo-provider
# 禁用QOS同一台机器可能会有端口冲突现象
qos-enable: false
qos-accept-foreign-ip: false
# 配置注册中心
registry:
address: nacos://127.0.0.1:8848
# 设置协议-协议由提供方指定消费方被动接受
protocol:
name: dubbo
port: 20880
spring:
main:
# 解决Bean重复定义问题
allow-bean-definition-overriding: true
@SpringBootApplication
public class DubboBaseApplication {
public static void main(String[] args) {
SpringApplication.run(DubboBaseApplication.class, args);
}
}
/**
* 消费者
*/
@EnableDubbo //开启Dubbo的注解支持
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
/**
* 生产者
*/
@EnableDubbo //开启Dubbo的注解支持
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
entity
/**
* RPC接口DTO
* 注意这里要实现序列化接口
* @Author wangmx
*/
@Data
public class ProviderTestEntity implements Serializable {
// ID
private int id;
// 名字
private String name;
// 序号
private Integer number;
}
service
/**
* RPC接口
* @Author wangmx
*/
public interface IProviderService {
List<ProviderTestEntity> queryList();
}
vo
/**
* 返回数据
*
* @author wangmx
*/
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public R() {
put("code", 200);
put("msg", "success");
}
public static R error() {
return error(500, "未知异常,请联系管理员");
}
public static R error(String msg) {
return error(500, msg);
}
public static R error(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static R errorMassgae(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("data", new ArrayList<String>());
r.put("msg", msg);
return r;
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}
public static R ok() {
return new R();
}
@Override
public R put(String key, Object value) {
super.put(key, value);
return this;
}
}
controller
/**
* 消费测试接口
* @Author wangmx
*/
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
// Dubbo远程调用注解
@Reference
private IProviderService providerService;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public R getList(){
// 远程调用
List<ProviderTestEntity> list = providerService.queryList();
return R.ok().put("data",list);
}
}
impl
/**
* 生产者Dubbo接口实现
* @Author wangmx
*/
@Service
public class ProviderServiceImpl implements IProviderService {
@Override
public List<ProviderTestEntity> queryList() {
// 初始化数据
ProviderTestEntity testEntity1 = new ProviderTestEntity();
testEntity1.setId(1);
testEntity1.setName("张三");
testEntity1.setNumber(100);
ProviderTestEntity testEntity2 = new ProviderTestEntity();
testEntity2.setId(2);
testEntity2.setName("李四");
testEntity2.setNumber(101);
ProviderTestEntity testEntity3 = new ProviderTestEntity();
testEntity3.setId(2);
testEntity3.setName("王五");
testEntity3.setNumber(102);
// 组装数据
List<ProviderTestEntity> list = new ArrayList<>();
list.add(testEntity1);
list.add(testEntity2);
list.add(testEntity3);
return list;
}
}
我这里 Nacos 是 windows 版本的 ,不知道 Nacos 或者 没有的 可以看我 Nacos 介绍和安装
分别启动 dubbo-provide , dubbo-consumer 服务
查看 Nacos 服务列表
下载地址:https://github.com/2224132867/dubbo-nacos