你好,我是Qiuner. 为帮助别人少走弯路和记录自己编程学习过程而写博客
这是我的 github https://github.com/Qiuner ⭐️
gitee https://gitee.com/Qiuner 🌹
如果本篇文章帮到了你 不妨点个赞吧~ 我会很高兴的 😄 (^ ~ ^)
想看更多 那就点个关注吧 我会尽力带来有趣的内容 😎
- 这篇中规中举,有不少bug记录与方便您复制的代码,相信一定能节省学习时间同时达到更好的效果
- 本博客要与原文档搭配使用 day03-微服务01 - 飞书云文档 (feishu.cn)
- 本来这个系列博客应该五月份出完,但有各种事情。所幸都安然度过 也不敢再标题写全网最快了 只写全网最全 😎
本篇文章的重点是 作业 trade客户端拆分与相应OpenFeign编写思路,您一看就能明白 我比老师讲的更详细 看完不会找我 就是这么自信 😎
封面颜色是 槐花黄绿
alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' alias dps='docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"' alias dis='docker images' # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi
source /root/.bashrc
解决方式
taskkill /f /t /im nginx.exe
软件测试之 性能测试 性能测试基础指标 Loadrunner、Jmeter等工具-CSDN博客
下面是修改的配置文件
server: port: 8081 spring: application: name: item-service profiles: active: dev datasource: url: jdbc:mysql://${hm.db.host}:3306/hm-item?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver username: root password: ${hm.db.pw} mybatis-plus: configuration: default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler global-config: db-config: update-strategy: not_null id-type: auto logging: level: com.hmall: debug pattern: dateformat: HH:mm:ss:SSS file: path: "logs/${spring.application.name}" knife4j: enable: true openapi: title: 黑马商城商品管理接口文档 description: "黑马商城商品管理接口文档" email: zhanghuyi@itcast.cn concat: Qiuner url: https://www.itcast.cn version: v1.0.0 group: default: group-name: default api-rule: package api-rule-resources: - com.hmall.item.controller
@Bean public RestTemplate restTemplate() { return new RestTemplate(); }
private void handleCartItems(List vos) { // TODO 1.获取商品id Set itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet()); // 2.查询商品 原代码 // List items = itemService.queryItemByIds(itemIds); // 这里查询商品不再从本地数据库中查询 而是发送请求 让远程服务器接受来查询 // 使用RestTemplate发送请求 ResponseEntity> response= restTemplate.exchange( "http://localhost:8081/items?ids={ids}", HttpMethod.GET, null, new ParameterizedTypeReference>() { }, Map.of("ids",CollUtils.join(itemIds,",")) ); // 解析响应 if (!response.getStatusCode().is2xxSuccessful()){ // 查询失败 return; } //这里做转换 List items =response.getBody(); if (CollUtils.isEmpty(items)) { return; } // 3.转为 id 到 item的map Map itemMap = items.stream().collect(Collectors.toMap(ItemDTO::getId, Function.identity())); // 4.写入vo for (CartVO v : vos) { ItemDTO item = itemMap.get(v.getItemId()); if (item == null) { continue; } v.setNewPrice(item.getPrice()); v.setStatus(item.getStatus()); v.setStock(item.getStock()); } }
http://192.168.197.130:8848/nacos
这里的discoveryClient是一个顶级接口,所有的服务注册中心都实现了这个接口
instances.get这里是选择负载均衡算法
mvn clean install
org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-loadbalancer
day03-微服务01 - 飞书云文档 (feishu.cn)
@FeignClient("item-service") public interface ItemClient { @GetMapping("/items") List queryItemByIds(@RequestParam("ids") Collection ids); }
类部分
@FeignClient("item-service")
:定义一个 Feign 客户端,服务名称为 item-service
,这个名称应该与服务注册中心(如 Nacos)中的服务名称一致。
public interface ItemClient
:声明一个接口,表示这个接口将包含远程服务的 API 方法。
方法部分
@GetMapping("/items")
:定义一个 GET 请求,映射到 item-service
服务的 /items
端点。
List
:
queryItemByIds
表示查询多个商品 ID 的信息。@RequestParam("ids") Collection ids
:使用 @RequestParam
注解表示这是一个查询参数,参数名称为 ids
,类型是 Collection
。List
:方法返回一个 ItemDTO
对象的列表。一:阅读原本购物车代码
发现有一个批量删除的代码,那我们就用这个了
本方法没有返回值,所以得出的client代码如下
package com.hmall.api.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.Collection; @FeignClient("cart-service") public interface CartClient { @GetMapping("/carts") void deleteCartItemByIds(@RequestParam("ids") Collection ids); }
cartClient.deleteCartItemByIds(itemIds);
CartClient
package com.hmall.api.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.Collection; @FeignClient("cart-service") public interface CartClient { @GetMapping("/carts") void deleteCartItemByIds(@RequestParam("ids") Collection ids); }
ItemClient
package com.hmall.api.client; import com.hmall.api.dto.ItemDTO; import com.hmall.api.dto.OrderDetailDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import java.util.Collection; import java.util.List; @FeignClient("item-service") public interface ItemClient { @GetMapping("/items") List queryItemByIds(@RequestParam("ids") Collection ids); @PutMapping("/items/stock/deduct") void deductStock(@RequestBody List items); }