订单微服务模块

本文介绍了如何在SpringBoot项目中构建订单微服务模块,包括项目搭建、使用RestTemplate进行远程访问支付模块、实体类的重构和模块之间的依赖管理。作者详细展示了如何通过Maven管理和引入共用模块,以减少代码冗余。
摘要由CSDN通过智能技术生成

目录

构建订单微服务模块

项目搭建

RestTemplate远程访问

 订单controller

启动运行 

工程重构

 创建spring_clou_api_commons module

entity和实体类放入common中 

 迁移支付模块与订单模块相同的entity类

使用maven,将common模块打包(install) 

修改其他模块对应的代码 

 删除重复

引入Maven (spring_cloud_api_common)


构建订单微服务模块

项目搭建

支付微服务模块相似流程建立module

  1. 建Module
  2. 改POM,引入依赖
  3. 写yml
  4. 主启动类
  5. 业务类

RestTemplate远程访问

因为这里是消费者类,主要是消费,那么就没有service和dao,需要调用pay模块的方法, 并且这里还没有微服务的远程调用,那么如果要调用另外一个模块,则需要使用基本的api调用,使用RestTemplate调用pay模块,RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模版类,是Spring提供的用于访问Rest服务的客户端模版工具类。

使用RestTemplate访问restful接口非常简单粗暴无脑。(url,requestMap,ResponseBean.class)这三个参数分别代表REST请求地址/请求参数/HTTP响应转换被转换成的对象类型。

将restTemplate注入到容器

package com.tudeen.learn.config;

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

@Configuration
public class ApplicationContextConfig {

    @Bean //applicationContext.xml <bean id="" class="">
    //@LoadBalanced 暂时注解掉,使用自定义负载
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

 订单controller

package com.tudeen.learn.controller;

import com.tudeen.learn.common.CommonResult;
import com.tudeen.learn.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Slf4j
@RestController
public class OrderController {
    //远程调用支付模块8001端口
    private  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/getId/{id}")
    public CommonResult<Payment> getId(@PathVariable("id") String id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/getPaymentByid/" + id, CommonResult.class);
    }
}

启动运行 

同时运行支付与订单模块项目

 点击运行

http://localhost/consumer/payment/getId/31 可以远程调用支付模块的接口

工程重构

 创建spring_clou_api_commons module

    <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>

entity和实体类放入common中 

 迁移支付模块与订单模块相同的entity类

使用maven,将common模块打包(install) 

修改其他模块对应的代码 

 删除重复

删除原先支付与订单模块的重复的Payment与CommentResult实体类

引入Maven (spring_cloud_api_common)
  <dependency>
            <groupId>com.tudeen.learn</groupId>
            <artifactId>spring_cloud_api_commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值