首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Spring Cloud工程完善

  • 25-02-15 07:41
  • 3743
  • 10316
blog.csdn.net

目录

完善订单服务

启动类

配置文件

实体类

Controller

Service

Mapper

测试运行

完成商品服务

启动类

配置文件

实体类

Controller

Service

Mapper

测试运行

远程调用

需求

实现

1.定义RestTemplate

2.修改order-service中的OrderService

测试运行

RestTemplate

什么是REST?

什么是RESTful?

RESTful API 缺点:


完善订单服务

启动类
  1. package order;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class OrderServiceApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(OrderServiceApplication.class, args);
  8. }
  9. }
配置文件
  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=false
  6. username: root
  7. password: #密码
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. # 设置 Mybatis 的 xml 保存路径
  10. mybatis:
  11. configuration: # 配置打印 MyBatis 执行的 SQL
  12. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  13. map-underscore-to-camel-case: true #自动驼峰转换
实体类
  1. package order.model;
  2. import lombok.Data;
  3. import java.util.Date;
  4. @Data
  5. public class OrderInfo {
  6. private Integer id;
  7. private Integer userId;
  8. private Integer productId;
  9. private Integer num;
  10. private Integer price;
  11. private Integer deleteFlag;
  12. private Date createTime;
  13. private Date updateTime;
  14. }
Controller
  1. package order.controller;
  2. import order.model.OrderInfo;
  3. import order.service.OrderService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RequestMapping("/order")
  9. @RestController
  10. public class OrderController {
  11. @Autowired
  12. private OrderService orderService;
  13. @RequestMapping("/{orderId}")
  14. public OrderInfo getOrderById(@PathVariable("orderId") Integer orderId){
  15. return orderService.selectOrderById(orderId);
  16. }
  17. }
Service
  1. package order.service;
  2. import order.mapper.OrderMapper;
  3. import order.model.OrderInfo;
  4. import order.model.ProductInfo;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.web.client.RestTemplate;
  8. @Service
  9. public class OrderService {
  10. @Autowired
  11. private OrderMapper orderMapper;
  12. public OrderInfo selectOrderById(Integer orderId) {
  13. OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
  14. return orderInfo;
  15. }
  16. }
Mapper
  1. package order.mapper;
  2. import order.model.OrderInfo;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. @Mapper
  6. public interface OrderMapper {
  7. @Select("select * from order_detail where id=#{orderId}")
  8. OrderInfo selectOrderById(Integer orderId);
  9. }
测试运行

完成商品服务

启动类
  1. package product;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class ProductServiceApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ProductServiceApplication.class, args);
  8. }
  9. }
配置文件
  1. server:
  2. port: 9090
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://127.0.0.1:3306/cloud_product?characterEncoding=utf8&useSSL=false
  6. username: root
  7. password: #密码
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. # 设置 Mybatis 的 xml 保存路径
  10. mybatis:
  11. configuration: # 配置打印 MyBatis 执行的 SQL
  12. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  13. map-underscore-to-camel-case: true #自动驼峰转换
实体类
  1. package product.model;
  2. import lombok.Data;
  3. import java.util.Date;
  4. @Data
  5. public class ProductInfo {
  6. private Integer id;
  7. private String productName;
  8. private Integer productPrice;
  9. private Integer state;
  10. private Date createTime;
  11. private Date updateTime;
  12. }
Controller
  1. package product.controller;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import product.model.ProductInfo;
  8. import product.service.ProductService;
  9. @Slf4j
  10. @RequestMapping("/product")
  11. @RestController
  12. public class ProductController {
  13. @Autowired
  14. private ProductService productService;
  15. @RequestMapping("/{productId}")
  16. public ProductInfo getProductById(@PathVariable("productId") Integer productId){
  17. log.info("接收到参数"+productId);
  18. return productService.selectProductById(productId);
  19. }
  20. }
Service
  1. package product.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import product.mapper.ProductMapper;
  5. import product.model.ProductInfo;
  6. @Service
  7. public class ProductService {
  8. @Autowired
  9. private ProductMapper productMapper;
  10. public ProductInfo selectProductById(Integer id){
  11. return productMapper.selectProductById(id);
  12. }
  13. }
Mapper
  1. package product.mapper;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import org.apache.ibatis.annotations.Select;
  4. import product.model.ProductInfo;
  5. @Mapper
  6. public interface ProductMapper {
  7. @Select("select * from product_detail where id = #{id}")
  8. ProductInfo selectProductById(Integer id);
  9. }
测试运行

远程调用

需求

根据订单查询订单信息时, 根据订单⾥产品ID, 获取产品的详细信息.

实现

实现思路: order-service服务向product-service服务发送⼀个http请求, 把得到的返回结果, 和订单结果融合在⼀起, 返回给调⽤⽅。

实现⽅式: 采⽤Spring 提供的RestTemplate。

1.定义RestTemplate
  1. package order.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.client.RestTemplate;
  5. @Configuration
  6. public class BeanConfig {
  7. @Bean
  8. public RestTemplate restTemplate(){
  9. return new RestTemplate();
  10. }
  11. }
