<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
寫在前面:在閱讀本文前,請前擁有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
在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
說明:
讀者請自行建立專案目錄(建立空專案,然後在空專案中新建三個module)
專案案例說明:業務假設場景=》 產品購買消費金額(consumer)同時並返回所有消費的總金額(需要呼叫到provider專案中服務實現)。
介面編寫
2.1.1.在com.dubbo.api.service(讀者請自行建立,下同,package建立將不一一贅述)包下建立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.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 { /** * 獲得總消費 * @param a * @return */ 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 */ @Service public class ProductServiceImpl implements ProductService { /** * 使用dubbo的註解 com.alibaba.dubbo.config.annotation.Reference。進行遠端呼叫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; /** * 產品controller */ @RestController public class ProductController { @Autowired private ProductService productService; /** * 新增完 返回總共消費 * @param a * @return */ @RequestMapping("/add") public String getCost(int a){ return "該產品總共消費 :"+productService.getCost(a); } }
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 { /** * 假設之前總花費了100 */ private final Integer totalCost = 1000; /** * 之前總和 加上 最近一筆 * @param cost * @return */ @Override public Integer add(int cost) { return totalCost + cost; } }
編寫第二步程式碼完成後 ,啟動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內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45