准备工作:Nacos
一、前置工作
安装Nacos,参考:Nacos 快速开始
此次安装 Nacos 最新版本:2.4.0 单机版
安装教程:Linux 安装 nacos 2.4.0-CSDN博客
二、创建配置文件
创建命名空间
新增配置文件
properties
dubbo:
application:
id: tyron-dubbo3-consumer
name: tyron-dubbo3-consumer
serialize-check-status: WARN
registry:
address: nacos://${nacos.config.server-addr}
parameters.namespace: ${nacos.config.namespace}
properties
server:
port: 5656
dubbo:
application:
id: tyron-dubbo3-provider
name: tyron-dubbo3-provider
serialize-check-status: WARN
protocol:
id: dubbo
name: dubbo
host: 127.0.0.1
port: 7788
serialization: hessian2
registry:
address: nacos://${nacos.config.server-addr}
parameters.namespace: ${nacos.config.namespace}
dubboParams: tyron
三、搭建项目
3.1、父 pom
properties
<?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>com.tyron</groupId>
<artifactId>dubbo3-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo3-provider</module>
<module>dubbo3-consumer</module>
<module>dubbo3-api</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring.boot.version>2.7.17</spring.boot.version>
<nacos.boot.version>0.2.12</nacos.boot.version>
<dubbo.version>3.2.9</dubbo.version>
</properties>
</project>
3.2、dubbo3-provider 服务提供者
pom.xml 文件,定义依赖
properties
<?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>dubbo3-demo</artifactId>
<groupId>com.tyron</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo3-provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- spring boot starter -->
<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>
<!--nacos配置中心依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos.boot.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>org.javassist</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- dubbo整合nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- 自定义api -->
<dependency>
<groupId>com.tyron</groupId>
<artifactId>dubbo3-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
配置文件 application.yaml :nacos的配置文件
properties
nacos:
config:
# 指定命名空间
namespace: tyron-dubbo
#配置服务地址
server-addr: 192.168.1.8:8848
#data-ids 为新增加的data-id
data-ids: dubbo3-provider
#配置类型
type: yaml
#是否启动刷新配置
autoRefresh: true
#运行时启用
bootstrap:
enable: true
定义接口实现,通过@NacosValue 读取nacos的配置文件数据
java
package com.tyron.service.impl;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.tyron.dubbo3.api.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@NacosValue(value = "${dubboParams}", autoRefreshed = true)
private String dubboParams;
@Override
public String getString() {
return dubboParams;
}
}
3.3、dubbo3-api 接口模块
dubbo 提供和调用本地方法一样的体验,所以需要提供api包
java
package com.tyron.dubbo3.api;
public interface DemoService {
String getString();
}
3.4、dubbo3-consumer 消费者
pom.xml 文件,定义依赖
同 dubbo3-provider 的定义:dubbo3-consumer/pom.xml
配置文件
properties
nacos:
config:
# 指定命名空间
namespace: tyron-dubbo
#配置服务地址
server-addr: 192.168.1.8:8848
#data-ids 为新增加的data-id
data-id: dubbo3-consumer
#配置类型
type: yaml
#是否启动刷新配置,开启之后可适应
autoRefresh: true
#运行时启用
bootstrap:
enable: true
消费者,调用api接口
java
package com.tyron.consumer;
import com.tyron.dubbo3.api.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoConsumer {
@DubboReference(version = "1.0.0", timeout = 1000)
private DemoService demoService;
@GetMapping("/tyron-test")
public String tyronTest() {
return demoService.getString();
}
}
四、项目启动
启动dubbo3-provider
启动dubbo3-consumer
本地接口调用,返回nacos配置的值。
更多进阶:Spring Boot 整合 Dubbo3 + Nacos 2.4.0【进阶】+ 踩坑记录