Search in sources :

Example 1 with FeignInject

use of org.nutz.boot.starter.feign.annotation.FeignInject in project nutzboot by nutzam.

the class FeignStarter method afterBorn.

@SuppressWarnings({ "unchecked", "rawtypes" })
public Object afterBorn(Object obj, String beanName) {
    try {
        Mirror mirror = Mirror.me(obj);
        for (Field field : obj.getClass().getDeclaredFields()) {
            FeignInject fc = field.getAnnotation(FeignInject.class);
            if (fc == null)
                continue;
            String url = Strings.sBlank(conf.get(PROP_URL), "http://127.0.0.1:8080");
            Encoder encoder = getEncoder(fc, field);
            Decoder decoder = getDecoder(fc, field);
            Client client = getClient(fc, field, url);
            Logger.Level level = Level.valueOf(conf.get(PROP_LOGLEVEL, "BASIC").toUpperCase());
            Class apiType = field.getType();
            Logger logger = new Slf4jLogger(apiType);
            boolean useHystrix = "true".equals(Strings.sBlank(fc.useHystrix(), conf.get(PROP_HYSTRIX_ENABLE)));
            Feign.Builder builder = useHystrix ? HystrixFeign.builder() : Feign.builder();
            if (encoder != null)
                builder.encoder(encoder);
            if (decoder != null)
                builder.decoder(decoder);
            if (client != null)
                builder.client(client);
            builder.logger(logger);
            builder.logLevel(level);
            Object t = null;
            if (useHystrix) {
                t = ((HystrixFeign.Builder) builder).target(apiType, url, getIocBean(apiType, fc.fallback()));
            } else {
                t = builder.target(apiType, url);
            }
            mirror.setValue(obj, field.getName(), t);
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    return obj;
}
Also used : Slf4jLogger(feign.slf4j.Slf4jLogger) Decoder(feign.codec.Decoder) JacksonDecoder(feign.jackson.JacksonDecoder) JAXBDecoder(feign.jaxb.JAXBDecoder) GsonDecoder(feign.gson.GsonDecoder) Logger(feign.Logger) Slf4jLogger(feign.slf4j.Slf4jLogger) FeignInject(org.nutz.boot.starter.feign.annotation.FeignInject) Level(feign.Logger.Level) HystrixFeign(feign.hystrix.HystrixFeign) Field(java.lang.reflect.Field) GsonEncoder(feign.gson.GsonEncoder) JAXBEncoder(feign.jaxb.JAXBEncoder) Encoder(feign.codec.Encoder) JacksonEncoder(feign.jackson.JacksonEncoder) HystrixFeign(feign.hystrix.HystrixFeign) Feign(feign.Feign) Client(feign.Client) OkHttpClient(feign.okhttp.OkHttpClient) RibbonClient(feign.ribbon.RibbonClient) EurekaClient(com.netflix.discovery.EurekaClient) ApacheHttpClient(feign.httpclient.ApacheHttpClient) LBClient(feign.ribbon.LBClient) Mirror(org.nutz.lang.Mirror)

Aggregations

EurekaClient (com.netflix.discovery.EurekaClient)1 Client (feign.Client)1 Feign (feign.Feign)1 Logger (feign.Logger)1 Level (feign.Logger.Level)1 Decoder (feign.codec.Decoder)1 Encoder (feign.codec.Encoder)1 GsonDecoder (feign.gson.GsonDecoder)1 GsonEncoder (feign.gson.GsonEncoder)1 ApacheHttpClient (feign.httpclient.ApacheHttpClient)1 HystrixFeign (feign.hystrix.HystrixFeign)1 JacksonDecoder (feign.jackson.JacksonDecoder)1 JacksonEncoder (feign.jackson.JacksonEncoder)1 JAXBDecoder (feign.jaxb.JAXBDecoder)1 JAXBEncoder (feign.jaxb.JAXBEncoder)1 OkHttpClient (feign.okhttp.OkHttpClient)1 LBClient (feign.ribbon.LBClient)1 RibbonClient (feign.ribbon.RibbonClient)1 Slf4jLogger (feign.slf4j.Slf4jLogger)1 Field (java.lang.reflect.Field)1