码迷,mamicode.com
首页 > 其他好文 > 详细

Feign动态调用,结合Ribbon

时间:2018-04-19 18:48:22      阅读:1556      评论:0      收藏:0      [点我收藏+]

标签:rest   方法   man   new   return   feign   gap   ble   oid   

代码如下,三种方法:

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

 

Feign动态调用,结合Ribbon

标签:rest   方法   man   new   return   feign   gap   ble   oid   

原文地址:https://www.cnblogs.com/wuzhiyuan/p/8885120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!