1. 使用IDEA新建一个Maven项目
新建项目
选择Maven后,点击next下一步
选择项目类型
配置项目的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.boot</groupId> <artifactId>boot-dubbo</artifactId> <version>1.0-SNAPSHOT</version> <!--在这里设置打包类型为pom,作用是为了实现多模块项目--> <packaging>pom</packaging> </project>
2. 创建boot-dubbo的子模块项目
创建Dubbo服务接口项目,我们把项目建立在刚才我们创建的boot-dubbo项目下面,作为boot-dubbo的子模块项目进行管理。
点击我们刚才新建好的项目,新建一个boot-dubbo的子模块 项目
新建项目的子模块项目
选择Maven后点击 next
配置模块参数
完成项目子模块的创建
同理我们继续我们第二步的步骤,再次创建一个boot-dubbo-provider和boot-dubbo-consumer这两个子模块项目。
创建完成后,我们的整体项目结构图如下图所示:
项目完成结构图
至此,我们创建项目的准备工作已经完毕。
3. 定义每个项目的pom.xml文件
打开我们的顶层项目boot-dubbo的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.boot</groupId> <artifactId>boot-dubbo</artifactId> <version>1.0-SNAPSHOT</version> <!-- 这里是我们子模块的设置 --> <modules> <module>boot-dubbo-api</module> <module>boot-dubbo-provider</module> <module>boot-dubbo-consumer</module> </modules> <!-- 在这里设置打包类型为pom,作用是为了实现多模块项目 --> <packaging>pom</packaging> <!-- 第一步:添加Springboot的parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <!-- 设置我们项目的一些版本属性 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <dubbo.version>2.5.5</dubbo.version> <zkclient.version>0.10</zkclient.version> <lombok.version>1.16.18</lombok.version> <spring-boot.version>1.5.7.RELEASE</spring-boot.version> </properties> <!-- 声明一些项目依赖管理,方便我们的依赖版本管理 --> <dependencyManagement> <dependencies> <!-- Springboot依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version> </dependency> <!-- Springboot-web依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot.version}</version> </dependency> <!-- 使用lombok实现JavaBean的get、set、toString、hashCode、equals等方法的自动生成 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <!-- Dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <!-- zookeeper的客户端依赖 --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
boot-dubbo-api的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"> <parent> <artifactId>boot-dubbo</artifactId> <groupId>com.boot</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>boot-dubbo-api</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies> </project>
boot-dubbo-provider的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"> <parent> <artifactId>boot-dubbo</artifactId> <groupId>com.boot</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>boot-dubbo-provider</artifactId> <dependencies> <dependency> <groupId>com.boot</groupId> <artifactId>boot-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
boot-dubbo-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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>boot-dubbo</artifactId> <groupId>com.boot</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>boot-dubbo-consumer</artifactId> <dependencies> <dependency> <groupId>com.boot</groupId> <artifactId>boot-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <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>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
以上是Springboot整合Dubbo项目和环境搭建的方法的详细内容。更多信息请关注PHP中文网其他相关文章!