Spring Boot 2.x 基礎案例:整合Dubbo 2.7.3+Nacos1.1.3(最新版)
1、概述
本文將介紹如何基於Spring Boot 2.x的版本,通過Nacos作為配置與註冊中心,實現Dubbo服務的註冊與消費。
整合元件的版本說明:
Spring Boot 2.1.9
Dubbo 2.7.3
Nacos 1.1.3
本文的亮點:
- 1.採用yml方式進行dubbo的配置。
- 2.相關元件採用較新版本進行整合。
- 3.相關原始碼放置於Github上,可隨時檢視。
原始碼放置Github: https://github.com/raysonfang/spring-boot-demo-all
之前公司在使用Dubbo 2.6.1的時候,採用Zookeeper作為註冊中心。當時,也只是僅僅拿來作為註冊中心使用,一沒有專門的管理後臺進行視覺化管理操作,二是功能單一,僅作為註冊中心使用。
經過一段時間的學習和了解以後,發現採用阿里開源的Nacos作為註冊中心與外部配置中心。它比Zookeeper更適合做服務的註冊與配置,畢竟是大廠開源,經過大量實踐。
如果不清楚Nacos是什麼,或具有什麼主要功能以及架構設計思想。自行花點時間查一下資料。
Nacos:
注:此次主要實踐Nacos作為註冊中心,後面會單獨整合Nacos作為配置中心的實踐分享。
2、基礎框架搭建
使用idea+maven多模組進行專案搭建
spring-boot-dubbo-nacos-demo:父工程
shop-service-provider: dubbo服務提供者
shop-service-consumer: dubbo服務消費者,是一個web工程
3、pom.xml說明
spring-boot-dubbo-nacos-demo:父工程的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.raysonblog</groupId> <artifactId>misco-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>misco-dubbo</name> <packaging>pom</packaging> <description>Demo project for Spring Boot Dubbo Nacos</description> <modules> <module>shop-service-provider</module> <module>shop-service-consumer</module> </modules> <properties> <java.version>1.8</java.version> <spring-boot.version>2.1.9.RELEASE</spring-boot.version> <dubbo.version>2.7.3</dubbo.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>apache.snapshots.https</id> <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
shop-service-provider: pom.xml引入dubbo與nacos相關的jar
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.raysonblog</groupId>
<artifactId>misco-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>cn.raysonblog</groupId>
<artifactId>shop-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop-service-provider</name>
<description>服務者 Demo project for Spring Boot dubbo nacos</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自帶的logback依賴 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- Dubbo Registry Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
shop-service-consumer: 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.raysonblog</groupId>
<artifactId>misco-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>cn.raysonblog</groupId>
<artifactId>shop-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shop-service-consumer</name>
<description>Demo project for Spring Boot dubbo nacos</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除自帶的logback依賴 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- Dubbo Registry Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>cn.raysonblog</groupId>
<artifactId>shop-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、配置檔案說明
網上大部分資料都是基於application.properties配置,或者是基於xml配置dubbo的相關引數。而實際上,在Spring Boot工程中,
shop-service-provider:application.yml配置檔案說明
spring:
application:
name: shop-service-provider
# log config
logging:
config: classpath:log4j2.xml
level:
root: info
web: info
file: logs/shop-service-provider.log
# Dubbo Application nacos
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
nacos:
service-address: 127.0.0.1
port: 8848
dubbo:
registry:
address: nacos://${nacos.service-address}:${nacos.port}
protocol:
name: dubbo
port: 20881
scan:
base-packages: cn.raysonblog.*.service.impl
shop-service-consumer: application.yml說明
server:
address:
port: 8081
servlet:
context-path: /
tomcat:
uri-encoding: UTF-8
spring:
application:
name: shop-service-consumer
# log config
logging:
config: classpath:log4j2.xml
level:
root: info
web: info
file: logs/shop-service-provider.log
# Dubbo Application nacos
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
nacos:
service-address: 127.0.0.1
port: 8848
dubbo:
registry:
address: nacos://${nacos.service-address}:${nacos.port}
dubbo相關引數:也可以在DubboConfigurationProperties
類中檢視
5、編寫業務程式碼
shop-service-provider:程式碼實現
針對dubbo服務提供者,我沒有單獨把RpcShopService
介面單獨放到一個子模組提供,建議在引用到實際專案中,可以單獨提供介面包,在消費端直接引用介面包,這樣就可以脫離服務提供者。
ShopServiceProviderApplication.java
啟動主類
package cn.raysonblog.shopserviceprovider;
import org.apache.dubbo.config.spring.context.annotation.DubboConfigConfiguration;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.concurrent.CountDownLatch;
/**
* dubbo 服務提供方
* @author raysonfang
* @公眾號 Java技術乾貨(ID:raysonfang)
*/
@SpringBootApplication
@EnableDubbo
public class ShopServiceProviderApplication {
//使用jar方式打包的啟動方式
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws InterruptedException{
SpringApplication.run(ShopServiceProviderApplication.class, args).registerShutdownHook();
countDownLatch.await();
}
}
需要注意@EnableDubbo
這個註解,是開啟Dubbo服務必要的。
RpcShopService.java
:暴露介面,供消費端使用
package cn.raysonblog.shopserviceprovider.service;
/**
* 提供暴露的Rpc介面
* @author raysonfang
*/
public interface RpcShopService {
String sayHello(String name);
}
ShopServiceImpl.java
: 實現類
package cn.raysonblog.shopserviceprovider.service.impl;
import cn.raysonblog.shopserviceprovider.service.RpcShopService;
import org.apache.dubbo.config.annotation.Service;
/**
* 介面實現類
*
* ## @Service 這個註解是使用dubbo提供的,
* 這個註解中有很多屬性,需要單獨瞭解去進行配置
*
* @author raysonfang
*/
@Service
public class ShopServiceImpl implements RpcShopService {
public String sayHello(String name) {
return name;
}
}
shop-service-consumer: 程式碼實現
package cn.raysonblog.shopserviceconsumer;
import cn.raysonblog.shopserviceprovider.service.RpcShopService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
*
* 把主類和controller寫在一起,方便簡單測試演示。
*
* @author raysonfang
*/
@SpringBootApplication
@RestController
public class ShopServiceConsumerApplication {
@Reference
RpcShopService shopService;
@RequestMapping(name = "/sayHello", method = RequestMethod.GET)
public String sayHello(){
return shopService.sayHello("Hello Dubbo Nacos!更多原創分享,技術交流,關注:Java技術乾貨(ID:raysonfang)");
}
public static void main(String[] args) {
SpringApplication.run(ShopServiceConsumerApplication.class, args);
}
}
6、測試
測試的時候,啟動順序
6.1、啟動nacos-server註冊中心
下載nacos-server:https://github.com/alibaba/nacos/releases
解壓nacos-server, 找到bin目錄
windows點選startup.cmd, 啟動nacos
在瀏覽器中輸入:http://localhost:8848/nacos/index.html
, 便可以訪問到nacos的控制檯。使用者名稱和密碼預設為nacos
6.2、啟動shop-service-provider服務提供者
在nacos控制檯可以看到資訊:
6.3、啟動shop-service-consumer服務消費者
在nacos控制檯可以看到如下資訊:
在瀏覽器端輸入:http://localhost:8081/sayHello, 便會返回結果。
7、問題記錄及解決
7.1、在整合的時候,pom引入dubbo及nacos相關依賴包,花費時間蠻多。主要是包引入不成功。
解決:去maven的本地依賴庫中,刪除引入不成功的依賴包,在重新reimport。
7.2、 在開啟dubbo的時候,註解引用不正確:錯誤注入@EnableDubboConfig
。
解決: 換成使用@EnableDubbo
。
7.3、yml配置Dubbo的相關屬性,網上資料蠻少的。
解決:通過檢視DubboConfigurationProperties.java
原始碼,去分析屬性配置。
8、後記
由於能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!
原始碼放置Github: https://github.com/raysonfang/spring-boot-demo-all
歡迎大家star, 批評
我平常學習,編碼也都會放置github上,歡迎持續關注交流。
我的github: https://github.com/raysonfang
相關推薦
Spring Boot 2.x 基礎案例:整合Dubbo 2.7.3+Nacos1.1.3(最新版)
1、概述 本文將介紹如何基於Spring Boot 2.x的版本,通過Nacos作為配置與註冊中心,實現Dubbo服務的註冊與消費。 整合元件的版本說明: Spring Boot 2.1.9 Dubbo 2.7.3 Nacos 1.1.3 本文的亮點: 1.採用yml方式進行dubbo的配置。 2.
Spring Boot 2.x 基礎案例:整合Dubbo 2.7.3+Nacos1.1.3(配置中心)
本文原創首發於公眾號:Java技術乾貨 1、概述 本文將Nacos作為配置中心,實現配置外部化,動態更新。這樣做的優點:不需要重啟應用,便可以動態更新應用裡的配置資訊。在如今流行的微服務應用下,將應用的配置統一管理,顯得尤為重要。 上一篇寫了《Spring Boot 2.x 基礎案例:整合Dubbo
Spring Boot 2.x基礎教程:快速入門
開發十年,就只剩下這套架構體系了! >>>
Spring Boot 2.x基礎教程:工程結構推薦
Spring Boot框架本身並沒有對工程結構有特別的要求,但是按照最佳實踐的工程結構可以幫助我們減少可能會遇見的坑,尤其是Spr
Spring Boot 2.x基礎教程:構建RESTful API與單元測試
首先,回顧並詳細說明一下在快速入門中使用的@Controller、@RestController、@RequestMapping註
Spring Boot 2.x基礎教程:使用Swagger2構建強大的API文件
隨著前後端分離架構和微服務架構的流行,我們使用Spring Boot來構建RESTful API專案的場景越來越多。通常我們的一個RESTful API就有可能要服務於多個不同的開發人員或開發團隊:IOS開發、Android開發、Web開發甚至其他的後端服務等。為了減少與其他團隊平時開發期間的頻繁溝通成本,傳
Spring Boot 2.x基礎教程:JSR-303實現請求引數校驗
請求引數的校驗是很多新手開發非常容易犯錯,或存在較多改進點的常見場景。比較常見的問題主要表現在以下幾個方面: 僅依靠前端框架解決引數校驗,缺失服務端的校驗。這種情況常見於需要同時開發前後端的時候,雖然程式的正常使用不會有問題,但是開發者忽略了非正常操作。比如繞過前端程式,直接模擬客戶端請求,這時候就會突然在
Spring Boot 2.x基礎教程:Swagger介面分類與各元素排序問題詳解
之前通過Spring Boot 2.x基礎教程:使用Swagger2構建強大的API文件一文,我們學習瞭如何使用Swagger為Spring Boot專案自動生成API文件,有不少使用者留言問了關於文件內容的組織以及排序問題。所以,就特別開一篇詳細說說Swagger中文件內容如何來組織以及其中各個元素如何控制
Spring Boot 2.x基礎教程:Swagger靜態文件的生成
前言 通過之前的兩篇關於Swagger入門以及具體使用細節的介紹之後,我們已經能夠輕鬆地為Spring MVC的Web專案自動構建出API文件了。如果您還不熟悉這塊,可以先閱讀: Spring Boot 2.x基礎教程:使用Swagger2構建強大的API文件 Spring Boot 2.x基礎教程:Swa
Spring Boot 2.X(十五):整合 Swagger2 開發 API 文件(線上+離線)
前言 相信很多後端開發在專案中都會碰到要寫 api 文件,不管是給前端、移動端等提供更好的對接,還是以後為了以後交接方便,都會要求寫 api 文件。 而手寫 api 文件的話有諸多痛點: 文件更新的時候,需要再次傳送給對接人 介面太對,手寫文件很難管理 介面返回的結果不明確 不能直接線上測試介面,通常需要使
Spring Boot 2.X(十八):整合 Spring Security-登入認證和許可權控制
前言 在企業專案開發中,對系統的安全和許可權控制往往是必需的,常見的安全框架有 Spring Security、Apache Shiro 等。本文主要簡單介紹一下 Spring Security,再通過 Spring Boot 整合開一個簡單的示例。 Spring Security 什麼是 Spring Se
Spring Boot 2.x基礎教程:使用JdbcTemplate訪問MySQL資料庫
在第2章節中,我們介紹瞭如何通過Spring Boot來實現HTTP介面,以及圍繞HTTP介面相關的單元測試、文件生成等實用技能。但是,這些內容還不足以幫助我們構建一個動態應用的服務端程式。不論我們是要做App、小程式、還是傳統的Web站點,對於使用者的資訊、相關業務的內容,通常都需要對其進行儲存,而不是像第
Spring Boot 2.x基礎教程:預設資料來源Hikari的配置詳解
通過上一節的學習,我們已經學會如何應用Spring中的JdbcTemplate來完成對MySQL的資料庫讀寫操作。接下來通過本篇文章,重點說說在訪問資料庫過程中的一個重要概念:資料來源(Data Source),以及Spring Boot中對資料來源的建立與配置。 基本概念 在開始說明Spring Boot中
Spring Boot 2.x基礎教程:使用國產資料庫連線池Druid
上一節,我們介紹了Spring Boot在JDBC模組中自動化配置使用的預設資料來源HikariCP。接下來這一節,我們將介紹另外一個被廣泛應用的開源資料來源:Druid。 Druid是由阿里巴巴資料庫事業部出品的開源專案。它除了是一個高效能資料庫連線池之外,更是一個自帶監控的資料庫連線池。雖然HikariC
Spring Boot 2.x基礎教程:使用Spring Data JPA訪問MySQL
在資料訪問這章的第一篇文章《Spring中使用JdbcTemplate訪問資料庫》 中,我們已經介紹瞭如何使用Spring Boot中最基本的jdbc模組來實現關係型資料庫的資料讀寫操作。那麼結合Web開發一章的內容,我們就可以利用JDBC模組與Web模組的功能,綜合著使用來完成一個適用於很多簡單應用場景的後
Spring Boot 2.x基礎教程:使用MyBatis訪問MySQL
之前我們已經介紹了兩種在Spring Boot中訪問關係型資料庫的方式: 使用spring-boot-starter-jdbc 使用spring-boot-starter-data-jpa 雖然Spring Data JPA在國外廣泛流行,但是在國內還是MyBatis的天下。所以,今天這篇我們將具體說說如
Spring Boot 2.x基礎教程:使用 ECharts 繪製各種華麗的資料圖表
上一節我們介紹瞭如何在Spring Boot中使用模板引擎Thymeleaf開發Web應用的基礎。接下來,我們介紹一下後端開發經常會遇到的一個場景:視覺化圖表。 通常,這類需求在客戶端應用中不太會用到,但是在後端的各種統計分析模組會經常碰到。比如:通過折線圖、柱狀圖、雷達圖等視覺化形式,更直觀的展現和分析經營
Spring Boot 2.x基礎教程:Spring Data JPA的多資料來源配置
[上一篇](http://blog.didispace.com/spring-boot-learning-21-3-7/)我們介紹了在使用JdbcTemplate來做資料訪問時候的多資料來源配置實現。接下來我們繼續學習如何在使用Spring Data JPA的時候,完成多資料來源的配置和使用。 ## 新增多
Spring Boot 2.x基礎教程:事務管理入門
## 什麼是事務? 我們在開發企業應用時,通常業務人員的一個操作實際上是對資料庫讀寫的多步操作的結合。由於資料操作在順序執行的過程中,任何一步操作都有可能發生異常,異常會導致後續操作無法完成,此時由於業務邏輯並未正確的完成,之前成功操作的資料並不可靠,如果要讓這個業務正確的執行下去,通常有實現方式: 1.
Spring Boot 2.x基礎教程:EhCache快取的使用
[上一篇](http://blog.didispace.com/spring-boot-learning-21-5-1)我們學會了如何使用Spring Boot使用程序內快取在加速資料訪問。可能大家會問,那我們在Spring Boot中到底使用了什麼快取呢? 在Spring Boot中通過`@EnableC