zoukankan      html  css  js  c++  java
  • @FeignClient同一个name使用多个配置类报错,解决方案【转+改进】

    今天手上的SpringCloud 项目做开发测试,多个FeigenClient报错,错误标题是(截取了部分):

    defined in null, could not be registered. A bean with that name has already

    找了下资料,在以下两篇文章中找到需要的东西:

    https://blog.csdn.net/neosmith/article/details/82349449

    http://www.flydean.com/spring-cloud-openfeign-demo/

    第二篇讲的更详细,但是在我按照文章中的代码改写代码时,还是无法正确生成bean。于是我改下了下,我用的spring cloud 版本是Greenwich.SR3

    原来的代码如下:

     1 /**
     2  * 因为@FeignClient的value不能重复,所以需要在这里以自定义的方式来创建
     3  * @author wayne
     4  * @version FeignClientController,  2019/9/5 7:07 下午
     5  */
     6 @Data
     7 @Component
     8 @Import(FeignClientsConfiguration.class)
     9 public class FeignClientController {
    10 
    11     private OrderClient orderClient;
    12     private UserClient userClient;
    13 
    14     public FeignClientController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
    15         this.orderClient = Feign.builder().client(client)
    16                 .encoder(encoder)
    17                 .decoder(decoder)
    18                 .contract(contract)
    19                 //默认是Logger.NoOpLogger
    20                 .logger(new Slf4jLogger(OrderClient.class))
    21                 //默认是Logger.Level.NONE
    22                 .logLevel(Logger.Level.FULL)
    23                 .target(OrderClient.class, "http://openfeign-server");
    24 
    25         this.userClient = Feign.builder().client(client)
    26                 .encoder(encoder)
    27                 .decoder(decoder)
    28                 .contract(contract)
    29                 //默认是Logger.NoOpLogger
    30                 .logger(new Slf4jLogger(UserClient.class))
    31                 //默认是Logger.Level.NONE
    32                 .logLevel(Logger.Level.FULL)
    33                 .target(UserClient.class, "http://openfeign-server");
    34     }
    35 }

    将client声明为bean,才能在我这个版本里解决文章头部的错误信息。

    改写后代码(原示例代码上改):

    package com.flydean.client;
    
    import feign.Client;
    import feign.Contract;
    import feign.Feign;
    import feign.Logger;
    import feign.codec.Decoder;
    import feign.codec.Encoder;
    import feign.slf4j.Slf4jLogger;
    import org.springframework.cloud.openfeign.FeignClientsConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Import;
    import org.springframework.stereotype.Component;
    
    @Component
    @Import(FeignClientsConfiguration.class)
    public class FeignClientController {
    
        private OrderClient orderClient;
        private UserClient userClient;
    
        private Decoder decoder;
        private Encoder encoder;
        private Client client;
        private Contract contract;
    
        public FeignClientController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
    
            this.decoder = decoder;
            this.encoder = encoder;
            this.client = client;
            this.contract = contract;
    
            String serverName = "http://openfeign-server";
            this.orderClient = initClient(OrderClient.class, serverName);
    
            this.userClient = initClient(UserClient.class, serverName);
        }
    
        private <T> T initClient(Class<T> clazz, String serverName) {
            return Feign.builder().client(client)
                    .encoder(encoder)
                    .decoder(decoder)
                    .contract(contract)
                    //默认是Logger.NoOpLogger
                    .logger(new Slf4jLogger(clazz))
                    //默认是Logger.Level.NONE
                    .logLevel(Logger.Level.FULL)
                    .target(clazz, serverName);
        }
    
        @Bean
        public OrderClient initOrderClient() {
            return orderClient;
        }
    
        @Bean
        public UserClient initUserClient() {
            return userClient;
        }
    }
  • 相关阅读:
    Powershell 音乐播放
    Powershell指令集_2
    Powershell指令集_2
    Powershell 邮件发送
    Powershell 邮件发送
    Oracle 11g 关闭内存自动管理
    Oracle 11g 内存手动管理
    Oracle 内存参数调整
    RESTful三理解
    RESTful三理解
  • 原文地址:https://www.cnblogs.com/chongsha/p/11909395.html
Copyright © 2011-2022 走看看