springboot多模块项目集成dubbo,项目结构展示
- 父工程 springbootparent
- 子模块 springbootapi , springbootserver , springbootclient
一 springboot多模块创建
-
创建父工程 File -> new -> Project
点击 next
后面接着next ->next->finish
保留上图箭头所指文件(.idea , .gitignore , pom.xml , iml文件 ),其他文件删除
将父工程 的 pom.xml 文件中的打包方式为 pom , spring版本自己看着选(低版本稳定) , 属性添加UTF-8编码,删除所有依赖dependency , 删除 build 。 如下:
(注:如果pom.xml没有自动更新,那 右键左边project栏里面pom.xml文件->Maven->Reimport,下面要是也出现这种情况同样手动更新)
-
创建子模块 springbootapi (右键父工程 -> New -> Module)
点击 next
next -> next -> finish
保留上图箭头指向文件( springbootapi 子模块的 src , pom.xml 和 iml 文件 ),删除后 springbootapi其他文件,如下
在父工程pom.xml中添加子模块
<modules>
<module>springbootapi</module>
</modules>
在子模块 springbootapi pom.xml中修改 声明父工程为 springbootparent
<parent>
<groupId>com.example</groupId>
<artifactId>springbootparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
删除 子项目中 springbootapi pom.xml 中 build内容 , 如果没声明打包类型,添加打包类型jar
- 创建子模块 springbootserver , springbootclient
其他的子模块创建过程和之前创建的子模块springbootapi 一样(除了springbootapi 需要删除build ,其他子模块不要删除),不再啰嗦了,照着上面操作。
注:别忘了springbootserver , springbootclient模块添加到父工程pom.xml的modules,然后在springbootserver 和springbootclient的pom.xml中声明父工程
子模块 springbootserver , springbootclient 创建完后,添加子模块之间的依赖
springbootapi 作为dubbo公共接口服务,springbootserver和springbootclient 需要依赖springbootapi
在springbootserver的pom.xml中添加springbootapi 的依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在springbootclient的pom.xml中添加springbootapi 的依赖
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
到此一个简单的多模块项目创建完。父工程springbootparent,子模块有springbootapi,springbootserver,springbootclient , 其中子模块springbootserver和springbootclient 都依赖 springbootapi
下面开始集成dubbo
二 springboot多模块集成dubbo
注:在集成dubbo 之前,请先确认本地安装且启动了zookeeper(注册中心)和dubbo-admin(dubbo管理平台,在这里用于观察下面集成dubbo集成是否成功),若没有启动,先启动。若没安装过请移步我之前的博文安装启动。https://blog.csdn.net/a704397849/article/details/91904085
- springbootapi 作为dubbo公共接口服务
目录结构,如下:
公共服务接口UserService
package com.example.springbootapi.dubbo.service;
/**
* @author
*/
public interface UserService {
public String print(String s);
}
- springbootserver 作为dubbo提供者提供服务
目录结构,如下:
pom.xml 新增依赖 (dubbo,zookeeper,zkclient,log4j依赖)
(注:如果pom.xml没有自动更新,那 右键左边project栏里面pom.xml文件->Maven->Reimport,下面要是也出现这种情况同样手动更新)
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
完成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.example</groupId>
<artifactId>springbootparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootserver</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<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>
springboot配置文件application.properties (注:dubbo依赖是dubbo-spring-boot-starter时,提供者配置spring.dubbo.server=true 必须有)
server.port=9991
#应用名
spring.dubbo.application.name=provider
#dubbo依赖版本为dubbo-spring-boot-starter时,dubbo提供者必须有这个server=true
spring.dubbo.server=true
#注册中心地址
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
#协议端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#扫描包,接口服务实现的包,根据个人接口服务类所在位置自己做修改
spring.dubbo.scan=com.example.springbootserver.service
新增服务接口实现类UserServiceImpl (注意@Service注解是dubbo的,不是spring的)
package com.example.springbootserver.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.springbootapi.dubbo.service.UserService;
import org.springframework.stereotype.Component;
@Service(interfaceClass = UserService.class)
@Component
public class UserServiceImpl implements UserService {
@Override
public String print(String s) {
return "say:" + s;
}
}
启动类SpringbootserverApplication
package com.example.springbootserver;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubboConfiguration
@SpringBootApplication
public class SpringbootserverApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootserverApplication.class, args);
}
}
到此dubbo提供者完成,运行后到dubbo-admin管理平台看是否成功提供dubbo服务
从控制台启动信息看是成功的,现在去dubbo管理平台dubbo-admin看服务提供者信息
从上述信息可以得知,dubbo 提供者子模块springbootserver 配置启动成功!
- springbootclient 作为dubbo消费者调用远程服务
目录结构,如下:
pom.xml 新增依赖 (dubbo,zookeeper,zkclient,log4j,web依赖)
(注:如果pom.xml没有自动更新,那 右键左边project栏里面pom.xml文件->Maven->Reimport,下面要是也出现这种情况同样手动更新)
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
完整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.example</groupId>
<artifactId>springbootparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
springboot配置application.properties
server.port=9992
#dubbo消费者配置放在dubbo-consumer.xml中
新增duboo消费者配置xml文件dubbo-consumer.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:application name="client"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
<!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
<dubbo:consumer check="false" />
<!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
<dubbo:reference id="UserService" check="false" interface="com.example.springbootapi.dubbo.service.UserService"/>
<!-- 需要使用到提供者服务的包路径,多个包时用逗号隔开。注: 网上好多这里写错了 -->
<dubbo:annotation package="com.example.springbootclient.controller"/>
</beans>
新增网页请求调用远dubbo程服务的类UserController
package com.example.springbootclient.controller;
import com.example.springbootapi.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author
*/
@Controller
public class UserController {
//reference 在xml配置,这里需要用注解Autowired
@Autowired
public UserService userService;
@RequestMapping("/test")
@ResponseBody
public String test(){
String s = userService.print("hhhhhhhhhhh");
return s;
}
}
启动类
package com.example.springbootclient;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
/**
* 消费者: 先扫描dubbo 然后 扫描spring
* @author
*/
@EnableDubboConfiguration
@ImportResource(locations = {"classpath:dubbo-consumer.xml"})
@ComponentScan(basePackages = "com.example.springbootclient.controller")
@SpringBootApplication
public class SpringbootclientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootclientApplication.class, args);
}
}
到此dubbo 消费者完成,运行后到dubbo-admin管理平台看是否成功注册dubbo消费者
从控制台启动信息看是成功的,现在去dubbo管理平台dubbo-admin看服务消费者信息
从上述信息可以得知,dubbo 消费者子模块springbootclient 配置启动成功!
浏览器输入 http://127.0.0.1:9992/test
到此整个springboot dubbo 多模块项目创建并且验证完成
源码
链接:https://pan.baidu.com/s/1SmgcG_lWfw-NZJduu4ipVw
提取码:9cw1
注:拿到源码运行之前请先确认本地安装运行了zookeeper和dubbo-admin,若没有启动,先启动。若没安装过请移步我之前的博文安装启动。https://blog.csdn.net/a704397849/article/details/91904085
最后
下一篇介绍 有子模块既是消费者又是提供者的springboot dubbo多模块项目demo