一、版本与环境搭建:

写在前面:截至 2021-12-17 当前Dubbo最新包与Jdk 17还不适配 调试无果后采用Jdk jdk1.8.0_311完成验证

1.Dubbo官网:Apache Dubbo

2.Zookeeper官网:Download

3.Centos7部署Docker参考:菜鸟学院

安装完设置启动并设置开机自启:

systemctl start docker
systemctl enable docker

4.Docker官网:Docker

Centos7拉取Zookeeper命令,容器启动命令:

docker pull zookeeper:3.7.0

5.Linux服务器,百度云首次申领三个月免费试用:百度云服务器

6.最终还是手动搭建了Zookeeper环境,地址如下,大概3个月内都能用:

zookeeper://106.12.148.211:2181

springboot2.6.1
dubbo3.0.2.1
zookeeper3.7.0
jdk1.8.0_311

服务器如下:

在这里插入图片描述

二、聚合项目 RPC调用

项目结构,先上图:

在这里插入图片描述
启动服务,调用测试:

http://127.0.0.1:8090/test/hello?name=hongxu_moon
在这里插入图片描述

父项目配置:

<?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 http://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.6.1</version>
        <relativePath/>
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>zookeeper</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>service</module>
        <module>consumer</module>
        <module>provider</module>
    </modules>

    <properties>
        <java.version>8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!-- 增加dubbo版本号控制 -->
        <dubbo.version>3.0.2.1</dubbo.version>
        <spring.version>4.3.16.RELEASE</spring.version>
        <curator.version>5.1.0</curator.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- dubbo 依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
         <!--Zookeeper -->

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>

接口模块SERVICE:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>zookeeper</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <finalName>zhx_moon_service</finalName>
    </build>
</project>

接口类:

package com.demo.service;

/**
 * Description: 测试服务
 *
 * @Author: zhx & moon hongxu_1234@163.com
 * @Date: 2021-12-10 23:32
 * @version: V1.0.0
 */
public interface TestService {

    /**
     * 测试服务接口
     * @param name
     * @return
     */
    String test(String name);
}

生产者PROVIDER:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>zookeeper</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>zhx_moon_provider</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

实现类:

package com.demo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: 提供者启动
 *
 * @Author: zhx & moon hongxu_1234@163.com
 * @Date: 2021-12-16 1:38
 * @version: V1.0.0
 */
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

package com.demo.serviceImpl;

import com.demo.service.TestService;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * Description: 测试服务实现
 *
 * @Author: zhx & moon hongxu_1234@163.com
 * @Date: 2021-12-10 23:36
 * @version: V1.0.0
 */
@DubboService(version = "1.0.0",interfaceClass = TestService.class)
public class TestServiceImpl implements TestService {

    @Override
    public String test(String name) {
        return "Hello" + name;
    }
}

YML配置:

server:
  port: 8089

spring:
  application:
    name: auto_provider

dubbo:
  application:
    name: providers
    id: providers
  registry:
    address: zookeeper://106.12.148.211:2181
    timeout: 100000
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.demo.serviceImpl

消费者模块CONSUMERS:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>zookeeper</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

消费接口:

package com.demo;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: 消费者启动
 *
 * @Author: zhx & moon hongxu_1234@163.com
 * @Date: 2021-12-16 1:40
 * @version: V1.0.0
 */
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

package com.demo.controller;

import com.demo.service.TestService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: 消费者
 *
 * @Author: zhx & moon hongxu_1234@163.com
 * @Date: 2021-12-10 23:42
 * @version: V1.0.0
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @DubboReference(version = "1.0.0")
    TestService testService;

    @GetMapping("/hello")
    public String test(String name){
        return testService.test(name);
    }
}

YML配置:

server:
  port: 8090

spring:
  application:
    name: auto_consumer

dubbo:
  application:
    name: consumer
    id: consumer
  registry:
    address: zookeeper://106.12.148.211:2181
    timeout: 10000
  scan:
    base-packages: com.demo.controller
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