找到注释@EnableCircuitBreaker,但没有实现。你忘了包括一个开胃菜吗?

Posted

技术标签:

【中文标题】找到注释@EnableCircuitBreaker,但没有实现。你忘了包括一个开胃菜吗?【英文标题】:Annotation @EnableCircuitBreaker found, but there are no implementations. Did you forget to include a starter? 【发布时间】:2019-04-09 18:12:35 【问题描述】:

尝试从 SpringBoot + SpringCloud 应用程序构建 ShadowJar。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

@EnableCircuitBreaker
@SpringBootApplication
public class App 

    public static void main(String[] args) 
        SpringApplication.run(App.class);
    


应用程序在 IDE 中成功启动,但构建和运行 jar 导致以下错误:

java.lang.IllegalStateException: Annotation @EnableCircuitBreaker found, but there are no implementations. Did you forget to include a starter?
    at org.springframework.cloud.commons.util.SpringFactoryImportSelector.selectImports(SpringFactoryImportSelector.java:76)
    at org.springframework.context.annotation.ConfigurationClassParser$DefaultDeferredImportSelectorGroup.process(ConfigurationClassParser.java:842)
    at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:828)
    at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:563)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:692)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)

Gradle 设置:

plugins 
    id 'java'
    id 'application'
    id "com.github.johnrengelman.shadow" version "4.0.2"


sourceCompatibility = 1.10

shadowJar 
    baseName = 'bundle'
    version = '1.0-SNAPSHOT'
    zip64 = true
    manifest 
        attributes 'Main-Class': 'package.App'
    


repositories 
    mavenCentral()


dependencies 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.6.RELEASE'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', 
            version: '2.0.2.RELEASE',  exclude group: 'com.google.code.gson', module: 'gson' 
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix', version: '2.0.2.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '2.0.2.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin', version: '2.0.2.RELEASE'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.6.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'

缺少哪个启动器/依赖项?

【问题讨论】:

【参考方案1】:

这是我的配置。

        buildscript 
            ext 
                springBootVersion = '2.0.5.RELEASE'
            
            repositories 
                mavenLocal()
                mavenCentral()

            
            dependencies 
                classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
            
        

        apply plugin: 'java'
        apply plugin: 'eclipse'
        apply plugin: 'org.springframework.boot'
        apply plugin: 'io.spring.dependency-management'

        group = 'com.learning.microservices'
        version = '0.0.1-SNAPSHOT'
        sourceCompatibility = 1.8

        repositories 
            mavenLocal()
            mavenCentral()
        


        ext 
            springCloudVersion = 'Finchley.SR1'
        

        dependencies 
            compile('org.springframework.boot:spring-boot-starter-web')
            compile('org.springframework.cloud:spring-cloud-starter-config')
            compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
            compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix')
            compile('org.springframework.cloud:spring-cloud-starter-sleuth')
            compile('org.projectlombok:lombok:1.16.8')
            compile('org.springframework.boot:spring-boot-starter-actuator')
            compile('com.learning.microservices:interfaces:1.0.0')
            compile('org.springframework.boot:spring-boot-starter-data-jpa')
            runtime('com.h2database:h2')
            testCompile('org.springframework.boot:spring-boot-starter-test')
        

        dependencyManagement 
            imports 
                mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
            
        

你在使用 spring-boot-gradle-plugin 吗?启动 Spring Boot 云应用程序很有帮助。还要考虑给用户一个 spring 云依赖项。

Here你可以多看看。

【讨论】:

看起来没有必要使用ShadowJar,SpringBoot插件构建fat jar没有任何问题。【参考方案2】:

没有理由不使用BootJar(而是使用ShadowJar)来构建uber-jar。这导致错误的包装。

这是一个工作 build.gradle 来打包 SpringBoot + SpringCloud uber-jar:

buildscript 
    ext 
        springBootVersion = '2.0.6.RELEASE'
    
    repositories 
        mavenCentral()
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
    


plugins 
    id 'java'
    id 'application'
    id 'org.springframework.boot' version '2.0.6.RELEASE'


sourceCompatibility = 1.10
mainClassName = 'com.App'

bootJar 
    baseName = 'bundle'
    version = '1.0-SNAPSHOT'
    group = 'micro-services'


repositories 
    mavenCentral()


ext 
    springCloudVersion = '2.0.2.RELEASE'


dependencies 
    compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
    compile "org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:$springCloudVersion", 
             exclude group: 'com.google.code.gson', module: 'gson' 
    compile "org.springframework.cloud:spring-cloud-starter-netflix-hystrix:$springCloudVersion"
    compile "org.springframework.cloud:spring-cloud-starter-sleuth:$springCloudVersion"
    compile "org.springframework.cloud:spring-cloud-starter-zipkin:$springCloudVersion"
    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
    testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
    testCompile group: 'junit', name: 'junit', version: '4.12'

【讨论】:

以上是关于找到注释@EnableCircuitBreaker,但没有实现。你忘了包括一个开胃菜吗?的主要内容,如果未能解决你的问题,请参考以下文章

聊聊Hystrix的源码

如何找到哪个注释发送 showDetails?

未找到注释“SqlServer:Include”。确保注释已添加 ASP.Net MVC

未找到改造注释。 (参数#1)

JSF 如何找到用@ManagedBean 注释的bean?

如何找到具有特定注释的所有 Java 对象(不是类)?