Dubbo(一)之简单应用

为什么要用Dubbo

  • 服务治理框架
  • 服务的监控
  • 服务的注册发现
  • 服务的通信
  • 服务的容错
  • 服务的负载均衡

Dubbo架构图

在这里插入图片描述

1. Dubbo的简单使用

  1. 构建两个项目(服务提供者、服务消费者)
  2. 构建一个公共的接口模块,两个项目都依赖此接口
  3. maven中引入Dubbo包
  4. 配置dubbo配置文件(发布的ip和端口以及服务接口)
    • 可以选择配置注册中心,或者选择不配置注册中心,直连
  5. 远程调用

2. Dubbo项目搭建

2.1 创建项目

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2.2 创建模块

先删除项目中多余的src文件夹

2.2.1 创建api模块

用maven模板创建
作为对外提供接口的模块
在这里插入图片描述

2.2.2 创建provider模块

在这里插入图片描述

2.2.3 创建consumer模块

步骤同上

创建完成后,项目结构如下
在这里插入图片描述

2.3 调整项目

删除无用文件
在这里插入图片描述
将consumer子模块pom文件中的一些公共管理模块放到父模块pom中
在这里插入图片描述
在这里插入图片描述

在父pom文件中添加模块
在这里插入图片描述
在子模块中添加父模块

配置文件
provider项目中的application.properties

# 应用名称
spring.application.name=spring-cloud-dubbo-sample-provider

dubbo.scan.base-packages=com.xhc.springcloud.dubbo.springclouddubbosampleprovider.service

dubbo.protocol.port=20882
dubbo.protocol.name=dubbo

spring.cloud.nacos.discovery.server-addr=47.93.8.54:8848

consumer项目中的application.properties

# 应用名称
spring.application.name=spring-cloud-dubbo-sample-consumer
spring.cloud.nacos.discovery.server-addr=47.93.8.54:8848

provider的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">

    <parent>
        <groupId>com.xhc.springcloud.dubbo</groupId>
        <artifactId>spring-cloud-dubbo-example</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xhc.springcloud.dubbo</groupId>
    <artifactId>spring-cloud-dubbo-sample-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-dubbo-sample-provider</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.xhc.springcloud.dubbo</groupId>
            <artifactId>spring-cloud-dubbo-sample-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.3.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>
                        com.xhc.springcloud.dubbo.springclouddubbosampleprovider.SpringCloudDubboSampleProviderApplication
                    </mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

consumer的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">

    <parent>
        <groupId>com.xhc.springcloud.dubbo</groupId>
        <artifactId>spring-cloud-dubbo-example</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xhc.springcloud.dubbo</groupId>
    <artifactId>spring-cloud-dubbo-sample-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-dubbo-sample-consumer</name>
    <description>Demo project for Spring Boot</description>

    <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>com.xhc.springcloud.dubbo</groupId>
            <artifactId>spring-cloud-dubbo-sample-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</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.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>
                        com.xhc.springcloud.dubbo.springclouddubbosampleconsumer.SpringCloudDubboSampleConsumerApplication
                    </mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

在这里插入图片描述
在这里插入图片描述
将provider和consumer分别启动
调用localhost:8080 使用dubbo成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值