• 分布式项目搭建


    使用idea搭建一个基于springboot的分布式项目。

    总体项目目录结构:

     父maven项目:添加公共的依赖,pom如下:

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <modules>
    5. <module>ordermodule>
    6. <module>stockmodule>
    7. modules>
    8. <parent>
    9. <groupId>org.springframework.bootgroupId>
    10. <artifactId>spring-boot-starter-parentartifactId>
    11. <version>2.3.11.RELEASEversion>
    12. <relativePath>relativePath>
    13. parent>
    14. <groupId>com.springcloud.learninggroupId>
    15. <artifactId>springcloudalibabaartifactId>
    16. <version>0.0.1-SNAPSHOTversion>
    17. <name>springcloudalibabaname>
    18. <description>Spring Cloud Alibabadescription>
    19. <packaging>pompackaging>
    20. <properties>
    21. <java.version>1.8java.version>
    22. properties>
    23. <dependencies>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starterartifactId>
    27. dependency>
    28. dependencies>
    29. <build>
    30. <plugins>
    31. <plugin>
    32. <groupId>org.springframework.bootgroupId>
    33. <artifactId>spring-boot-maven-pluginartifactId>
    34. plugin>
    35. plugins>
    36. build>
    37. project>

    stock : 库存模块

     pom : 依赖于父模块,添加自己所需的web模块

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>springcloudalibabaartifactId>
    6. <groupId>com.springcloud.learninggroupId>
    7. <version>0.0.1-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>stockartifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. dependencies>
    17. project>
    StockController
    1. package com.springcloud.learning.stock.controller;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. @RequestMapping("/stock")
    6. public class StockController {
    7. @RequestMapping("reduct")
    8. public String reduct(){
    9. System.out.println("扣减库存");
    10. return "扣减库存";
    11. }
    12. }
    StockApplication
    1. package com.springcloud.learning.stock;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class StockApplication {
    6. public static void main(String[] args) {
    7. SpringApplication.run(StockApplication.class,args);
    8. }
    9. }

    application.yml   :  设置端口

    1. server:
    2. port: 8011

    order:订单模块

    pom : 依赖于父模块,添加自己所需的web模块。

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>springcloudalibabaartifactId>
    6. <groupId>com.springcloud.learninggroupId>
    7. <version>0.0.1-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>orderartifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. dependencies>
    17. project>

     OrderController

    1. package com.springcloud.learning.order.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. import org.springframework.web.client.RestTemplate;
    7. @RestController
    8. @RequestMapping("/order")
    9. public class OrderController {
    10. @Autowired
    11. private RestTemplate restemplate;
    12. @RequestMapping("/add")
    13. public String add(){
    14. System.out.println("下单成功!");
    15. String msg = restemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
    16. return "Hello World";
    17. }
    18. }
    OrderApplication
    1. package com.springcloud.learning.order;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.boot.web.client.RestTemplateBuilder;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.web.client.RestTemplate;
    7. @SpringBootApplication
    8. public class OrderApplication {
    9. public static void main(String[] args) {
    10. SpringApplication.run(OrderApplication.class,args);
    11. }
    12. @Bean
    13. public RestTemplate restTemplate(RestTemplateBuilder builder){
    14. RestTemplate restTemplate = builder.build();
    15. return restTemplate;
    16. }
    17. }

    application.yml

    1. server:
    2. port: 8010

    idea 开启多服务启动控制面板:

    选择 springboot

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

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

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

  • 相关阅读:
    4 redis的HyperLogLog入门&原理
    Linux性能优化实战CPU篇之软中断(三)
    使用OpenGL绘制shp文件
    计算连续性状的PRS得分
    【iOS】—— 六大原则和工厂模式
    多条件组合查询SQL语句
    《深度学习》深度学习 框架、流程解析、动态展示及推导
    python 字符串类型
    虚拟机里为什么桥接模式可以广播,NAT模式不能广播?
    添加阿里云maven镜像
  • 原文地址:https://blog.csdn.net/hoho_12/article/details/125961170