02-项目实践

本笔记来源于:尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)
b站视频
文章来自:
https://github.com/OT-mt/cloud2020/tree/master/springcloud-2%E5%B0%9A%E7%A1%85%E8%B0%B7%E5%91%A8%E9%98%B3-2020

脑图

本地:
尚硅谷SpringCloud

在线:
尚硅谷SpringCloud

新建父项目

  1. 新建maven工程
  2. 选择模板 org.apache.maven.archetypes:maven-archetypes-site
  3. 选择项目字符编码setting->editor->file Encodings ->将所有字符编码全部换成 utf-8
  4. 支持注解setting->Build,Execution,Deployment->Compiler->Annotation Processors 内的 Enable annotation processing 打上勾表示能够使用注解。
  5. 更改编译版本setting->Build,Execution,Deployment->Compiler->Java Compiler 下 Pe-module butecode version 框内的编译版本换成 8
  6. 删除src文件夹

父工程POM

maven中的 dependencyManagement 与 dependencies

dependencyManagement 父类管理

Maven 使用dependencyManagement元素提供依赖版本号管理的方式
==通常会在一个组织或者项目的最顶级父POM中看到dependencyManagement ==

使用dependencyManagement可以使所有子项目中引用一个依赖而不用显示列出版本号。Maven会沿父子层级向上走,直到找到dependencyManagement元素的项目 ,然后它就会使用版本号

如果某一个子项目需要另外一个版本,加上version即可。如果子项目中声明了版本号,那么使用子项目中的jar包。

dependencyManagement只是声明依赖,并不实现引入,一次子项目需要显式的声明需要用的依赖。

支付模块构建

1. cloud-provider-payment8001微服务提供者支持Moudle模块

  1. 建module
    1. 新建maven工程,不使用任何模板,改jdk为1.8。
    2. 写入项目name即可。
  2. 改POM
    1. 主POM内增加 moudle
    2. 子项目引入依赖
  3. 写YML
  4. 主启动
  5. 业务类

    2. 热部署Devtools

  6. 引入坐标依赖
    1
    2
    3
    4
    5
    6
    7
        <!--热部署-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
    </dependency>
  7. 添加插件到父类总工程中
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <build>
    <finalName>工程名字</finalName>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <fork>true</fork>
    <addResources>true</addResources>
    </configuration>
    </plugin>
    </plugins>
    </build>
  8. 开启自动编译权限
    setting->Build,Execution,Deployment->Compiler 内复选框全部打勾
  9. 开启热注册
    1. 进入子模块 ctrl shift alt /
    2. 选择 Registry
      compiler.automake.allow.when.app.running
      compiler.automake.allow.when.app.running
      这两个勾选中
    3. 重启idea

3. cloud-consumer-order80微服务消费者订单Module模块

  1. 建module
    1. 新建maven工程,不使用任何模板,改jdk为1.8。
    2. 写入项目name即可。
  2. 改POM
    1. 主POM内增加 moudle
    2. 子项目引入依赖
  3. 写YML
  4. 主启动
  5. 业务类
    1. 将第一个子模块支付模块的实体类包复制过来
    2. 新建Controller类
  6. 使用 RestTemplate 进行两个模块之间的调用,由消费者订单模块调用支付模块。
    1. 新建配置类,注册 RestTemplate bean
      1
      2
      3
      4
      5
      6
      7
      @Configuration
      public class ApplicationContextConfig {
      @Bean
      public RestTemplate getRestTemplate(){
      return new RestTemplate();
      }
      }
    2. 写Controller方法
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      public class OrderController {
      public static final String PAYMENT_URL = "http://localhost:8001";

      @Resource
      private RestTemplate restTemplate;

      @GetMapping("/consumer/payment/create")
      public CommonResult<Payment> create(Payment payment){
      return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
      }

      @GetMapping("/consumer/payment/get/{id}")
      public CommonResult<Payment> getPayment(@PathVariable("id")Long id){
      return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
      }
      }
    3. 启动
      1. 先启动8001
      2. 再启动 80
      3. idea右下角弹出窗口,选择show,共同管理两个端口号
      4. 使用http://localhost/consumer/payment/get/31访问,80为默认端口号可省略不写

工程重建:

  1. 问题项目中有重复部分:两个工程中有完全相同的实体类
  2. 新建公共项目 cloud-api-commons 将公共有的实体类包括工具类等放到里面,所有模块都可以使用
    • 导入依赖 lombok devtools 以及 hutool 工具包
    • 将实体类包导入到项目中
  3. maven clean install
    • 点击上面的闪电 跳过测试
    • 进入 maven 找到新建模块,进入 Lifecycle 先clean再 install重新安装
    • 注意观察公共模块 pom 文件中必须含有独立的 groupid 和 artifactid

02-项目实践
http://yuanql.top/2023/06/27/13_SpringCloud/springcloud-2尚硅谷周阳-2020/02-项目实践/
作者
Qingli Yuan
发布于
2023年6月27日
许可协议