Hello Spring Cloud Alibaba(六)之使用dubbo
dubbo介绍dubbo使用建立公共接口api模块服务提供者服务消费者启动
说明
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>
<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>
<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:
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:
dashboard: 192.168.0.100
:8858
client-ip: 192.168.0.1
application:
name: hello
-alibaba
-consumer
server:
port: 8000
dubbo:
protocol:
name: dubbo
port: 7010
registry:
address: nacos
://192.168.0.100
:8848
application:
name: hello
-alibaba
-consumer
建立包类
启动
先启动服务提供者、后启动消费者
说明