SpringBoot開發案例之整合Dubbo分布式服務


前言

在 SpringBoot 很火熱的時候,阿里巴巴的分布式框架 Dubbo 不知是處於什么考慮,在停更N年之后終於進行維護了。在之前的微服務中,使用的是當當維護的版本 Dubbox,整合方式也是使用的 xml 配置方式。

改造前

之前在 SpringBoot 中使用 Dubbox是這樣的。先簡單記錄下版本,Dubbox-2.8.4、zkclient-0.6、zookeeper-3.4.6。

項目中引入 spring-context-dubbo.xml 配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">
    <!-- 記錄監控信息 -->
    <dubbo:monitor protocol="registry"/>
	<!-- 提供方應用信息,用於計算依賴關系 -->
	<dubbo:application name="spring-boot-pay" />
	<!-- 使用zookeeper注冊中心暴露服務地址 subscribe 默認:true 是否向此注冊中心訂閱服務,如果設為false,將只注冊,不訂閱 check 默認:true 注冊中心不存在時,是否報錯    -->
	<dubbo:registry protocol="zookeeper" address="192.168.1.180:2181" check="false"/>
	<!-- 
	           生產者配置 生產者  遠程默認調用3次 參數 retries="2" async="true" 異步返回結果 默認是同步 timeout="10000" 毫秒
	           用dubbo協議在20882端口暴露服務  固定線程池 10 啟動時建立線程,不關閉,一直持有  負載均衡策略 輪詢
	 -->
	<dubbo:provider  timeout="10000"  threads="10" threadpool="fixed" loadbalance="roundrobin"/>
	<!-- name="dubbo" 協議名稱   為防止被大量連接撐掛,可在服務提供方限制大接收連接數,以實現服務提供方自我保護。 host 部署外網設置為內網通信地址-->
	<dubbo:protocol name="dubbo" port="-1" dispatcher="all"  accepts="1000"   />
	
	<!-- 使用注解方式-->     
    <dubbo:annotation package="com.itstyle"/>
</beans>

啟動類引入以下注解:

@SpringBootApplication
@ImportResource({"classpath:spring-context-dubbo.xml"})
public class Application{
	private static final Logger logger = Logger.getLogger(Application.class);

	public static void main(String[] args) throws InterruptedException,
			IOException {
		logger.info("支付項目啟動 ");
	}

}

改造后

然而 SpringBoot 引入了新的概念 Spring Boot Starter,它有效的降低了項目開發過程的復雜程度,對於簡化開發操作有着非常好的效果。

starter的理念

starter 會把所有用到的依賴都給包含進來,避免了開發者自己去引入依賴所帶來的麻煩。

需要注意的是不同的 starter 是為了解決不同的依賴,所以它們內部的實現可能會有很大的差異,例如 jpa 的starter 和 Redis 的 starter 可能實現就不一樣,這是因為 starter 的本質在於 synthesize,這是一層在邏輯層面的抽象,也許這種理念有點類似於 Docker,因為它們都是在做一個“包裝”的操作,如果你知道 Docker 是為了解決什么問題的,也許你可以用 Docker 和 starter 做一個類比。

starter的實現

雖然不同的starter實現起來各有差異,但是他們基本上都會使用到兩個相同的內容:ConfigurationProperties和AutoConfiguration。

因為Spring Boot堅信“約定大於配置”這一理念,所以我們使用ConfigurationProperties來保存我們的配置,並且這些配置都可以有一個默認值,即在我們沒有主動覆寫原始配置的情況下,默認值就會生效,這在很多情況下是非常有用的。

除此之外,starter的ConfigurationProperties還使得所有的配置屬性被聚集到一個文件中(一般在resources目錄下的application.properties),這樣我們就告別了Spring項目中XML地獄。

starter的整體邏輯

強如Dubbo,當然也會創建屬於自己的 starter 來迎合Spring Boot 的火熱。

這里我們使用Dubbo比較新的版本,pom.xml 引入以下:

<!-- dubbo 替換  dubbox-->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.6.2</version>
</dependency>
<dependency>
	<groupId>com.alibaba.spring.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>
<!-- curator-recipes 替換  zkclient-->
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>4.0.1</version>
</dependency>

application.properties 配置:

## dubbo springboot 配置
spring.dubbo.application.id=springboot_pay
spring.dubbo.application.name=springboot_pay
spring.dubbo.registry.address=zookeeper://192.168.1.127:2181
spring.dubbo.provider.threads=10
spring.dubbo.provider.threadpool=fixed
spring.dubbo.provider.loadbalance=roundrobin
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo

啟動類加入以下注解:

@EnableDubboConfiguration
@SpringBootApplication
public class Application{
	private static final Logger logger = Logger.getLogger(Application.class);

	public static void main(String[] args) throws InterruptedException,
			IOException {
		logger.info("支付項目啟動 ");
	}

}

相關暴露接口實現配置:

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;

@Service
@Component
public class AliPayServiceImpl implements IAliPayService {
      //省略代碼
}

最后啟動服務,如果啟動成功並注冊到注冊中心,說明改造成功。

補充

Dubbo 2.6.1 是改變結構后首次發布的版本,Dubbo 2.6.0 已合並當當網提供的 Dubbox 分支。

Dubbo的版本策略:兩個大版本並行發展,2.5.x是穩定版本,2.6.x是新功能實驗版本。2.6上實驗都穩定了以后,會遷移到2.5。

總結

  • 原當當 Dubbox 2.8.4 替換為 Dubbo 2.6.2
  • 原 spring-context-dubbo.xml 配置 替換為 dubbo-spring-boot-starter 2.0.0
  • 原 zkclient 0.6 替換為 curator-recipes 4.0.1
  • 原 zookeeper 3.4.6 升級為 zookeeper 3.5.3

案例

支付寶,微信,銀聯詳細代碼案例:https://gitee.com/52itstyle/spring-boot-pay

參考

https://github.com/apache/incubator-dubbo

https://github.com/alibaba/dubbo-spring-boot-starter/blob/master/README_zh.md

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters

https://www.nosuchfield.com/2017/10/15/Spring-Boot-Starters/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM