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 測試代碼
測試成功