Feign动态调用,结合Ribbon
Posted wuzhiyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Feign动态调用,结合Ribbon的主要内容,如果未能解决你的问题,请参考以下文章