🔥 面试 | Spring Cloud 相关



项目场景

如果Eureka Server 挂了,服务还能互相调用吗?

If the Eureka Server goes down, services can still call each other in the short term because Eureka clients cache the registry locally.

However, over time, the cache may become stale, causing failed calls. In production, we usually deploy Eureka Server in a cluster to avoid single point of failure.

如果 MQ 堆积了几百万消息,怎么解决?

分析原因(消费端/生产端) -> 消费端增加处理/生产端限流 + 死信/监控

If millions of messages are piled up in the MQ, the first step is to analyze the cause, usually either insufficient consumer throughput or traffic spike on the producer side.

In the short term, we can add more consumers(such as pods or threads), enable batch consumption, or throttle producers.

For long-term solutions, we should optimize consumer logic, increase parallelism by splitting queues and partitioning, and use dead-letter queues plus monitoring to prevent future backlogs.

Using dead-letter queues means the messages reach the queue but there are no consumers to consume them. And We configure a delay time to control when the message can be delivered to the specified queue with routing key. Thus we accomplish to avoid the excessive concurrency.

如果 MyBatis 查询很慢,怎么排查?

应用层sql -> 数据库explain -> 系统连接

If Mybatis query is slow, I would first check at the application level whether the generated SQL is correct, avoiding issues like N+1 queries or inefficient pagination.

Then I would run the final SQL directly in the database and use EXPLAIN to see if it’s missing indexes or blocked by locks.

Finally, I’d check system-level factors like the connection pool or network letency.

The typical solutions are optimizing SQL and indexes, avoiding N+1 queries, using database-native pagination, and applying caching for hot data.

如果 Pod 内存不断飙升,直到 OOMKilled,怎么分析?

底层k8s -> 应用层JVM -> 业务层

If a Pod’s memory keeps growing until it gets OOMKilled, I would first check in Kubernetes whether the memory limit is too low.

Then I’d look at monitoring to see if the growth is sudden(possible memory leak) or load-driven(data accumulation).

For application-level leaks, I’d export a heap dump and analyze it with tools like jmap to check for issues like unbounded caches, unclosed connections, or objects not released.

If it’s caused by business load, such as MQ messages are not consumed in time, I’d optimize the consumption logic or scale out the service.

The solutions usually involve setting memory limits, optimizing memory usage in the code, and setting cache expiration or current limiting when necessary.

如果 CI/CD 部署失败,如何快速回滚?

first, check the failure occurs in the build stage or the deployment stage.

A build failure does’t affect production, so I just fix and rebuild.

If the deployment fails, We usually use kubernetes rollout undo to quickly revert to the previous version, and it will switch to the last stable environment.

如果 Spring Boot 服务启动很慢,可能的原因有哪些?

Sping Boot applications may start slowly for several reasons:

too many dependencies and bean initializations,
slow database connections or schema auto-generation,
delays from external services like Eureka or Redis,
heavy logging or reflection overhead,
heavy custom initialization logic such as large cache preloading,
pod startuping loads too many configmap or secret.

To troubleshoot, I usually check the logs to see where the application is stuck, use debug mode, or analyze by the startup actuator endpoint.

Optimizations include reducing dependency scope, removing unnecessary auto-configurations, tuning database connections, and deferring heavy initialization tasks.

Spring boot

Spring Boot 自动装配原理是什么?

Spring Boot is a rapid development framework based on Spring. It can accomplish automatic configuration, embedded container, fast startup, and easy integration with spring cloud.

Its automatic configuration is based on @EnableAutoConfiguration and spring.factories. Spring Boot will scan the configuration classes under the dependencies at startup and automatically assembles the beans that meet the requirements.

@SpringBootApplication 包含了哪些注解?

@SpringBootApplication is the core annotation of Spring Boot. It consists of @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan.

Spring Boot 如何做配置管理(application.yml、Profile、多环境切换)?

Spring Boot uses application.yaml or application.rpoperties to manage the configuration.

It supports multi-environment configuration via Profile and can maps the configuration to an object via @ConfigurationProperties.

常见 Starter 原理?自定义 Starter 怎么写?

Starter is based on @EnableAutoConfiguration and SpringFactoriesLoader. It dynamicly load configuration classes through confitional annotation to achieve automatic configuration.

we can define a starter by 4 steps:

  1. write a automatic configuration class,
  2. write a property binding class,
  3. regiter in spring.factories or AutoConfiguration.imports
  4. package the codes to a dependency, then we can import it in the project.

Spring Boot 如何实现热部署?

如何处理配置中心(Spring Cloud Config / Nacos)的动态刷新?

In the Spring Cloud Config, we can dynamicly refresh the configurations by @RefreshScpoe annotation and actuator or Spring Cloud Bus.

In Nacos, the configuration supports automatic refresh by default.

Eureka

什么是服务注册与发现?Eureka 的工作原理?

The service instance register the infomation to the Eureka Server when start up, and keep alive by heartbeat mechanism. the consumer can get the service address lists by Eureka with load balance.

Eureka choose AP, which ensures high availability. And it can avoid
services being mistakenly rejected on a large scale through self-protection mode.

Eureka Client 向 Eureka Server 注册和心跳机制是怎样的?

The client will send a heartbeat meassage to keep alive status per 30s. Eureka server will mark it expiration if the service doesn’t send heartbeat in 90s.

什么是 CAP 定理?Eureka 为什么选择 AP?

CAP means consistency, availability and partition tolerance.

