顺便学习一下springboot如何搭建分模块项目
创建分模块springboot
-
先创建一个springboot的空项目然后删除无关文件
image.png - 修改pom文件,引入所需依赖(dubbo,zkclient)
<?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.xun</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging><!--设置打包方式为pom-->
<name>dubbo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 1.模块管理 -->
<modules>
<module>provider</module>
<module>consumer</module>
</modules>
<dependencies>
<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>
</dependency>
<!-- 2.引入dubbo和zookeeper依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-
然后创建模块
image.png
创建之后项目结构如图:
image.png - 至此,我们的分模块项目已经基本创建完成
各个模块之间的依赖关系
- 接下来说下各个模块之间的依赖关系
consumer --> provider
consumer 和 provier 又同时依赖于父pom【dubbo】 - ok理清依赖关系开始配置我们的各个模块
依赖配置
- 最外层pom.xml作为我们所有模块的父pom打包方式要是pom类型,上边配置中已经说明
- 配置provider模块,需要继承父pom
<?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.xun</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<!-- 1.继承父类pom -->
<parent>
<groupId>com.xun</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>
- 配置consumer模块,需要继承父pom并且要引入provider依赖
<?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.xun</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<!-- 1.继承父pom -->
<parent>
<groupId>com.xun</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 2.引入服务接口依赖 -->
<dependency>
<groupId>com.xun</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- ok依赖配置完成,接下来就开始撸代码了
配置dubbo配置
- provider模块dubbo配置
server:
port: 8081
dubbo:
#应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者
application:
name: provider
#注册中心配置,用于配置连接注册中心相关信息
registry:
address: zookeeper://39.106.149.250:2182
#协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受
protocol:
name: dubbo
port: 20880
#扫描包
scan:
base-packages: com.xun.provider.service
- consumer模块dubbo配置
server:
port: 8082
dubbo:
application:
name: consumer
registry:
address: zookeeper://39.106.149.250:2182
protocol:
name: dubbo
port: 20880
业务代码层
- 需要在程序入口的main方法添加@EnableDubbo注解开启dubbo
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
- 服务接口编写
在provider模块下创建service包并编写接口代码
这里只提供接口实现类了
注意@Service注解要导入alibaba的包
package com.xun.provider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.xun.provider.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Override
public String getUser() {
return "你好,DUBBO";
}
}
- 消费者业务代码编写
在consumer模块下创建controller包并编写接口代码
注意这是使用@Reference注入
package com.xun.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.xun.provider.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference
private UserService userService;
@GetMapping("/getUser")
public String getUser(){
return userService.getUser();
}
}
-
编译
image.png
注意顺序是 1.dubbo 2.provider 3.consumer
运行测试
- 项目启动要首先启动服务再启动消费者
-
项目启动成功访问测试
image.png
网友评论