使用idea搭建一个基于springboot的分布式项目。
总体项目目录结构:

父maven项目:添加公共的依赖,pom如下:
- <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <modules>
- <module>ordermodule>
- <module>stockmodule>
- modules>
-
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.3.11.RELEASEversion>
- <relativePath>relativePath>
- parent>
-
- <groupId>com.springcloud.learninggroupId>
- <artifactId>springcloudalibabaartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springcloudalibabaname>
- <description>Spring Cloud Alibabadescription>
- <packaging>pompackaging>
-
- <properties>
- <java.version>1.8java.version>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- dependencies>
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
stock : 库存模块

pom : 依赖于父模块,添加自己所需的web模块
- <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>springcloudalibabaartifactId>
- <groupId>com.springcloud.learninggroupId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>stockartifactId>
-
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- dependencies>
-
- project>
StockController
- package com.springcloud.learning.stock.controller;
-
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/stock")
- public class StockController {
-
- @RequestMapping("reduct")
- public String reduct(){
- System.out.println("扣减库存");
- return "扣减库存";
- }
- }
StockApplication
- package com.springcloud.learning.stock;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class StockApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(StockApplication.class,args);
- }
- }
application.yml : 设置端口
- server:
- port: 8011
order:订单模块

pom : 依赖于父模块,添加自己所需的web模块。
- <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>springcloudalibabaartifactId>
- <groupId>com.springcloud.learninggroupId>
- <version>0.0.1-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>orderartifactId>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- dependencies>
-
-
- project>
OrderController
- package com.springcloud.learning.order.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- @RestController
- @RequestMapping("/order")
- public class OrderController {
-
- @Autowired
- private RestTemplate restemplate;
-
- @RequestMapping("/add")
- public String add(){
- System.out.println("下单成功!");
- String msg = restemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
- return "Hello World";
- }
- }
OrderApplication
- package com.springcloud.learning.order;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.client.RestTemplateBuilder;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.client.RestTemplate;
-
- @SpringBootApplication
- public class OrderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(OrderApplication.class,args);
- }
-
- @Bean
- public RestTemplate restTemplate(RestTemplateBuilder builder){
- RestTemplate restTemplate = builder.build();
- return restTemplate;
- }
-
- }
application.yml
- server:
- port: 8010
idea 开启多服务启动控制面板:

选择 springboot

如下图,就能控制多个服务的启动与停止:

备注: 项目中的maven依赖在打开idea时,总是会飘红,观察到本地maven配置中的settings.xml中有两个mirror地址。默认会使用第一个,但是第一个地址是公司内网地址,在家里不能访问。此时注释掉第一个地址,然后刷新maven依赖,使用如下功能重启idea。

maven项目给了我们便利的同时,使我们从海量的jar配置中解放出来,但是各种各样的依赖下载问题也总是让我们感觉到头疼。