注:本篇文章主要参考周阳老师讲解的cloud进行整理的!
新建cloud-consumer-order80
修改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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mui.cloud</groupId>
<artifactId>cloud2025</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-consumer-order80</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--web + actuator-->
<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>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--hutool-all-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--fastjson2-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<!-- swagger3 调用方式 http://你的主机IP地址:5555/swagger-ui/index.html -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
写yml
新建application.yml
server:
port: 80
编写主启动类
package com.mui.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main80 {
public static void main(String[] args) {
SpringApplication.run(Main80.class, args);
}
}
业务类
- 将PayDTO,返回类型枚举,统一返回类拷贝到当前module
RestTemplate
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
RestTemplate官网
Modifier and Type | Method | Description |
---|---|---|
<T> ResponseEntity<T> | getForEntity(String url, Class<T> responseType, Object… uriVariables) | Retrieve an entity by doing a GET on the specified URL. |
<T> ResponseEntity<T> | getForEntity(String url, Class<T> responseType, Map<String,?> uriVariables) | Retrieve a representation by doing a GET on the URI template. |
<T> ResponseEntity<T> | getForEntity(URI url, Class<T> responseType) | Retrieve a representation by doing a GET on the URL. |
<T> T | getForObject(String url, Class<T> responseType, Object… uriVariables) | Retrieve a representation by doing a GET on the specified URL. |
<T> T | getForObject(String url, Class<T> responseType, Map<String,?> uriVariables) | Retrieve a representation by doing a GET on the URI template. |
<T> T | getForObject(URI url, Class<T> responseType) | Retrieve a representation by doing a GET on the URL. |
RestTemplateConfig配置类
package com.mui.cloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
PayController控制器
package com.mui.cloud.controller;
import com.mui.cloud.entities.PayDTO;
import com.mui.cloud.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@RestController
@Tag(name = "订单微服务模块", description = "订单模块")
public class OrderController {
private static final String PaymentSrv_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/pay/add")
@Operation(summary = "新增订单", description = "新增订单, json串做参数")
public ResultData addOrder(PayDTO payDTO) {
return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
}
@GetMapping("/consumer/pay/get/{id}")
@Operation(summary = "根据id查询订单")
public ResultData getPayInfo(@PathVariable("id") Integer id) {
return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id, ResultData.class, id);
}
@PutMapping("/consumer/pay/update")
@Operation(summary = "修改订单")
public ResultData updateOrder(PayDTO payDTO) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<PayDTO> entity = new HttpEntity<>(payDTO, headers);
return restTemplate.exchange(PaymentSrv_URL + "/pay/update", HttpMethod.PUT, entity, ResultData.class).getBody();
}
@DeleteMapping("/consumer/pay/delete/{id}")
@Operation(summary = "删除订单")
public ResultData deleteOrder(@PathVariable("id") Integer id) {
Map<String, Integer> paramters = new HashMap<>();
paramters.put("id", id);
return restTemplate.exchange(PaymentSrv_URL + "/pay/delete/" + id, HttpMethod.DELETE, null, ResultData.class, paramters).getBody();
}
}