Eureka chooses AP , which ensures high availability and partition tolerance, but sacrifices strong consistency. When Eureka fails, the service is still available instead of directly rejecting the request. This action will provide a better user experience.

ZooKeeper chooses CP, which means it ensures the service data has consistency, but when some errors occurs at Zookeeper, the requests will be rejected.

Eureka 和 Zookeeper、Nacos 的区别?

Eureka 服务下线 / 健康检查机制?

There are two type of Eureka offline mechanism. Active offline and Passive offline. Active offline means the service sends an cancel request to the Eureka server. Passive offline means Eureka server cleans the services which don’t send a heartbeat over 90s.

The Eureka default health check mechanism is heartbeat. but it can be combined with Spring Boot Actuator health check. We can configure and enable the healthcheck on the services, and then the service will report Actuator health status to Eureka server.

Eureka 自我保护机制是怎么实现的?

Eureka will count the heartbeat ratio in the last 15 minutes, if it is lower than 85% by default, Eureka will enter self-protection mode.

under this mode, Eureka won’t remove any services in order to protect AP and won’t return its original mode until the network is restored.

消息队列

MQ 的应用场景有哪些?为什么要用 MQ?

Using MQ can accomplish asynchronous decoupling. for example, our payment system sends a cross-border transfer, then it needs to generate and send a swift message to the clearing system, record
this transfer flow and inform account manager. MQ can process these operations asynchronously after core system notifies MQ.

The clearing system is responsible for message delivery, the risk control system is responsible for verification, and the notification system is responsible for push notification. This decouples core system, reduces peak traffic and protects the clearing channel, ensures message delivery, and meets audit trail requirements.

Handle peak traffic situations. for example, when a large number of meassges enter the system, they can first be delivered to the MQ queue, and then be smoothly sent to the clearing system for consumption.

MQ can copy messages to the log system to ensure full link traceablity. and It can meet compliance, auditing and risk control needs.

RabbitMQ 的交换机类型(Direct/Topic/Fanout/Headers)的区别?

RabbitMQ provides four common exchange types:
Direct(for exact matching by routing key),
Topic(for matching with regex),
Fanout(for broadcast mode that will inform every consumer),
Headers(for matching based on message attributes).

Direct and Topic are most commonly used in general business scenarios.

如何保证消息 不丢失?

retry mechanism, acknowledgement mechanism

如何避免消息 重复消费?

MQ cann’t avoid duplicate delivery, so idempotence must be guaranteed on the consumer side. Common practices include using unique business IDs(such as serial numbers), database unique constaints, or Redis deduplication to ensure that messages are only rocessed once.

如何保证消息的 顺序性?

partition based on business keys(such as account IDs) ensures that individual keys are ordered and the entire process can be run in parallel.

什么是消息积压?如何解决?

The producer send too fast and the consumer has no time to process.

So we can solve this problem from consumer side and producer side.

in short term: we can add consumers, patch consumption or throttle producers.

In long term: we can optimize the consumption logic, increase parallelism by splitting queues and partitioning, and use dead-letter queues and monitoring to prevent future backlogs.

Spring AMQP 的重试机制?

Kafka 和 RabbitMQ 的区别?

Kafka is designed for high throughput and big data processing, using a distributed logging architecture. It’s suitable for log collection and real-time analysis.

RabbitMQ is designed for reliable message delivery. Based on the AMQP protocol, It supports flexible routing and is suitable for scenarios requiring strong consistency, such as financial payments.

Mybatis

MyBatis 的执行流程是怎样的?

Mybatis creates SqlSession via SqlSessionFactory, dynamically proxies mapper interface, maps SQL to XML/annotations, and performs database operations.

#{}${} 的区别?

MyBatis 一级缓存和二级缓存的区别?

Mapper 接口与 XML 是如何绑定的?

The Mapper interface is bound to XML through namespace(package) and method name(id), and MyBatis maps the interface method call to the coresponding SQL statement execution through dynamic proxy.

Usually, MyBatis add the class to session.getMapper method to generate a proxy object. the proxy object will intercept the method , and find the SQL in XML through namespace and method. Then run the SQL, get result, return the value object.

MyBatis 如何实现动态 SQL?

MyBatis 性能调优手段?

Docker

Docker 镜像和容器的区别?

Image is static template. Container is the running instance of Image.

Dockerfile 常见指令(FROM、RUN、CMD、ENTRYPOINT、COPY、WORKDIR…)?

Docker 镜像的分层结构?

如何减少镜像体积?

容器和虚拟机的区别?

Docker can run on VM, VM can provide docker a running environment.

Docker 网络模式有哪些?

Kubernetes (K8s)

K8s 的核心组件有哪些?(etcd、API Server、Controller、Scheduler、Kubelet…)

Pod、Deployment、Service、Ingress 的区别?

Pod 为什么会被 OOMKilled?

Liveness Probe 和 Readiness Probe 有什么区别?

K8s 滚动更新和回滚是怎么实现的?

ConfigMap 和 Secret 的区别?

如何排查 Pod CrashLoopBackOff?

HPA(Horizontal Pod Autoscaler)工作原理?

CI/CD

CI/CD 的流程是怎样的?

常见的 CI/CD 工具(Jenkins、GitLab CI、ArgoCD)区别?

在 CI/CD 中如何做自动化测试?

如何在 CI/CD 中实现蓝绿发布 / 灰度发布?

CI/CD 如何与 Docker、K8s 结合?

如何保障生产环境的回滚机制?