dubbo介绍
dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
dubbo使用
建立公共接口api模块
- 删除以下红框内的内容:
- 建包类
服务提供者
- 引入依赖
引入dubbo,common-api依赖,完整依赖如下:
<?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>top.musuixin</groupId>
<artifactId>hello-alibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-alibaba-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 表明这是一个spring-cloud应用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>top.musuixin</groupId>
<artifactId>hello-alibaba-common-api</artifactId>
<!-- 版本可以在通用依赖中设置-->
<version>0.0.1-SNAPSHOT</version>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 配置dubbo
配置如下:
spring:
cloud:
nacos:
config:
server-addr: 192.168.0.100:8848
file-extension: yaml
discovery:
server-addr: 192.168.0.100:8848
application:
name: hello-alibaba-provider
server:
port: 8030
dubbo:
protocol:
# 协议名称
name: dubbo
#服务端口
port: 7000
registry:
# nacos注册中心
address: nacos://192.168.0.100:8848
application:
name: hello-alibaba-provider
- 建立包类
- 开启dubbo
服务消费者
- 引入依赖,与提供者一样
- 配置文件
spring:
cloud:
nacos:
config:
server-addr: 192.168.0.100:8848
file-extension: yaml
discovery:
server-addr: 192.168.0.100:8848
sentinel:
transport:
#服务端的ip:port
dashboard: 192.168.0.100:8858
#这里要配置客户端的ip
client-ip: 192.168.0.1
application:
name: hello-alibaba-consumer
server:
port: 8000
dubbo:
protocol:
name: dubbo
port: 7010
registry:
# nacos注册中心
address: nacos://192.168.0.100:8848
application:
name: hello-alibaba-consumer
- 建立包类
启动
先启动服务提供者、后启动消费者