2.修改order-service中的OrderService
  1. package order.service;
  2. import order.mapper.OrderMapper;
  3. import order.model.OrderInfo;
  4. import order.model.ProductInfo;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.web.client.RestTemplate;
  8. @Service
  9. public class OrderService {
  10. @Autowired
  11. private OrderMapper orderMapper;
  12. @Autowired
  13. private RestTemplate restTemplate;
  14. public OrderInfo selectOrderById(Integer orderId){
  15. OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
  16. String url = "http://127.0.0.1:9090/product/"+orderInfo.getProductId();
  17. ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
  18. orderInfo.setProductInfo(productInfo);
  19. return orderInfo;
  20. }
  21. }
测试运行

RestTemplate

RestTemplate 是从 Spring3.0 开始⽀持的⼀个 HTTP 请求⼯具, 它是⼀个同步的 REST API 客⼾端, 提供了常⻅的REST请求⽅案的模版。

什么是REST?

REST(Representational State Transfer), 表现层资源状态转移.
REST是由HTTP的主要设计者Roy Fielding博⼠在2000年他的博⼠论⽂中提出来的⼀种软件架构⻛格.

这⾥⾯主要有三个概念:

1. 资源: ⽹络上的所有事物都可以抽象为资源, 每个资源都有⼀个唯⼀的资源标识符(URI)
2. 表现层: 资源的表现形式, ⽐如⽂本作为资源, 可以⽤txt格式表现, 也可以通过HTML, XML, JSON等格式来表现, 甚⾄以⼆进制的格式表现.
3. 状态转移: 访问URI, 也就是客⼾端和服务器的交互过程. 客⼾端⽤到的⼿段,只能是HTTP协议. 这个过程中, 可能会涉及到数据状态的变化. ⽐如对数据的增删改查, 都是状态的转移.

REST 是⼀种设计⻛格, 指资源在⽹络中以某种表现形式进⾏状态转移.
简单来说: REST描述的是在⽹络中Client和Server的⼀种交互形式, REST本⾝不实⽤,实⽤的是如何设计 RESTful API(REST⻛格的⽹络接⼝).

什么是RESTful?

REST 是⼀种设计⻛格, 并没有⼀个明确的标准. 满⾜这种设计⻛格的程序或接⼝我们称之为RESTful(从单词字⾯来看就是⼀个形容词). 所以RESTful API 就是满⾜REST架构⻛格的接⼝.
RESTful ⻛格⼤致有以下⼏个主要特征:

1. 资源: 资源可以是⼀个图⽚, ⾳频, 视频或者JSON格式等⽹络上的⼀个实体, 除了⼀些⼆进制的资源外普通的⽂本资源更多以JSON为载体、⾯向⽤⼾的⼀组数据(通常从数据库中查询⽽得到) .

2. 统⼀接⼝: 对资源的操作. ⽐如获取, 创建, 修改和删除. 这些操作正好对应HTTP协议提供的GET、POST、PUT和DELETE⽅法. 换⾔⽽知,如果使⽤RESTful⻛格的接⼝, 从接⼝上你可能只能定位其资源,但是⽆法知晓它具体进⾏了什么操作,需要具体了解其发⽣了什么操作动作要从其HTTP请求⽅法类型上进⾏判断。

RestTemplate 是Spring提供, 封装HTTP调⽤, 并强制使⽤RESTful⻛格. 它会处理HTTP连接和关闭,只需要使⽤者提供资源的地址和参数即可.

RESTful实践

RESTful⻛格的API 固然很好很规范, 但⼤多数互联⽹公司并没有按照其规则来设计, 因为REST是⼀种风格,⽽不是⼀种约束或规则, 过于理想的RESTful API 会付出太多的成本.

RESTful API 缺点:

1. 操作⽅式繁琐, RESTful API通常根据GET, POST, PUT, DELETE 来区分对资源的操作动作. 但是HTTP Method 并不可直接⻅到, 需要通过抓包等⼯具才能观察. 如果把动作放在URL上反⽽更加直观, 更利于团队的理解和交流.
2. ⼀些浏览器对GET, POST之外的请求⽀持不太友好, 需要额外处理.
3. 过分强调资源. ⽽实际业务需求可能⽐较复杂, 并不能单纯使⽤增删改查就能满⾜需求, 强⾏使⽤RESTful API会增加开发难度和成本。 

所以, 在实际开发中, 如果业务需求和RESTful API不太匹配或者很⿇烦时, 也可以不⽤RESTful API. 如果使⽤场景和REST⻛格⽐较匹配, 就可以采⽤RESTful API.
总之: ⽆论哪种⻛格的API, 都是为了⽅便团队开发, 协商以及管理, 不能墨守成规. 尽信书不如⽆书, 尽信规范不如⽆规范。 

欢迎大家访问我的主页---》链接

【微信号】Q3265047996
微信名片
注:本文转载自blog.csdn.net的新绿MEHO的文章"https://blog.csdn.net/wmh_1234567/article/details/142313419"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

103
后端
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top