Springcloud-Alibaba〖二〗微服务cloud-consumer-order80构建与公共类cloud-api-commons

接着上一章写

我摊牌了,大佬们总结的脑图给你们~

在线脑图点击就送~

四. 热部署(可做可不做,不做可跳过)

每次代码写完后人工重启非常耗费时间的,所以我们选择热部署

4.1 添加依赖在8001项目中

 <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <scope>runtime</scope>
       <optional>true</optional>
 </dependency>

4.2 配置父工程pom文件

 <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

4.3 开启自动编译选项

选项全勾中
在这里插入图片描述

4.4 配置选项

来到8001工程,打开idea,按住ctrl +shift+ alt + /
在这里插入图片描述
点开,然后选择选项打勾
在这里插入图片描述

热部署部署完成后,你每次修改一次代码,都会重启我们整个项目,所以开发可以用,但是一旦上生产环境,千万要关闭

五. 建立 cloud-consumer-order80微服务

5.1 创建模块

在这里插入图片描述

5.2 改pom文件

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

5.3 建立yml文件

server:
  port: 80

5.4 建立主启动

package com.aiguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

5.5 引入实体类

还是之前8001项目的两个实体类
在这里插入图片描述

5.6 创建 RestTemplate模板加入spring的bean中

package com.aiguigu.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

5.7 controller层

package com.aiguigu.springcloud.controller;

import com.aiguigu.springcloud.entities.CommonResult;
import com.aiguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
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){
        log.info("*******消费者启动创建订单*******");
        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);
    }

}

5.8 测试及坑

测试查找成功
在这里插入图片描述

测试一下插入发现成功了,但是数据库没值,只有自增主键,这里插入没成功的原因是,8001的创建参数没加@RequestBody,大家记得加上才能映射成功

在这里插入图片描述

六. 工程重构

6.1 重复代码存在

在这里插入图片描述

6.2 创建一个公共模块

6.2.1首先创建出cloud-api-commons模块

在这里插入图片描述

6.2.2 添加新模块的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">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.aiguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-api-commons</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
</project>

6.2.3 将公共实体类代码拷贝到新模块中

在这里插入图片描述

6.2.4 Maven打包

先clean,记得提前点闪电跳过测试
在这里插入图片描述
再点install
在这里插入图片描述
成功
在这里插入图片描述

6.2.5 各自删除8001与80项目的实体包

6.2.6 各自导入打包完成的实体类依赖

引入如下依赖在8001项目与80项目中

<dependency>
	<groupId>com.atguigu.springcloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>

6.2.7 测试代码

测试成功
在这里插入图片描述

制作不易,转载请标注~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章