Hello Spring Cloud Alibaba(六)之使用dubbo

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
  • 建立包类
    在这里插入图片描述

启动

先启动服务提供者、后启动消费者
在这里插入图片描述

说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值