黑马商城项目(三)微服务

一、单体架构

测试高并发软件

二、微服务

三、SpringCloud

四、微服务拆分

黑马商城模块:

服务拆分原则:

拆分服务:

独立project:

maven聚合:

拆分案例:

远程调用:

java 复制代码
package com.hmall.cart.config;

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

@org.springframework.context.annotation.Configuration
public class Configuration {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
java 复制代码
@Service
@RequiredArgsConstructor  //自动生成必备参数的构造函数
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    /*private final IItemService itemService;*/

    // @Autowired  Spring不建议使用@Autowired
    private final RestTemplate restTemplate;

    /*Spring建议使用构造函数注入*/
/*    public CartServiceImpl(RestTemplate restTemplate){
        this.restTemplate=restTemplate;
    }*/
}
java 复制代码
 private void handleCartItems(List<CartVO> vos) {
        // 1.获取商品id
        Set<Long> itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet());
        // 2.查询商品
        //List<ItemDTO> items = itemService.queryItemByIds(itemIds);
        //2.1 利用RestTemplate发送http请求,得到http响应
        ResponseEntity<List<ItemDTO>> response = restTemplate.exchange(
                "http://localhost:8081/items?ids={ids}",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ItemDTO>>() {
                }, //利用反射拿到这个对象上的泛型
                Map.of("ids", CollUtil.join(itemIds, ","))
        );
        //2.2解析响应
        if(!response.getStatusCode().is2xxSuccessful()){
            //查询失败 直接结束
            return;
        }
        List<ItemDTO> items = response.getBody();

        if (CollUtils.isEmpty(items)) {
            return;
        }
        // 3.转为 id 到 item的map
        Map<Long, ItemDTO> 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());
        }
    }

逻辑:

五、服务治理

上文提到的远程调用带来的问题:

注册中心:

Nacos注册中心:

bash 复制代码
docker run -d \
--name nacos \
--env-file ./nacos/custom.env \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
--restart=always \
nacos/nacos-server:v2.1.0-slim

服务注册:

多实例部署:

服务发现:

六、OpenFeign

快速入门:



请求路径参数通过SpringMVC注解@GetMapping以及@RequestParam完成替代

请求参数通过调用该接口方法时传递的参数完成替代

返回值类型通过该接口定义的抽象方法的返回值类型完成替代

从而实现整体替代,简化代码

java 复制代码
package com.hmall.cart.client;

import com.hmall.cart.domain.dto.ItemDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Collection;
import java.util.List;

@FeignClient("item-service")
public interface ItemClient {
    @GetMapping("/items")
    List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);
}
java 复制代码
    private final ItemClient itemClient;    //自动注入

private void handleCartItems(List<CartVO> vos) {
        // 1.获取商品id
        Set<Long> itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet());
        // 2.查询商品
        //2.1 根据服务名称获取服务的实例列表
     /*   List<ServiceInstance> instances = discoveryClient.getInstances("item-service");
        if(CollUtil.isEmpty(instances)){
            //判断为空 直接结束
            return;
        }
        //2.2 手写负载均衡,从实例列表中挑选一个实例
        ServiceInstance instance = instances.get(RandomUtil.randomInt(0, instances.size()));

        URI uri = instance.getUri(); //主机地址 http://localhost:8081


        //List<ItemDTO> items = itemService.queryItemByIds(itemIds);
        //2.3 利用RestTemplate发送http请求,得到http响应
        ResponseEntity<List<ItemDTO>> response = restTemplate.exchange(
                uri+"/items?ids={ids}",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ItemDTO>>() {
                }, //利用反射拿到这个对象上的泛型
                Map.of("ids", CollUtil.join(itemIds, ","))
        );
        //2.4解析响应
        if(!response.getStatusCode().is2xxSuccessful()){
            //查询失败 直接结束
            return;
        }
        List<ItemDTO> items = response.getBody();*/

        List<ItemDTO> items = itemClient.queryItemByIds(itemIds);

        if (CollUtils.isEmpty(items)) {
            return;
        }
        // 3.转为 id 到 item的map
        Map<Long, ItemDTO> 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());
        }
    }

底层原理:

代码中通过 itemClient对象来调用接口方法时,实际上它是一个动态代理对象。

动态代理对象底层逻辑都是由 InvocationHandler 实现

这里的 FeignInvocationHandlerReflectiveFeign 的一个内部类,实现了 InvocationHandler 接口, InvocationHandler 接口中的invoke 方法就是起到了代理的作用 : 所有的被代理对象中的所有业务(方法) 都会通过 invoke 方法实现代理,除了一些基本方法(比如 equals, hashCode ,toString

InvocationHandler 接口的实现类 FeignInvocationHandler 中重写对应invoke 从而实现代理的方法如下:

最后调用对应的方法发送请求(Client

连接池:

OpenFeign底层发送请求使用的是 Client

OpenFeign整合并使用连接池步骤:

最佳实践:

拆分的最佳方式:

方法一:

方法二:

按照第二种方法实践:

日志输出:

七、服务拆分作业

相关推荐
自由生长202415 小时前
从Web网站回退到从命令行:用领域驱动设计构建软件最关键的业务内核
架构
Nandeska15 小时前
1、全面理解MySQL的架构
架构
G311354227315 小时前
Linux 内核设计中的核心思想与架构原则
linux·架构·php
运维栈记16 小时前
如何排错运行在Kubernetes集群中的服务?
云原生·容器·kubernetes
Xの哲學18 小时前
Linux VxLAN深度解析: 从数据平面到内核实现的全面剖析
linux·服务器·算法·架构·边缘计算
阿里云云原生18 小时前
直播回顾丨详解阿里云函数计算 AgentRun,手把手带你走进 Agentic AI 生产时代
云原生
2301_7683502318 小时前
MySQL架构长啥样?
架构
古城小栈18 小时前
微服务网格:Istio 流量管理实战
微服务·架构·istio
CloudWeGo20 小时前
Volo 新能力:面向易用性与性能的 HTTP & RPC 框架迭代
架构
^_scv_^20 小时前
QEMU-RISCV平台opensbi代码分析(2)
linux·架构·risc-v