Spring cloud:支付微服务-支付
2023-09-27 14:23:50 时间
环境
- spring cloud Edgware.SR6
- jdk 7
- sts 4.6.0
- mysql 5.7
背景
搭建支付微服务的环境。
搭建步骤
数据层
package jiangbo.springcloud.dao;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import jiangbo.springcloud.entity.PaymentInfo;
@Repository
public class PaymentDaoImpl implements PaymentDao {
private static final String QUERY_ALL_PAYMENT_SQL = "select * from payment_info";
private static final String QUERY_PAYMENT_BY_ID_SQL = QUERY_ALL_PAYMENT_SQL + " where id = ?";
private static final RowMapper<PaymentInfo> ROW_MAPPER = new BeanPropertyRowMapper<>(PaymentInfo.class);
private final JdbcTemplate jdbcTemplate;
public PaymentDaoImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<PaymentInfo> queryAllPayments() {
return jdbcTemplate.query(QUERY_ALL_PAYMENT_SQL, ROW_MAPPER);
}
@Override
public long insertPaymentInfo(PaymentInfo paymentInfo) {
paymentInfo.setStatus("SUCCESS");
return new SimpleJdbcInsert(jdbcTemplate)
.withTableName("payment_info")
// 指定主键
.usingGeneratedKeyColumns("id")
// 更新的列
.usingColumns("order_id", "amount", "status")
// 参数
.executeAndReturnKey(new BeanPropertySqlParameterSource(paymentInfo)).longValue();
}
@Override
public PaymentInfo queryPaymentInfo(long id) {
return jdbcTemplate.queryForObject(QUERY_PAYMENT_BY_ID_SQL, ROW_MAPPER, id);
}
}
服务层
package jiangbo.springcloud.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import jiangbo.springcloud.dao.PaymentDao;
import jiangbo.springcloud.entity.PaymentInfo;
import jiangbo.springcloud.service.PaymentService;
@Service
public class PaymentServiceImpl implements PaymentService {
private final PaymentDao paymentDao;
public PaymentServiceImpl(PaymentDao paymentDao) {
this.paymentDao = paymentDao;
}
@Override
public List<PaymentInfo> queryAllPayments() {
return paymentDao.queryAllPayments();
}
@Override
public PaymentInfo insertPaymentInfo(PaymentInfo paymentInfo) {
long id = paymentDao.insertPaymentInfo(paymentInfo);
return paymentDao.queryPaymentInfo(id);
}
}
控制层
package jiangbo.springcloud.controller;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jiangbo.springcloud.entity.PaymentInfo;
import jiangbo.springcloud.service.PaymentService;
@RestController
@RequestMapping("/payment")
public class PaymentContrller {
private final PaymentService paymentService;
public PaymentContrller(PaymentService paymentService) {
this.paymentService = paymentService;
}
@PostMapping
public PaymentInfo newPaymentInfo(@RequestBody PaymentInfo paymentInfo) {
return paymentService.insertPaymentInfo(paymentInfo);
}
@GetMapping
public List<PaymentInfo> allPayemtns() {
return paymentService.queryAllPayments();
}
}
验证
使用命名进行数据的新增,看到如下的结果,则证明成功:
curl -H "Content-Type: application/json" -X POST --data '{"orderId":8,"amount":"8.88"}', http://localhost:4420/payment
{"id":2,"orderId":8,"amount":"8.88","status":"SUCCESS","createTime":1587224718000}
相关文章
- 微服务下前后端分离的统一认证授权服务,基于Spring Security OAuth2 + Spring Cloud Gateway实现单点登录
- 微服务容器化迁移——在K8s中部署Spring Cloud
- Spring 中那些让你爱不释手的代码技巧
- Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制
- Spring Cloud Gateway实战案例(限流、熔断回退、跨域、统一异常处理和重试机制)
- 使用Ocelot、IdentityServer4、Spring Cloud Eureka搭建微服务网关:Step by Step(一)
- Spring Cloud-新一代Web框架微服务
- Spring Cloud-Eureka 服务注册中心
- 微服务架构的基础框架选择:Spring Cloud还是Dubbo?
- Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud 系列之 Eureka 实现服务注册与发现
- Spring Cloud Eureka集群 动态扩展新节点
- 【Spring Cloud】如何把Feign默认的HTTP客户端URLConnection更换成支持连接池的Apache HttpClient或OKHttp
- Spring Cloud Alibaba 微服务组件 Nacos 注册中心(三)