zoukankan      html  css  js  c++  java
  • Feign动态调用,结合Ribbon

    代码如下,三种方法:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;

    
    

    import com.fasterxml.jackson.databind.JsonNode;
    import com.netflix.config.ConfigurationManager;

    
    

    import feign.Feign;
    import feign.ribbon.RibbonClient;



    @EnableEurekaClient @SpringBootApplication @EnableAutoConfiguration @EnableFeignClients @EnableDiscoveryClient
    public class TestApp implements CommandLineRunner { @Autowired private RestTemplate restTemplate; @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } // private static final Logger logger = // LoggerFactory.getLogger(GalaplatBaseplatformSerialnumberApp.class); public static void main(String[] args) { SpringApplication.run(TestApp.class, args); } public void run(String... args) throws Exception { ConfigurationManager.loadPropertiesFromResources("galaplat-baseplatform-serialnumber.properties"); ITestFeign testFeign1 = Feign.builder().client(RibbonClient.create()).decoder(new JacksonDecoder()).target(ITestFeign.class, "http://galaplat-baseplatform-serialnumber"); JsonNode jsonNode1 = testFeign1.gettabcode(); System.out.println(jsonNode1); ITestFeign testFeign2 = Feign.builder().decoder(new JacksonDecoder()).target(ITestFeign.class, "http://192.168.51.116:8008"); JsonNode jsonNode2 = testFeign2.gettabcode(); System.out.println(jsonNode2); JsonNode body = restTemplate.getForEntity("http://galaplat-baseplatform-serialnumber/serial", JsonNode.class).getBody(); System.out.println(body); } }
    import com.fasterxml.jackson.databind.JsonNode;
    import com.galaplat.base.core.common.exception.BaseException;
    
    import feign.RequestLine;
    
    interface ITestFeign {
    
        @RequestLine("GET /serial")
        JsonNode gettabcode() throws BaseException;
    
    }
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.Module;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.lang.reflect.Type;
    import java.util.Collections;
    
    import feign.Response;
    import feign.Util;
    import feign.codec.Decoder;
    
    public class JacksonDecoder implements Decoder {
    
        private final ObjectMapper mapper;
    
        public JacksonDecoder()
        {
            this(Collections.<Module> emptyList());
        }
    
        public JacksonDecoder(Iterable<Module> modules)
        {
            this(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).registerModules(modules));
        }
    
        public JacksonDecoder(ObjectMapper mapper)
        {
            this.mapper = mapper;
        }
    
        @Override
        public Object decode(Response response, Type type) throws IOException
        {
            if (response.status() == 404)
                return Util.emptyValueOf(type);
            if (response.body() == null)
                return null;
            Reader reader = response.body().asReader();
            if (!reader.markSupported())
            {
                reader = new BufferedReader(reader, 1);
            }
            try
            {
                // Read the first byte to see if we have any data
                reader.mark(1);
                if (reader.read() == -1)
                {
                    return null; // Eagerly returning null avoids "No content to map due to end-of-input"
                }
                reader.reset();
                return mapper.readValue(reader, mapper.constructType(type));
            }
            catch (RuntimeJsonMappingException e)
            {
                if (e.getCause() != null && e.getCause() instanceof IOException)
                {
                    throw IOException.class.cast(e.getCause());
                }
                throw e;
            }
        }
    }
    galaplat-baseplatform-serialnumber.ribbon.MaxAutoRetries=1
    
    galaplat-baseplatform-serialnumber.ribbon.MaxAutoRetriesNextServer=1
    
    galaplat-baseplatform-serialnumber.ribbon.OkToRetryOnAllOperations=true
    
    galaplat-baseplatform-serialnumber.ribbon.ServerListRefreshInterval=2000
    
    galaplat-baseplatform-serialnumber.ribbon.ConnectTimeout=3000
    
    galaplat-baseplatform-serialnumber.ribbon.ReadTimeout=3000
    
    galaplat-baseplatform-serialnumber.ribbon.listOfServers=192.168.51.116:8008
    
    galaplat-baseplatform-serialnumber.ribbon.EnablePrimeConnections=false
  • 相关阅读:
    找出数组中最长的连续数字序列(JavaScript实现)
    从数组中选出和等于固定值的n个数(JavaScript实现)
    比较任意两个JSON串是否相等(比较对象是否相等)JAVA版
    freshcodecolor纯正则实现的在线代码着色(高亮)
    最新QQ强制聊天代码,同时可判断好友关系
    (转)spring异常抛出触发事务回滚策略
    (转)Java回收对象的标记 和 对象的二次标记过程
    (转)调用System.gc没有立即执行的解决方法
    java线程池与五种常用线程池策略使用与解析
    (转)Spring事务管理详解
  • 原文地址:https://www.cnblogs.com/wuzhiyuan/p/8885120.html
Copyright © 2011-2022 走看看