惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 郭慕荣

Docker部署zookeeper总结 Mac中git ssh的配置(GitLab) Java 类加载机制 面试题(一) 如何监控和调优JVM性能? jvm常用的参数有哪些? 怎么配置? hashmap 和currenthashmap 的原理?详解一下 aop在项目中使用的场景?怎么使用? Java dubbo spring springboot中的spi机制 spring中常见的两种代理模式 Redis的zset 面试汇总 drools 规则引擎在线化配置 - 郭慕荣 springcloud中网关gateway总结 spring是怎么解决循环依赖的? MySQL 死锁 怎么处理? 在写left join的时候 是大表在左侧 还是小表在左侧(二) 在写left join的时候 是大表在左侧 还是小表在左侧(一) nacos客户端(接口调用者)如何感知被调用服务下线? (二) nacos客户端(接口调用者)如何感知被调用服务下线?(一) 在MySQL中 redolog undolog binlog 写入的场景,顺序 - 郭慕荣
springcloud中常用的注解详解
郭慕荣 · 2025-10-23 · via 博客园 - 郭慕荣

Spring Cloud 生态包含多个组件(如服务注册发现、服务调用、配置中心、熔断降级等),每个组件都提供了特定的注解简化开发。以下按组件分类梳理常用注解,详细说明其作用、核心属性及含义。

用于将服务注册到注册中心(如 Eureka、Nacos、Consul),或让服务发现其他服务。

  • 所属组件:Spring Cloud Eureka
  • 作用:标识当前服务是 Eureka 客户端,启动时会自动向 Eureka Server 注册。
  • 属性:无核心属性(仅用于开启功能)。
  • 说明:仅适用于 Eureka 注册中心,若使用其他注册中心(如 Nacos),需用更通用的 @EnableDiscoveryClient
  • 所属组件:Spring Cloud Common(通用服务发现)
  • 作用:通用的服务发现注解,适用于所有符合 Spring Cloud 服务发现规范的注册中心(如 Eureka、Nacos、Consul),启动时自动注册服务并支持发现其他服务。
  • 属性:无核心属性。
  • 说明:Spring Cloud 2020 版本后,若引入了注册中心依赖(如 Nacos),可省略此注解,默认自动开启服务注册发现。
  • 所属组件:Spring Cloud Eureka
  • 作用:自定义 Eureka 客户端实例配置(如服务实例 ID、续约间隔等),需通过实现类配置。
  • 常用属性(通过实现类设置):
    • instanceId:服务实例唯一标识(默认是 主机名:服务名:端口,可自定义为 IP:端口)。
    • leaseRenewalIntervalInSeconds:客户端向 Eureka Server 发送续约请求的间隔(默认 30 秒)。
    • preferIpAddress:是否优先使用 IP 地址作为服务实例的主机名(默认 false,设为 true 便于跨网络访问)。

Feign 是声明式 HTTP 客户端,简化服务间调用,常用注解如下:

  • 所属组件:Spring Cloud OpenFeign
  • 作用:开启 Feign 功能,扫描并加载标注了 @FeignClient 的接口。
  • 核心属性:
    • basePackages:指定扫描 @FeignClient 接口的包路径(如 basePackages = "com.example.feign")。
    • clients:指定具体要加载的 Feign 接口(如 clients = UserFeignClient.class,适用于精确扫描)。
    • defaultConfiguration:全局 Feign 配置类(如日志级别、拦截器等,优先级低于接口自身的 configuration)。
  • 所属组件:Spring Cloud OpenFeign
  • 作用:标识一个 Feign 客户端接口,声明要调用的远程服务,Spring 会自动生成代理实现类。
  • 核心属性:
    • name / value:必填,远程服务的名称(与注册中心的服务名一致,用于服务发现)。例如 name = "user-service"
    • url:可选,直接指定远程服务的 URL(跳过服务发现,适用于调试或固定地址的服务)。例如 url = "http://localhost:8081"
    • fallback:降级处理类(需实现当前 Feign 接口),当远程服务调用失败(超时、熔断等)时,会执行该类的对应方法。例如 fallback = UserFeignFallback.class
    • fallbackFactory:比 fallback 更灵活的降级工厂类,可获取异常信息。例如 fallbackFactory = UserFeignFallbackFactory.class
    • configuration:当前 Feign 接口的专属配置类(如自定义日志、拦截器),优先级高于 @EnableFeignClients 的全局配置。
    • path:远程服务的统一前缀路径。例如服务接口都是 /api/user 开头,可设置 path = "/api/user",接口方法只需写相对路径(如 @GetMapping("/{id}") 对应 /api/user/{id})。

