文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

springboot 集成dubbo的步骤详解

2024-04-02 19:55

关注

写在前面:在阅读本文前,请前拥有dubbo基础知识,springboot知识

dubbo官网: http://dubbo.apache.org
dubbo github 源码地址:https://github.com/apache/incubator-dubbo
dubbo 运维项目源码地址:https://github.com/apache/incubator-dubbo-ops

本文项目GitHub: https://github.com/Blankwhiter/dubbo-spring-boot-starter-test
源码对应版本1.0.1: https://github.com/Blankwhiter/dubbo-spring-boot-starter-test/archive/1.0.1.zip

第一步 搭建zookeeper环境

在centos窗口中,执行如下命令,拉取镜像,并启动zookeeper容器

docker pull zookeeper
docker run -d -v /home/docker/zookeeperhost/zookeeperDataDir:/data -v /home/docker/zookeeperhost/zookeeperDataLogDir:/datalog  -e ZOO_MY_ID=1 -e ZOO_SERVERS='server.1=125.77.116.145:2888:3888'  -p 2182:2181 -p 2888:2888 -p 3888:3888  --name zookeeper --privileged zookeeper

注:

1.zookeeper默认连接端口是2181 但本文测试用例时由于被其他程序占走,故使用2182。

2.读者请自行创建映射目录zookeeperDataDir | zookeeperDataLogDir

第二步 springboot集成dubbo

1.项目目录机构

在这里插入图片描述

说明:

读者请自行创建项目目录(创建空项目,然后在空项目中新建三个module)

项目案例说明:业务假设场景=》 产品购买消费金额(consumer)同时并返回所有消费的总金额(需要调用到provider项目中服务实现)。

2.代码编写

2.1 api目录

接口编写

2.1.1.在com.dubbo.api.service(读者请自行创建,下同,package创建将不一一赘述)包下创建CostService.java

package com.dubbo.api.service;

public interface CostService {
    
    Integer add(int cost);
}

2.1.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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dubbo</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>api</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 consumer目录 web访问、接口调用以及dubbo配置编写

2.2.1.引入 dubbo-spring-boot-starter 以及 上述的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dubbo</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>api</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2.2.在resources目录下 创建application.yml,并编写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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dubbo</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--引入api模块-->
        <dependency>
            <groupId>com.dubbo</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!--引入dubbo环境-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

    </dependencies>

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


</project>

2.2.3.使用 @EnableDubbo 注解开启dubbo
ConsumerApplication.java 启动类

package com.dubbo.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

2.2.4.编写产品service接口 ProductService.java

package com.dubbo.consumer.service;
public interface ProductService {
    
    Integer getCost(int a);
}

2.2.5.编写产品接口的实现,并调用远程服务CostService 。 ProductServiceImpl.java

package com.dubbo.consumer.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.api.service.CostService;
import com.dubbo.consumer.service.ProductService;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    
    @Reference
    private CostService costService;
    @Override
    public Integer getCost(int a) {
        return costService.add(a);
    }
}

2.2.6.编写访问类,ProductController.java

package com.dubbo.consumer.controller;
import com.dubbo.consumer.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    
    @RequestMapping("/add")
    public String getCost(int a){
        return "该产品总共消费 :"+productService.getCost(a);
    }
}

2.3 provider目录 api接口实现以及dubbo配置

2.3.1.引入 dubbo-spring-boot-starter 以及 上述的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dubbo</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>provider</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        <!--引入api-->
            <groupId>com.dubbo</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        <!--引入dubbo环境-->
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.3.2.在resources目录下 创建application.yml,并编写dubbo配置

dubbo:
  application:
    name: dubbo-provider
  registry:
    address: 125.77.116.145:2182
    # 读者请自行更改zookeeper地址
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: 30003
  monitor:
    protocol: register
  consumer:
    check: false
    timeout: 3000

server:
  port: 8061

2.3.3.使用 @EnableDubbo 注解开启dubbo
ConsumerApplication.java 启动类

package com.dubbo.provider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

2.3.3.编写CostService服务实现 CostServiceImpl.java

package com.dubbo.provider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.api.service.CostService;

@Service
public class CostServiceImpl implements CostService {
    
    private final Integer totalCost = 1000;
    
    @Override
    public Integer add(int cost) {
        return totalCost + cost;
    }
}

第三步 测试dubbo远程服务调用

编写第二步代码完成后 ,启动consumer项目,以及provider项目
在浏览器中访问 http://localhost:8062/add?a=100

在这里插入图片描述

出现如上结果即为调用成功

第四步 dubbo管理平台

dubbo运维旧版地址: https://github.com/apache/incubator-dubbo-ops/tree/master

1.本文将运维项目代码下载放于 D:learnplaceincubator-dubbo-ops

2.这里需要修改一个配置D:learnplaceincubator-dubbo-opsdubbo-adminsrcmain esourcesapplication.properties

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
server.port=8001
#服务器端口 server.port
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest
#访问的密码配置 spring.root.password  spring.guest.password
#dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.address=zookeeper://125.77.116.145:2182
#zookeeper地址

3.在D:learnplaceincubator-dubbo-opsdubbo-admin目录下 ,进入cmd窗口执行
mvn claen package 打包项目,
4.然后进入D:learnplaceincubator-dubbo-opsdubbo-admin arget ,进入cmd窗口执行
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar 运行项目
5.启动成功后 浏览器访问http://localhost:8001 输入账号:root / 密码:root 即可。

在这里插入图片描述

附录:

1.各个软件版本对应

versions

Java

Spring Boot

Dubbo

0.2.0

1.8+

2.0.x

2.6.2 +

0.1.1

1.7+

1.5.x

2.6.2 +

到此这篇关于springboot 简易集成dubbo的步骤详解的文章就介绍到这了,更多相关springboot 简易集成dubbo内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