0


springboot集成dubbo+zookeeper简单搭建(细到你没有感觉)

dubbo搭建过程中,请注意各依赖版本号,如果不知道版本对应,请跟下文保持一致,否则可能出现冲突,在你看到此文章之前,请确保你的zookeeper已经搭建完毕

1.创建项目

1.创建根(root)项目

选择maven,你也可以选择spirng initializr创建

我的父工程的名字是 dubbo_ex 根据自己的需要更改即可

创建完成之后,会得到一个只有src目录的项目,删除src(父工程不需要写代码)

pom中需要注意的注释,已经在下面代码中进行注释

给小白提一嘴:统一的包管理,<abc>1.2.3</abc>,这种标签是自定义的,不是什么标准,可以等于EL表达式的取值方式(可能是我太菜了,刚学maven,认为这种有固定的写法)

pom.xml

<?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>
    <!-- 此处是springboot的父工程,用于指定springboot 版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.fmh</groupId>
    <artifactId>dubbo_ex</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <!-- 此处无需手动添加,创建模块时会自动加入,如果创建完所有模块,此处没有自动加入,需要手动加入子模块名称-->
    <modules>
        <module>dubbo-api</module>
        <module>procedure</module>
        <module>consumer</module>
    </modules>
    <!-- 统一版本管理 -->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <zk.version>3.6.2</zk.version>
        <dubbo.version>2.7.7</dubbo.version>
        <curator.framework.version>5.1.0</curator.framework.version>
        <curator.recipes.version>5.1.0</curator.recipes.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zk.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${curator.framework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>${curator.recipes.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

2.创建模块

此处你也可以选择spring init,我们选择maven工程

注意此处截图圈红的位置

pom.xml (该pom什么也没改变,该模块无需添加dubbo等依赖)

<?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>dubbo_ex</artifactId>
        <groupId>com.fmh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

我们在刚刚创建的模块中,随意写一个接口,如下图:

3.创建消费者

创建步骤跟第二步一样的步骤,不再演示,名字随意

pom.xml

你的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo_ex</artifactId>
        <groupId>com.fmh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>procedure</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
        <!-- zookeeper和dubbo的集成包 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <!-- zookeeper和dubbo的集成包 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>

        <!-- 刚刚创建的api接口模块 -->
        <dependency>
            <groupId>com.fmh</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

application.properties


server.port=8082
## 注册中心 zookeeper的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.application.name=provider-ticker
# dubbo扫描的包
dubbo.scan.base-packages=com.fmh.service

dubbo.registry.timeout=20000

dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

实现接口,注意导入的包(@Component可以替换为@Service,不过要注意引入的包是否是spring的)

@Component
@DubboService(version = "1.0")
public class UserServiceImpl implements UserService {
    @Override
    public String hello(String str) {
        return str;
    }
}

springboot启动类

这个代码就不给出了

至此,服务提供者创建完毕

4.消费者模块创建

跟 2小节一样的步骤

pom.xml

注意引入自己的模块

<?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>dubbo_ex</artifactId>
        <groupId>com.fmh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fmh</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>

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

       
    </dependencies>
</project>

application.properties 配置文件


server.port=8083
dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.application.name=provider-consumer

dubbo.scan.base-packages=com.fmh.controller

dubbo.registry.timeout=20000

dubbo.protocol.name=dubbo-consumer
dubbo.protocol.port=20881

创建controller

@RestController
public class UserController {

    @DubboReference(version = "1.0")
    private UserService userService;

    @GetMapping("/user")
    public String user() {
        return userService.hello("只因你太美");
    }
}

创建springboot启动类(这个代码就不需要了吧,上面有给出哦)

然后启动项目

这个提示不用管,是因为zk或者dubbo没有排除log4j的组件,不影响

用postman调用接口,即可看到结果

另外 dubbo-admin可以可视化看到接口注册情况,看反馈情况决定是否更新,感谢您的阅读!

如果有问题,欢迎留言


本文转载自: https://blog.csdn.net/weixin_44231805/article/details/126144762
版权归原作者 一条不会写作的码农 所有, 如有侵权,请联系我们删除。

“springboot集成dubbo+zookeeper简单搭建(细到你没有感觉)”的评论:

还没有评论