DNS-F

DNS-F (DNS Failover) is a DNS-based mechanism supported by Nacos, a dynamic service discovery and configuration management platform, to provide failover support for service discovery. It works by allowing services to use DNS queries to retrieve IP addresses of available service instances, ensuring resilience and reliability when instances become unavailable. Here’s a breakdown of what […] →Read more

The new and revised 15 factors

12 factor In the modern era, software is commonly delivered as a service: called web apps, or software-as-a-service. The twelve-factor app is a methodology for building software-as-a-service apps that: Use declarative formats for setup automation, to minimize time and cost for new developers joining the project; Have a clean contract with the underlying operating system, […] →Read more

RAG Architecture

A typical RAG application has two main components: Indexing: a pipeline for ingesting data from a source and indexing it. This usually happens offline. Retrieval and generation: the actual RAG chain, which takes the user query at run time and retrieves the relevant data from the index, then passes that to the model. The most […] →Read more

EnableAutoConfiguration 注解的原理

在 Spring Boot 中,@EnableAutoConfiguration 是一个核心注解,它实现了自动配置的功能。它的作用是根据项目中依赖的库以及应用上下文中的配置,自动配置 Spring 应用程序的上下文。以下是它的原理和运行机制: 1. @EnableAutoConfiguration 的定义 @EnableAutoConfiguration 注解本质上是一个复合注解,其源码如下: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { Class<?>[] exclude() default {}; String[] excludeName() default {}; } 主要包括以下两部分: • @AutoConfigurationPackage: 自动将当前类所在包及其子包中的所有组件注册到 Spring 容器。 • @Import(AutoConfigurationImportSelector.class): 导入了 AutoConfigurationImportSelector,用来加载所有可用的自动配置类。 2. 自动配置的核心机制 2.1 自动配置类的定义 自动配置类通过 spring.factories 文件定义,这个文件通常位于 META-INF 目录下。例如: org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration • 每个类都是一个自动配置类,Spring […] →Read more

Cloud Native

Cloud native practices empower organizations to develop, build, and deploy workloads in computing environments (public, private, hybrid cloud) to meet their organizational needs at scale in a programmatic and repeatable manner. It is characterized by loosely coupled systems that interoperate in a manner that is secure, resilient, manageable, sustainable, and observable. Cloud native technologies and […] →Read more

we live by an invisible sun within us

Life is a pure flame, and we live by an invisible sun within us 生命是一团纯净的火焰, 我们依靠自己内心看不见的太阳而生存 The vast, th unbounded prospect lies before us. →Read more

mybatis spring boot starter 是怎么做的

Overview 要与 Spring 一起使用 MyBatis,你至少需要一个 SqlSessionFactory 和一个 mapper 接口。 MyBatis-Spring-Boot-Starter 将会: 自动探测存在的 DataSource 将使用 SqlSessionFactoryBean 创建并注册一个 SqlSessionFactory 的实例,并将探测到的 DataSource 作为数据源 将创建并注册一个从 SqlSessionFactory 中得到的 SqlSessionTemplate 的实例 自动扫描你的 mapper,将它们与 SqlSessionTemplate 相关联,并将它们注册到Spring 的环境(context)中去,这样它们就可以被注入到你的 bean 中 MyBatis-Spring-Boot-Starter 是一个开箱即用的解决方案,用于将 MyBatis 与 Spring Boot 集成。它简化了 MyBatis 的配置和使用,同时充分利用了 Spring 的依赖注入和生命周期管理功能。以下是它的实现原理的详细解析: 自动配置的实现 MyBatis-Spring-Boot-Starter 利用了 Spring Boot 的自动配置机制,通过 @EnableAutoConfiguration 和 spring.factories 文件实现自动化配置。 spring.factories MyBatis-Spring-Boot-Starter […] →Read more

道与术:程序员的内功心法

