04、微服务调用者订单Module模块

注:本篇文章主要参考周阳老师讲解的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 TypeMethodDescription
<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> TgetForObject(String url, Class<T> responseType, Object… uriVariables)Retrieve a representation by doing a GET on the specified URL.
<T> TgetForObject(String url, Class<T> responseType, Map<String,?> uriVariables)Retrieve a representation by doing a GET on the URI template.
<T> TgetForObject(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();
    }

}
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值