Feign 接口的方法需使用 Spring MVC 注解声明 HTTP 请求细节,例如:

  • @GetMapping / @PostMapping / @PutMapping / @DeleteMapping:指定请求方法和路径。
  • @PathVariable:获取 URL 路径参数(如 @GetMapping("/{id}") User getUser(@PathVariable("id") Long id))。
  • @RequestParam:获取请求参数(如 @GetMapping("/list") List<User> getList(@RequestParam("status") Integer status))。
  • @RequestBody:传递请求体(如 @PostMapping("/save") Boolean save(@RequestBody User user))。

用于集成 Spring Cloud Config 配置中心,实现配置的集中管理和动态刷新。

  • 所属组件:Spring Cloud Config
  • 作用:标识当前服务是 Config 配置中心服务器,用于加载远程配置(如 Git、SVN)并提供给客户端。
  • 属性:无核心属性。
  • 所属组件:Spring Cloud Context
  • 作用:声明 Bean 支持配置动态刷新。当配置中心的配置更新后,通过调用 /actuator/refresh 端点,标注该注解的类会重新实例化,加载新配置。
  • 属性:无核心属性。
  • 示例:
    @RestController
    @RefreshScope  // 配置更新时,该类会重新创建
    public class ConfigController {
        @Value("${app.name}")   

Resilience4j 是 Spring Cloud 推荐的熔断组件(替代 Hystrix),提供熔断、重试、限流等功能,常用注解如下:

  • 所属组件:Spring Cloud Resilience4j
  • 作用:开启熔断功能,当方法调用失败率达到阈值时,自动触发熔断(暂时停止调用,直接返回降级结果),避免服务级联失败。
  • 核心属性:
    • name:熔断器名称(需与配置文件中的规则对应,如 resilience4j.circuitbreaker.instances.<name>)。
    • fallbackMethod:降级方法名(当前类中定义的方法,参数和返回值需与原方法一致,最后可加一个 Exception 参数接收异常)。
  • 示例:
    @Service
    public class UserService {
        
  • 所属组件:Spring Cloud Resilience4j
  • 作用:开启重试功能,当方法调用失败时(如超时、异常),自动重试指定次数,提高调用成功率。
  • 核心属性:
    • name:重试器名称(对应配置文件 resilience4j.retry.instances.<name>)。
    • fallbackMethod:最终重试失败后的降级方法。
  • 配置示例(application.yaml):
    resilience4j:
      retry:
        instances:
          userService:  
  • 所属组件:Spring Cloud Resilience4j
  • 作用:实现接口限流,限制单位时间内的调用次数,防止服务被过度请求击垮。
  • 核心属性:
    • name:限流器名称(对应配置文件 resilience4j.ratelimiter.instances.<name>)。
    • fallbackMethod:限流触发后的降级方法。
  • 配置示例:
    resilience4j:
      ratelimiter:
        instances:
          userService:
            limitRefreshPeriod: 1s  
  • 所属组件:Spring Cloud LoadBalancer
  • 作用:为 RestTemplate 或 WebClient 开启负载均衡功能,使其在调用服务时,能通过服务名从注册中心获取实例列表,并按负载均衡策略(如轮询、随机)选择实例。
  • 属性:无核心属性。
  • 示例:
    @Configuration
    public class RestTemplateConfig {
        @Bean
        @LoadBalanced  
  • 所属组件:Spring Cloud Common
  • 作用:组合注解,简化微服务启动类配置,等价于: @SpringBootApplication(Spring Boot 基础注解) + @EnableDiscoveryClient(服务发现) + @EnableCircuitBreaker(熔断支持,Resilience4j 或 Hystrix)。
  • 示例:
    @SpringCloudApplication  // 替代三个注解
    public class UserServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication.class, args);
        }
    }

Spring Cloud 注解按功能可分为服务注册发现、服务调用(Feign)、配置中心、熔断降级、负载均衡等类别。核心注解如 @FeignClient(服务调用)、@CircuitBreaker(熔断)、@LoadBalanced(负载均衡)等,需重点掌握其属性含义(如 @FeignClient 的 name 和 fallback)及使用场景,以简化微服务开发。