springboot?集成dubbo的步驟詳解
寫在前面:在閱讀本文前,請(qǐng)前擁有dubbo基礎(chǔ)知識(shí),springboot知識(shí)
dubbo官網(wǎng): http://dubbo.apache.org
dubbo github 源碼地址:https://github.com/apache/incubator-dubbo
dubbo 運(yùn)維項(xiàng)目源碼地址:https://github.com/apache/incubator-dubbo-ops
本文項(xiàng)目GitHub: https://github.com/Blankwhiter/dubbo-spring-boot-starter-test
源碼對(duì)應(yīng)版本1.0.1: https://github.com/Blankwhiter/dubbo-spring-boot-starter-test/archive/1.0.1.zip
第一步 搭建zookeeper環(huán)境
在centos窗口中,執(zhí)行如下命令,拉取鏡像,并啟動(dòng)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默認(rèn)連接端口是2181 但本文測(cè)試用例時(shí)由于被其他程序占走,故使用2182。
2.讀者請(qǐng)自行創(chuàng)建映射目錄zookeeperDataDir | zookeeperDataLogDir
第二步 springboot集成dubbo
1.項(xiàng)目目錄機(jī)構(gòu)

說(shuō)明:
- 1.api目錄:存放消費(fèi)者與提供者調(diào)用的service接口
- 2.consumer目錄:消費(fèi)者目錄 調(diào)用提供者遠(yuǎn)程提供的接口實(shí)現(xiàn)
- 3.provider目錄:提供者目錄 提供給消費(fèi)者接口實(shí)現(xiàn)
讀者請(qǐng)自行創(chuàng)建項(xiàng)目目錄(創(chuàng)建空項(xiàng)目,然后在空項(xiàng)目中新建三個(gè)module)
項(xiàng)目案例說(shuō)明:業(yè)務(wù)假設(shè)場(chǎng)景=》 產(chǎn)品購(gòu)買消費(fèi)金額(consumer)同時(shí)并返回所有消費(fèi)的總金額(需要調(diào)用到provider項(xiàng)目中服務(wù)實(shí)現(xiàn))。
2.代碼編寫
2.1 api目錄
接口編寫
2.1.1.在com.dubbo.api.service(讀者請(qǐng)自行創(chuàng)建,下同,package創(chuàng)建將不一一贅述)包下創(chuàng)建CostService.java
package com.dubbo.api.service;
public interface CostService {
/**
* 成本增加接口
* @param cost
* @return
*/
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訪問、接口調(diào)用以及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目錄下 創(chuàng)建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環(huán)境-->
<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 啟動(dòng)類
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.編寫產(chǎn)品service接口 ProductService.java
package com.dubbo.consumer.service;
public interface ProductService {
/**
* 獲得總消費(fèi)
* @param a
* @return
*/
Integer getCost(int a);
}
2.2.5.編寫產(chǎn)品接口的實(shí)現(xiàn),并調(diào)用遠(yuǎn)程服務(wù)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;
/**
* 產(chǎn)品service
*/
@Service
public class ProductServiceImpl implements ProductService {
/**
* 使用dubbo的注解 com.alibaba.dubbo.config.annotation.Reference。進(jìn)行遠(yuǎn)程調(diào)用service
*/
@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;
/**
* 產(chǎn)品controller
*/
@RestController
public class ProductController {
@Autowired
private ProductService productService;
/**
* 添加完 返回總共消費(fèi)
* @param a
* @return
*/
@RequestMapping("/add")
public String getCost(int a){
return "該產(chǎn)品總共消費(fèi) :"+productService.getCost(a);
}
}
2.3 provider目錄 api接口實(shí)現(xiàn)以及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環(huán)境-->
<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目錄下 創(chuàng)建application.yml,并編寫dubbo配置
dubbo:
application:
name: dubbo-provider
registry:
address: 125.77.116.145:2182
# 讀者請(qǐng)自行更改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 啟動(dòng)類
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服務(wù)實(shí)現(xiàn) CostServiceImpl.java
package com.dubbo.provider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.api.service.CostService;
/**
* 花費(fèi)服務(wù)
*/
@Service
public class CostServiceImpl implements CostService {
/**
* 假設(shè)之前總花費(fèi)了100
*/
private final Integer totalCost = 1000;
/**
* 之前總和 加上 最近一筆
* @param cost
* @return
*/
@Override
public Integer add(int cost) {
return totalCost + cost;
}
}
第三步 測(cè)試dubbo遠(yuǎn)程服務(wù)調(diào)用
編寫第二步代碼完成后 ,啟動(dòng)consumer項(xiàng)目,以及provider項(xiàng)目
在瀏覽器中訪問 http://localhost:8062/add?a=100

出現(xiàn)如上結(jié)果即為調(diào)用成功
第四步 dubbo管理平臺(tái)
dubbo運(yùn)維舊版地址: https://github.com/apache/incubator-dubbo-ops/tree/master
1.本文將運(yùn)維項(xiàng)目代碼下載放于 D:learnplaceincubator-dubbo-ops
2.這里需要修改一個(gè)配置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 #服務(wù)器端口 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目錄下 ,進(jìn)入cmd窗口執(zhí)行
mvn claen package 打包項(xiàng)目,
4.然后進(jìn)入D:learnplaceincubator-dubbo-opsdubbo-admin arget ,進(jìn)入cmd窗口執(zhí)行
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar 運(yùn)行項(xiàng)目
5.啟動(dòng)成功后 瀏覽器訪問http://localhost:8001 輸入賬號(hào):root / 密碼:root 即可。

附錄:
1.各個(gè)軟件版本對(duì)應(yīng)
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 +
到此這篇關(guān)于springboot 簡(jiǎn)易集成dubbo的步驟詳解的文章就介紹到這了,更多相關(guān)springboot 簡(jiǎn)易集成dubbo內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Iterator與ListIterator迭代的區(qū)別
本文主要介紹了Java中Iterator與ListIterator迭代的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
SpringBoot項(xiàng)目如何連接MySQL8.0數(shù)據(jù)庫(kù)
這篇文章主要介紹了SpringBoot項(xiàng)目如何連接MySQL8.0數(shù)據(jù)庫(kù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Json字符串轉(zhuǎn)Java對(duì)象和List代碼實(shí)例
這篇文章主要介紹了Json字符串轉(zhuǎn)Java對(duì)象和List代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Mybatis如何使用動(dòng)態(tài)語(yǔ)句實(shí)現(xiàn)批量刪除(delete結(jié)合foreach)
這篇文章主要介紹了Mybatis如何使用動(dòng)態(tài)語(yǔ)句實(shí)現(xiàn)批量刪除(delete結(jié)合foreach),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