在软件开发的江湖中,C/C++ 和 Java 就像少林与武当,一个偏向“硬功夫”,刀枪棍棒样样能打;另一个更讲究“以柔克刚”,推崇灵活多变的内功。而 Python、Go 和 Rust 则是近年崛起的少年高手,一个擅长速成、一个拥抱云原生,还有一个以内存安全闻名,都在争夺编程江湖的“武林盟主”之位。 尤其是在企业开发的武林中,Java 堪称“中原武林盟主”,它的招式(术)浩如烟海,从简单的“打狗棒法”(工具类库)到深奥的“九阴真经”(分布式架构)。门下弟子修炼轮子(第三方库)无数。然而,纵然 Java 的术法浩如烟海,如果只沉迷于术,终究如蜻蜓点水,难入武道真谛。只练招式不修内功,就像拿着绝世神兵却没有力气挥动,最终难逃江湖纷争的淘汰法则。 正如古人所言:“术乃器,道为心。器可换,道难变。” 术是什么?术是刀枪剑戟的武器。 术是工具、框架、库、配置文件。你可以把术看成是你手中的菜刀,某个问题来了,直接拿出来用,快速解决,效率拉满。 比如: • Spring:一个开箱即用的武林秘籍,简直是厨师的多功能锅,煎炒烹炸样样行。 • MyBatis:数据库操作的助力,能让你用 SQL 潇洒如风,甚至可以用 XML 来写代码。 • Spring Security:防盗、防抢、防 SQL 注入三件套。 • WebFlux:响应式编程的“闪电五连鞭”,性能杠杠的。 术学起来很爽,用起来很炫,解决问题很快,但如果你只会用术,可能就像一个拿着高级武器但内功薄弱的武者,风吹草动就可能翻车。 道是什么?道是你的内功心法。 道是一种理念、一种本质,超脱于术之上。它是程序员的大局观,是问题背后的普适法则。懂得了道,你就能从根本上理解术为什么存在、怎么优化甚至怎么重写它。 比如: • 依赖注入和控制反转(IoC) 是道,理解了它,你就能明白 Spring 为何如此设计。 • ORM 的本质 是道,懂了它,你就知道 JPA 和 MyBatis 的优劣。 • CAP 理论 是道,掌握了它,分布式系统中的“妥协艺术”尽在掌握。 • 响应式编程的多路复用原理 是道,搞懂了它,WebFlux […] →Read more

pydantic 对象的陷阱

陷阱 在 Python 中,类成员在所有实例之间是共享的,例如以下代码,friends 是一个类成员(而不是实例成员),所有 User 实例都会共享同一个 friends 列表。也就是说,对一个实例的 friends 列表进行修改,会影响到其他实例的 friends 列表。 这是因为 friends 是在类定义时定义的,是一个类级别的属性。 示例代码 from datetime import datetime from pydantic import BaseModel class User(BaseModel): id: int name: str = "Walter Fan" signup_ts: datetime | None = None friends: list[int] = [] user1 = User(id=1) user2 = User(id=2) user1.friends.append(2) print(user1.friends) # [2] print(user2.friends) […] →Read more

奇异模板模式(Curiously Recurring Template Pattern)

在 C++ 中,“奇异模板”(Curiously Recurring Template Pattern, 简称 CRTP)是一种常用的模板编程技巧,主要用于实现静态多态(static polymorphism)或为派生类提供通用的功能。它的名字来源于其递归的结构:一个类使用自己作为模板参数传递给基类。 CRTP 的基本结构 CRTP 的核心是定义一个基类模板,并让派生类作为模板参数传递给这个基类。 基本代码示例 #include <iostream> // 基类模板 template <typename Derived> class Base { public: void interface() { // 调用派生类的方法 static_cast<Derived*>(this)->implementation(); } // 默认实现(可选) void implementation() { std::cout << "Base implementation\n"; } }; // 派生类 class Derived : public Base<Derived> { public: void implementation() { […] →Read more