【SpringCloud04】订单-支付微服务模块(消费者订单模块)
目录
- 1.建cloud-consumer-order80
- 2.改pom
- 3.写yml
- 4.主启动
- 5.业务类
- 5.1entities
- 5.2RestTemplate
- RestTemplate是什么
- RestTemplate官网及使用
- 5.3config配置类
- 5.4controller
- 6.测试
- 6.1测试查询
- 6.2测试插入
1.建cloud-consumer-order80
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"><modelVersion>4.0.0</modelVersion><parent><groupId>com.atguigu.springcloud</groupId><artifactId>cloud2022</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>cloud-consumer-order80</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><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>
3.写yml
server:port: 80
4.主启动
package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 简要描述** @Author: ASuLe* @Date: 2023/1/11 20:17* @Version: 1.0* @Description: 文件作用详细描述....*/
@SpringBootApplication
public class OrderMain80 {public static void main(String[] args) {SpringApplication.run(OrderMain80.class,args);}
}
5.业务类
问题1: 客户端是否需要controller层、service层、dao层吗?
回答: 不需要,只需要controller。
原因: 消费者调用8001提供者,提供服务的是8001,类似于同学不懂Java才找老师学习Java,这里老师就是教学服务的提供者,所以只应该有个controller,至于service和dao,不可能,因为你不可能操作提供者的数据库。
问题2: 但是order80和payment8001是不同的服务,该怎么调用呢?
解决1: 最原始web阶段使用HttpClient
解决2: 现在用restTemplate
5.1entities
主实体类Payment,Json封装体CommonResult类可直接复制
5.2RestTemplate
RestTemplate是什么
RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
RestTemplate官网及使用
RestTemplate官网地址
使用:
使用RestTemplate非常简单
- url:REST请求地址
- requestMap:请求参数
- ResponseBean.class:HTTP响应转换被转换成的对象类型
5.3config配置类
位置
package com.atguigu.springcloud.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/*** 简要描述** @Author: ASuLe* @Date: 2023/1/11 20:34* @Version: 1.0* @Description: 文件作用详细描述....*/
@Configuration
public class ApplicationContextConfig {//@Bean相当于applicationContext.xml <bean id="" class=""/>@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}}
5.4controller
package com.atguigu.springcloud.controller;import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.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;/*** 简要描述** @Author: ASuLe* @Date: 2023/1/11 20:23* @Version: 1.0* @Description: 文件作用详细描述....*/
@RestController
@Slf4j
public class OrderController {public static final String PAYMENT_URL = "http://localhost:8001";@Resourceprivate RestTemplate restTemplate;//这里是客户端都是发送GetMapping请求@GetMapping("/consumer/payment/create")public CommonResult<Payment> create(Payment payment) {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);}}
service服务控制台
6.测试
6.1测试查询
查询成功
6.2测试插入
看似插入成功,真的是这样吗?
如下图所示,插入的数据有主键,但是无内容
不要忘记@RequestBody注解
再次尝试插入
数据库中含有数据,插入成功
【SpringCloud04】订单-支付微服务模块(消费者订单模块)相关推荐
- 自动匹配未认领订单编号_海量订单系统微服务开发:使用MongoDB支持海量数据...
海量订单系统微服务开发 订单系统是电商平台中一个非常重要的组成部分,而且它还是一个具有巨大流量和高并发访问的系统,与订单相关的服务涉及库存.支付.物流等.在设计订单系统时,我们选择使用支持海量数据的N ...
- SpringCloud无介绍快使用,子module提供者支付微服务业务开发(六)
SpringCloud无介绍快使用,子module提供者支付微服务业务开发(六) 问题背景 项目搭建 Lyric: 一只灰狼问候我谁是神枪手 问题背景 从零开始学springcloud微服务项目 注意 ...
- (二)SpringCloud,Alibaba微服务架构之——核心模块及相应技术
一.微服务架构中核心模块及其使用技术总览 二.各模块详细说明 1.注册中心 该模块主要功能为 自动提供服务的注册与发现,集中式管理服务,让 服务调用端发现服务,让服务提供端注册服务,倘若没有注册中心, ...
- 支撑日均百万订单的微服务架构应该这么搞
最近几年微服务很火,大家都在建设微服务,如果不懂点微服务相关的技术,都不好意思跟同行打招呼了. 我也见过身边很多人在微服务踩过很多坑,我从 2016 年开始接触微服务,有多家大型企业的微服务分布式系统 ...
- 微服务之开发订单功能
业务流程 第一部分是信息的收集 主要是参数类型数据的完整性验证,计算以及转换 第二部分是数据库操作 减少库存,删除购物车,新增订单,和新增订单项 第三部分是收集需要的返回值 我们新增订单成功后,要返回 ...
- 聚合支付微服务版(微信篇)
序 之前分享过微信支付的实现,当时想的是做公司级的聚合支付.那时候还没有微服务,我是单独做的一套springBoot的支付,实现了扫码支付.微信内支付.业务与支付的基本流程可以串起来了,但是当时我是负 ...
- 若依微服务版新建业务模块后提示找不到mapper的解决方法
场景 若依微服务版怎样新增业务子模块并使用代码生成实现对表的增删改查: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/109524 ...
- 微服务如何实现各个模块之间相互调用
大家好,我是雄雄,现在是:2022年8月23日21:08:53 前言 在做微服务项目时,我们可能都会遇到这样的情况,那就是A服务需要调用B服务中的某个接口,那有这样的需求时,我们应该怎么处理呢? 实现 ...
- 微服务架构之公共模块式中创建API接口统一返回结果ApiResult
微服务架构之API接口统一返回结果ApiResult 一.创建公共模块common步骤 二.在api中新建一个ApiCode枚举和ApiResult 三.ApiCode与ApiResult编码如下 四 ...
最新文章
- SEL|世界土壤日-土壤生态学Nico Eisenhauer 教授讲座
- Java前沿分享:value或许成为java的新关键字
- 其他机无法连接到redis
- matlab读取/播放视频的函数(1)
- C#中DataTable使用以及对行与列的赋值
- RSA算法原理——(3)RSA加解密过程及公式论证
- Win 10 终于干趴了 Win 7
- 新的一年,碎片化学习前端,我推荐这几个公众号~
- opencv_python使用cv2.imread()读取中文路径报错问题(转)
- 如何实现跨项目代码复用
- 【Linux】磁盘分区
- HDU 2079 选课时间(母函数)
- 2021全国大学生电子设计大赛题目汇总
- 计算机电路图解,几种常见的放大电路原理图解
- SEO和SEM、ASO之间的区别?
- 如何设置快速启动栏 win7 快速启动栏 快速启动栏不见了
- Don't Starve,好脚本,好欢乐
- python 删除pdf页面_删除PDF其中几页的方法
- 如何在企业中从0-1建立一个数据/商业分析部门?
- 小红书算法sign php,小红书API签名算法分析