SpringBoot多模块项目(Multi-Modules)

1.事由:

经常看到GitHub上的一些官方demo,为了展示很多功能,项目是由多模块的形式组织的,基于maven构建的形式为根文件夹下有个pom.xml,每个功能文件夹下又有pom.xml。
刚开始傻傻不会用的时候,直接引入子文件夹下的pom.xml为maven项目。然后有需要看另一个功能又打开了一个子模块,因为IDE用的是idea,变成了多窗口。后来发现直接打开父模块接可以了,各个子模块可以在同一个窗口下切换和启动。

2.准备

以下最简单的建一个多模块项目,为了学习微服务,将建两个springboot web的子模块,服务提供者provider和消费者consumer。
两个服务在同个idea窗口中开发和启动,测试调用consumer的服务,consumer内部会调用provider。

3.新建父模块的pom.xml

  • new Project -> spring initializr快速构建SpringBoot,artifactId为springbootmodules
  • 因为父模块只是为了组织子模块用,删除src等没用的文件,删除pom.xml中maven依赖等信息。
  • 父模块打包类型packaging从jar调整为pom
  • 添加两个module:provider和consumer


    parent pom.jpg

4.新建子模块:

new Module - > spring initializr,分别新增artifactId为provider和consumer,放在springbootmodules目录之下:

consumer.png

consumer.png

结构如下,父pom.xml中的module也不会报错了
modules

5.更改application.properties

因为两个SpringBoot项目都默认是8080端口,为了能同时启动,调整consumer的端口为8081,在consumer的application.properties中添加server.port=8081

6.新增Controller api:

在provider和consumer分别新增两个controller: ProviderController和ConsumerController,返回最简单的字符串。
ProviderController.java:

package com.xd.provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {
    @GetMapping("/provider")
    public String provider() {
        return "provider";
    }
}

ConsumerController中为了调用provider(http://localhost:8081/provider),使用了spring的RestTemplate做http客户端,多配置了RestTemplate的Bean

package com.xd.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
package com.xd.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String consumer() {
        return "consumer";
    }

    @GetMapping("/consumerUseProvider")
    public String consumerUseProvider() {
        return "consumer use " + this.restTemplate.getForObject("http://localhost:8081/provider", String.class);
    }
}

7.启动服务和测试

test api

8.调整pom.xml

父pom.xml现在只有管理两个子模块的作用,可以抽取子模块统一的配置信息和依赖版本控制放父模块中管理。

父pom.xml:



    4.0.0

    com.xd
    springbootmodules
    0.0.1-SNAPSHOT
    pom

    
        provider
        consumer
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    


子模块pom.xml,设置parent为springbootmodules:



    4.0.0

    com.xd
    provider
    0.0.1-SNAPSHOT
    jar

    provider
    Demo project for Spring Boot

    
        
        
        
        
        com.xd
        springbootmodules
        0.0.1-SNAPSHOT
    

    
        
        
        
    

    
        
            
            
        

        
            
            
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

9.打包jar测试

使用mvn clean package命令,或点击idea maven窗口功能,把两个子模块打成jar包:


jar.jpg

使用java -jar命令启动两个服务:

java -jar /Users/TryXD/code/try/springbootmodules/provider/target/provider-0.0.1-SNAPSHOT.jar
java -jar /Users/TryXD/code/try/springbootmodules/consumer/target/consumer-0.0.1-SNAPSHOT.jar

两个服务都正常启动,并且服务消费者consumer调用服务提供者provider正常:


success

你可能感兴趣的:(SpringBoot多模块项目(Multi-Modules))