dubbo和springcloud問題解決——interface not allow null

環(huán)境

dubbo版本: 2.7.3

springCloud版本: 2.2.0

復(fù)現(xiàn)步驟

當使用@Service注解注入dubbo provider到Spring容器中時,在dubbo provider的bean對象里依賴SpringCloud的bean時硫朦,觸發(fā)報錯烈菌,應(yīng)用報錯無法啟動花履!

dubbo provider bean

@Service(registry = "dubbo-registry", interfaceClass = DubboProvideFacade.class)
public class DubboProviderServiceImpl implements DubboProvideFacade {
  @Resource
    private FeignClient feignClient;
  ······
}

SpringCloud Client

@FeignClient(name = "feignClient", url = "${feign.client.config.url}", configuration = FeignClientInterceptor.class)
public interface FeignClient {
    ······
}

報錯

報錯圖片
Caused by: java.lang.IllegalStateException: <dubbo:service interface="" /> interface not allow null!
    at org.apache.dubbo.config.ServiceConfig.checkAndUpdateSubConfigs(ServiceConfig.java:297)
    at org.apache.dubbo.config.ServiceConfig.export(ServiceConfig.java:358)
    at org.apache.dubbo.config.spring.ServiceBean.export(ServiceBean.java:327)
    at org.apache.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:105)
    at org.apache.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:51)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:408)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:896)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)
    at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:136)
    at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:101)
    at org.springframework.cloud.context.named.NamedContextFactory.getInstance(NamedContextFactory.java:145)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.get(FeignClientFactoryBean.java:224)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.feign(FeignClientFactoryBean.java:85)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:261)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:251)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171)
    ... 142 common frames omitted

原因

這個錯誤是由 Spring Cloud Feign 設(shè)計引起的济瓢,每個@FeignClient都會生成一個帶有新子 Spring 的 Feign 代理妹卿,該子 SpringApplicationContext可能會在其 parent 之前刷新ApplicationContext蔑鹦。如果有注解@Service的 Dubbo 服務(wù) bean 被子上下文掃描到箕宙,這個 DubboServiceBean也會被初始化,并且不能保證其依賴的 Spring bean 在那個時候已經(jīng)準備好

解決方式

方式一(已驗證)

FeignClient使用ApplicationContextAware進行獲取

首先寫一個SpringBean獲取工具類

@Component
public class SpringBeanUtil implements ApplicationContextAware {
    private static ApplicationContext appContext;

    private static boolean hasBean(String name){
        return appContext.containsBean(name);
    }

    public static Object getBeanByName(String name) throws BeansException {
        return appContext.getBean(name);
    }

    public static Object getBeanByType(Class requiredType) throws BeansException {
        return appContext.getBean(requiredType);
    }

    public static Object getBeanIfPresent(String name){
        if(hasBean(name)){
            return getBeanByName(name);
        }
        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        setAppContext(applicationContext);
    }

    private static synchronized void setAppContext(ApplicationContext appContext) {
        SpringBeanUtil.appContext = appContext;
    }
}

通過Util獲取FeignClient而不是Spring注入的方式

FeignClient client = SpringContextUtils.getBean(FeignClient.class)

方式二

升級dubbo版本至2.7.9

據(jù)說在2.7.9解決了改問題

方式三

一個不完美的解決方法是,讓FeignClient以來注入的方式進行注入

@Service(registry = "dubbo-registry", interfaceClass = DubboProvideFacade.class)
public class DubboProviderServiceImpl implements DubboProvideFacade {
  @Resource
  @Lazy
    private FeignClient feignClient;
  ······
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锅很,一起剝皮案震驚了整個濱河市凤跑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌仔引,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件当辐,死亡現(xiàn)場離奇詭異鲤看,居然都是意外死亡,警方通過查閱死者的電腦和手機义桂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門慷吊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人溉瓶,你說我怎么就攤上這事堰酿。” “怎么了触创?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長岩馍。 經(jīng)常有香客問我,道長疫铜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任块攒,我火速辦了婚禮佃乘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘庞呕。我一直安慰自己程帕,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布讲逛。 她就那樣靜靜地躺著岭埠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪惜论。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天混聊,我揣著相機與錄音乾巧,去河邊找鬼。 笑死沟于,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的拙绊。 我是一名探鬼主播泳秀,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼金句!你這毒婦竟也來了吕嘀?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤趁曼,失蹤者是張志新(化名)和其女友劉穎棕洋,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體摄悯,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡愧捕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了瘪阁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片邮偎。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖钢猛,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情贩绕,我是刑警寧澤壶愤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站娇哆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏碍讨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一宵统、第九天 我趴在偏房一處隱蔽的房頂上張望覆获。 院中可真熱鬧马澈,春花似錦弄息、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至具钥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間掌动,已是汗流浹背宁玫。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留欧瘪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓妖碉,卻偏偏與公主長得像芥被,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子拴魄,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容