dubbo作為最常用的分散式框架之一,本人覺得很有必要自己搭建一個簡單的框架。
專案最終結構
最外層為名為dubbo_demo的maven模組,內有基於springboot的生產者與消費者模組。建立maven專案
使用idea新建maven專案,填入基本資訊。
只保留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.lamarsan</groupId>
<artifactId>dubbo_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--子模組宣告-->
<modules>
<module>
provider
</module>
<module>
consumer
</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dubbo.version>2.8.5-SNAPSHOT</dubbo.version>
<curator-framework.version>1.3.3</curator-framework.version>
<zookeeper.version>3.4.8</zookeeper.version>
<zkclient.version>0.1</zkclient.version>
<mybatis-starter.version>1.3.2</mybatis-starter.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-starter.version}</version>
</dependency>
<!-- zookeeper start -->
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<!-- zookeeper end -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
複製程式碼
下面進行子模組的建立 :
右擊專案名,新建Module,建立springboot專案。provider
專案結構如下,包括與資料庫連線的dao,實體類model,以及服務service:
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>
<!--父模組宣告-->
<parent>
<groupId>com.lamarsan</groupId>
<artifactId>dubbo_demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.lamarsan</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.1.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
service
dubbo提供了服務方的宣告方式@Service,雖然和springboot的service宣告方式一樣,但是兩者引用的是不同的包。dubbo的包名為com.alibaba.dubbo.config.annotation.Service,不要導錯。
屬性配置
#當前服務/應用的名字
dubbo.application.id=provider
dubbo.application.name=provider
dubbo.application.qos-enable=false
dubbo.scan.basePackages=com.lamarsan.provider.service.impl
#通訊規則(通訊協議和介面)
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=12346
#註冊中心的協議和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=xxxxx:2181
user.service.version=1.0.0
spring.datasource.url=jdbc:mysql://localhost:3306/debate?serverTimezone=GMT%2B8&&useCursorFetch=true&&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations = classpath*:mapper/*.xml
複製程式碼
啟動項
package com.lamarsan.provider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableDubbo
@ComponentScan(basePackages = {"com.lamarsan.provider"}) //dubbo掃描的包
@MapperScan("com.lamarsan.provider.dao")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
複製程式碼
consumer
專案結構如下,包括一個controller;
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>
<parent>
<groupId>com.lamarsan</groupId>
<artifactId>dubbo_demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.lamarsan</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.lamarsan</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
<!--引入web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.1.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
controller
dubbo提供了@Reference註解,該註解可以替換@Autowired註解,用於引入遠端微服務,包名為 com.alibaba.dubbo.config.annotation.Reference
屬性配置
# ApplicationConfig Bean
dubbo.application.id = consumer
dubbo.application.name = consumer
dubbo.application.qos-enable = false
dubbo.consumer.timeout=60000
dubbo.consumer.retries=0
server.port = 8098
# zookeeper
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.registry.protocol = zookeeper
dubbo.registry.address = xxxxx:2181
複製程式碼
啟動項
package com.lamarsan.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableDubbo
@ComponentScan(basePackages = {"com.lamarsan.consumer"})
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
複製程式碼
呼叫結果
先啟動provider模組,再啟動consumer模組,實現從資料庫讀取一個使用者資訊:
以上就是全部的搭建流程。接下來是搭建過程中碰到的一些問題的解答。踩坑環節
1)資料來源沒有自動配置
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
提示資料來源沒有得到正確的配置。
解決方案1
在消費者啟動向中加入exclude = {DataSourceAutoConfiguration.class}
,如圖所示:
解決方案2
將消費者模組的pom.xml中的有關mybatis與資料庫的依賴刪去即可。由於一開始並沒有注意這一細節,所以直接是從生產者的pom.xml中複製黏貼的,故報了這個錯。
2)服務方正確啟動,但是無法呼叫服務中的方法
No provider available from registry 127.0.0.1:2181 for service com.focussend.email.service.EdmTaskS
提示沒有可用的服務。
由於之前在@service(version="")中加入了版本資訊,但是消費者端並沒有定義,故出現了版本不一致的問題,所以無法正確啟動。
解決方案1
去掉@service括號裡的內容。
解決方案2
在消費者端定義dubbo.xml中的版本資訊。
3)資料庫繫結異常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
出現的錯誤網上是千篇一律的提示mapper.xml與dao層介面類的名字不一致以及namespace問題,也有提示沒有生成.xml檔案的問題,但都不適用於自己的情況。到最後才發現是配置項的宣告問題。
解決方案
在.properties檔案中加入:
mybatis.mapper-locations = classpath*:mapper/*.xml
一行,實現對map的掃描。
總結
之前一直使用別人已經搭建好的dubbo框架,自己搭建一套才發現坑真的不是一般的多,經常會踩一些別人認為理所當然會配置正確的坑,別看只有這麼三個異常,從開始搭建到搭建結束花了將近四個小時,瘋狂的查詢問題。但是這些坑都是有意義的,確實是讓我這個記憶力不好的人深深地記住了。
最後給出此專案的的gihub地址:github.com/lamarsan/du…