IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    技术阅读周刊第十二期

    crossoverJie发表于 2023-12-29 06:10:07
    love 0

    image.png

    技术阅读周刊,每周更新。

    历史更新

    • 20231201:第八期
    • 20231215:第十期
    • 20231122:第十一期

    Deno vs Go: Native hello world performance | Tech Tonic

    URL: https://medium.com/deno-the-complete-reference/deno-vs-go-native-hello-world-performance-c57d8fc13c75

    使用 Deno 和 Go 进行基本的接口对比

    • MacBook Pro M2 with 16GB of RAM
    • Deno v1.38.0
    • Go v1.21.3

    都是最简单的 httpServer:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Deno.serve({
    port: 3000,
    }, (req) => {
    try {
    const pathName = new URL(req.url).pathname;
    if (pathName !== "/") {
    return new Response(null, { status: 404 });
    }
    return new Response("Hello world!");
    } catch (e) {
    return new Response(null, { status: 500 });
    }
    });
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package main

    import (
    "io"
    "net/http"
    )

    func main() {
    http.HandleFunc("/", helloWorld)
    http.ListenAndServe(":3000", nil)
    }

    func helloWorld(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello world!")
    }

    image.png

    总的来说 Deno 比 Go 慢了 30% 左右,但 CPU 占有率比 Go 更少,Go 的内存占用更低。

    这个对比就图一乐。

    Top 7 Spring Boot Design Patterns Unveiled | by Dharmendra Awasthi | Dec, 2023 | Stackademic

    URL: https://blog.stackademic.com/top-7-spring-boot-design-patterns-unveiled-4a2569f8d324

    7 个我们可以学习的 Spring Boot 的设计模式

    Singleton Pattern 单例模式

    这个没啥好说的,面试都被讲烂了,依然很经典。
    当我们使用这些注解声明一个 Bean 时@Component, @Service, @Repository, or @Controller,就会被创建一个单例对象注入到 IOC 容器中。

    Factory Pattern 工厂模式

    Spring 也提供了工厂模式的接口,我们可以自定义创建逻辑:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import org.springframework.beans.factory.FactoryBean;

    public class MyFactoryBean implements FactoryBean<MyBean> {
    @Override
    public MyBean getObject() throws Exception {
    // Custom logic to create and return MyBean instance
    return new MyBean();
    }

    @Override
    public Class<?> getObjectType() {
    return MyBean.class;
    }

    @Override
    public boolean isSingleton() {
    return true; // Or false, depending on your bean's scope
    }
    }

    Builder 创建者模式

    这个其实不算是 Spring 所提供的,但确实很好用;通常用于创建需要很多可选参数的对象时使用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import lombok.Builder;
    import lombok.Data;

    @Data
    @Builder
    public class User {
    private String username;
    private String email;
    private int age;
    // Other fields
    }

    User user = User.builder()
    .username("john_doe")
    .email("john@example.com")
    .age(30)
    .build();

    Proxy 代理模式

    代理模式在 spring 中通常用于 AOP 切面,可以实现一些通用的非业务逻辑功能;比如日志、缓存、安全检测等:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;

    @Aspect
    @Component
    public class LoggingAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void beforeServiceMethods() {
    // Logic to be executed before service methods
    System.out.println("Logging before service methods...");
    }
    }

    Observe 观察者模式

    本质上是将业务解耦,生产者发布事件,订阅者接收事件,只是 spring 帮我们封装好了逻辑。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    import org.springframework.context.ApplicationEvent;

    public class OrderPlacedEvent extends ApplicationEvent {
    private final Order order;

    public OrderPlacedEvent(Object source, Order order) {
    super(source);
    this.order = order;
    }

    // Getters for order information
    }


    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.stereotype.Service;

    @Service
    public class OrderService {
    private final ApplicationEventPublisher eventPublisher;

    public OrderService(ApplicationEventPublisher eventPublisher) {
    this.eventPublisher = eventPublisher;
    }

    public void placeOrder(Order order) {
    // Logic to place order

    // Publish OrderPlacedEvent
    eventPublisher.publishEvent(new OrderPlacedEvent(this, order));
    }
    }


    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;

    @Component
    public class EmailService {

    @EventListener
    public void sendEmailOnOrderPlacement(OrderPlacedEvent event) {
    Order order = event.getOrder();
    // Logic to send email based on the placed order
    }
    }

    转转一体化监控系统——Prometheus·Grafana成本治理_TakinTalks稳定性技术交流平台

    URL: https://news.shulie.io/?p=8229
    image.png

    链接里有 B 站视频,文字版链接:https://mp.weixin.qq.com/s/FySeVBL7EfihOlNDBvAPpw

    转转的监控方案

    • 基于 Prometheus 架构(确实已经是监控领域的标准了)
    • 使用 M3DB 替换了单机的 Prometheus。
      • 我们使用 VM 替换的 Prometheus,转转没有选择 VM 是因为 M3DB 的压缩率更高。
    • 采用 Push 模型推送数据
      • 由 SDK 进行推送,对业务无感知
      • 省略了注册中心,改为了数据库存储服务节点信息。
        • 由于我们使用了 kubernetes,所以是基于 kubernetes 的 SD 实现的服务发现。
        • 所以我们采用的也是 Pull 拉取模型
    • 重写了告警系统,Prometheus 自带的告警系统存在学习难度大等问题。

    文章链接:

    • https://medium.com/deno-the-complete-reference/deno-vs-go-native-hello-world-performance-c57d8fc13c75
    • https://blog.stackademic.com/top-7-spring-boot-design-patterns-unveiled-4a2569f8d324
    • https://news.shulie.io/?p=8229

    #Newletters



沪ICP备19023445号-2号
友情链接