本文环境:
springboot:2.1.12
apache dubbo:2.7.1
nacos:1.0.0
项目代码:
https://github.com/ROAOR1/dubbo-nacos.git
节点 | 角色说明 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 |
调用关系说明
1、服务容器负责启动,加载,运行服务提供者。
2、服务提供者在启动时,向注册中心注册自己提供的服务。
3、服务消费者在启动时,向注册中心订阅自己所需的服务。
4、注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
5、服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
6、服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Nacos是阿里巴巴最新开源的项目,核心定位是“一个更易于帮助构建云原生应用的动态服务发现、配置和服务管理平台”,nacos下载:https://github.com/alibaba/nacos/releases
我这里使用的是1.0.0版本,下载完成之后解压启动即可
下面开始创建我们的项目,贴上我的目录结构
public-api公共接口模块(接口),供服务消费者和服务提供者调用。
service-base服务提供者模块(接口实现类),引入了public-api模块
web服务消费者模块(controller),引入了public-api模块
消费者和提供者通过公共接口模块进行rpc远程调用
父工程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 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.1.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.top</groupId>
<artifactId>dubbo-nacos</artifactId>
<version>1.0.0</version>
<name>dubbo-nacos</name>
<description>dubbo-nacos</description>
<packaging>pom</packaging>
<modules>
<module>public-api</module>
<module>service-base</module>
<module>web</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<dubbo.version>2.7.1</dubbo.version>
<nacos.version>1.0.0</nacos.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.top</groupId>
<artifactId>dubbo-nacos</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>public-api</artifactId>
<version>1.0.0</version>
<name>public-api</name>
<description>api公用接口</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
公共接口模块里面只有一个接口,没有配置文件,打jar包
public interface InfoService {
String getInfo();
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.top</groupId>
<artifactId>dubbo-nacos</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>service-base</artifactId>
<version>1.0.0</version>
<name>service-base</name>
<description>base模块</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入公共接口模块-->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>public-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--duboo+nacos-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件
server:
port: 8080
spring:
application:
name: service-base
dubbo:
registry:
address: nacos://127.0.0.1:8848 #注册地址
application:
name: service-base #应用名
protocol:
name: dubbo #dubbo协议
port: 20880 #协议端口
scan:
base-packages: com.top.service.base.impl #扫包范围
provider:
timeout: 30000 #超时时间
service:
version: 1.0.0 #自定义的版本
接口实现类,该类实现了上面我们在公共接口模块创建的接口
//dubbo提供的Service注解,用于声明对外暴露服务
@Component
@Service(version = "${service.version}")
public class InfoServiceImpl implements InfoService {
@Override
public String getInfo() {
return "hello,这里是service-base模块!";
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.top</groupId>
<artifactId>dubbo-nacos</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>web</artifactId>
<version>1.0.0</version>
<name>web</name>
<description>web模块</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入公共接口模块-->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>public-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--duboo+nacos-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件
server:
port: 8090
spring:
application:
name: web
dubbo:
registry:
address: nacos://127.0.0.1:8848 #注册地址
application:
name: web #应用名
consumer:
timeout: 30000 #超时时间
service:
version: 1.0.0 #自定义的版本,和消费者模块保持一致
controller,调用公共接口模块创建的接口
@RestController
public class InfoController {
//dubbo提供的Reference注解,用于调用远程服务
@Reference(version = "${service.version}",check = false)
private InfoService infoService;
@RequestMapping("/getInfo")
public String getInfo(){
return infoService.getInfo();
}
}
启动nacos,启动服务提供者和服务消费者,调用服务消费者的getInfo方法,服务提供者会返回结果
登上nacos控制台可以看到服务消费者与服务提供者
http:127.0.0.1:8848/nacos/index.html,账号和密码都是nacos