Spring Boot整合Dubbo

主要参考资料

https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md

示例代码

https://github.com/ralgond/dubbo-example
https://github.com/ralgond/dubbo-example/releases/tag/v0.0.3

依赖包版本

JDK的版本是1.8+
Spring Boot的版本是2.3.0.RELEASE
Dubbo的版本是2.7.1
dubbo-spring-boot-starter的版本是2.7.7

依赖组件

zookeeper,在localhost:2181启动

配置文件

注意一定不要忘了配置文件,此文件名为application.properties,在dubbo-example-spring-boot-consumer工程的src/main/resources的目录下。

POM的配置

由于Dubbo是在运行时才加载包,这样有个问题是无法在编译时就判定缺少了什么包,只有在程序启动后我们才能看到那些比如“类找不到”的异常。因此,我建议完全拷贝示例代码的POM文件,而不是只拷贝部分。比如说如果你漏掉了一个叫com.alibaba.spring:spring-context-support的包,你就会遇到“类找不到”的异常。完整的POM如下:

	<properties>
		<dubbo.version>2.7.1</dubbo.version>
		<dubbo.example.version>0.0.4-SNAPSHOT</dubbo.example.version>
		<spring-boot.version>2.3.0.RELEASE</spring-boot.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>

			<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>

			<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
			<dependency>
				<groupId>org.apache.dubbo</groupId>
				<artifactId>dubbo</artifactId>
				<version>${dubbo.version}</version>
			</dependency>

			<dependency>
				<groupId>org.apache.dubbo</groupId>
				<artifactId>dubbo-dependencies-zookeeper</artifactId>
				<version>${dubbo.version}</version>
				<type>pom</type>
			</dependency>

			<dependency>
				<groupId>com.alibaba.spring</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>1.0.11</version>
			</dependency>

			<dependency>
				<groupId>org.apache.dubbo</groupId>
				<artifactId>dubbo-spring-boot-starter</artifactId>
				<version>2.7.7</version>
			</dependency>

			<dependency>
				<groupId>com.github.ralgond</groupId>
				<artifactId>dubbo-example-sdk</artifactId>
				<version>${dubbo.example.version}</version>
			</dependency>

			<dependency>
				<groupId>com.github.ralgond</groupId>
				<artifactId>dubbo-example-loadbalance</artifactId>
				<version>${dubbo.example.version}</version>
			</dependency>


		</dependencies>
	</dependencyManagement>

        <dependencies>
		<!-- Spring Boot Starter -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- Dubbo Spring Boot Starter -->
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
		</dependency>

		<dependency>
			<groupId>com.alibaba.spring</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-dependencies-zookeeper</artifactId>
			<type>pom</type>
		</dependency>

		<dependency>
			<groupId>com.github.ralgond</groupId>
			<artifactId>dubbo-example-sdk</artifactId>
		</dependency>
	</dependencies>
        <build>
		<plugins>
                        <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>utf8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
posted @ 2020-11-04 15:17  ralgo  阅读(342)  评论(0)    收藏  举报