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的更多相关文章
- Ribbon负载均衡及Feign消费者调用服务
微服务调用Ribbon 简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netf ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择
笔记 6.Feign核心源码解读和服务调用方式ribbon和Feign选择 简介: 讲解Feign核心源码解读和 服务间的调用方式ribbon.feign选择 ...
- 【一起学源码-微服务】Feign 源码二:Feign动态代理构造过程
前言 前情回顾 上一讲主要看了@EnableFeignClients中的registerBeanDefinitions()方法,这里面主要是 将EnableFeignClients注解对应的配置属性注 ...
- SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解
前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...
- SpringCloud(5)---Feign服务调用
SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...
- Feign来调用服务
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Feign支持可 ...
- Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决
timed-out and no fallback available: 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该服务能不能通,如果通了就不管了,调用在指定时间内超时时,就会通过 ...
- Springcloud 整合Hystrix 断路器,支持Feign客户端调用
1,在这篇博文中,已经大致说过了Springcloud服务保护框架 Hystrix在服务隔离,服务降级,以及服务熔断中的使用 https://www.cnblogs.com/pickKnow/p/11 ...
- Spring Cloud Alibaba(8)---Feign服务调用
Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...
随机推荐
- idea Error : java 不支持发行版本5
问题描述 在Intellij idea中新建了一个Maven项目,运行时报错如下:Error : java 不支持发行版本5 解决 1.在Intellij中点击"File" --& ...
- [C++] vptr, where are you?
Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...
- accommodate ~ ache
accommodate The accommodation reflex [反射] (or accommodation-convergence [会聚] reflex) is a reflex act ...
- RB-Tree深度探索
关联式容器就是通过key值来寻找value,这个和数据库很相像,为了提升查找效率,因此关联式容器底层大多数用红黑树或哈希表来实现. 红黑树是高度平衡的二叉树,它也被称为平衡二元搜索树. 如上所示,正常 ...
- 【C/C++】拔河比赛/分组/招商银行
题目:小Z组织训练营同学进行一次拔河比赛,要从n(2≤n≤60,000)个同学中选出两组同学参加(两组人数可能不同).对每组同学而言,如果人数超过1人,那么要求该组内的任意两个同学的体重之差的绝对值不 ...
- 网络协议之:基于UDP的高速数据传输协议UDT
目录 简介 UDT协议 UDT的缺点 总结 简介 简单就是美.在网络协议的世界中,TCP和UDP是建立在IP协议基础上的两个非常通用的协议.我们现在经常使用的HTTP协议就是建立在TCP协议的基础上的 ...
- 5、Redis五大基本数据类型——String类型
一.Redis支持数据类型简介 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 二.String类 ...
- 记ByteCTF中的Node题
记ByteCTF中的Node题 我总觉得字节是跟Node过不去了,初赛和决赛都整了个Node题目,当然PHP.Java都是必不可少的,只是我觉得Node类型的比较少见,所以感觉挺新鲜的. Nothin ...
- Tableau如何绘制凹凸图
一.把订单日期拖拽至列,把销售额拖拽至行,类别拖拽至标记,并把订单日期拖拽至筛选器选择2017年 二.创建计算字段销售排名 三.将刚刚创建的销售排名拖拽至行,计算依据-类别 四.销量排名拖拽成两个,图 ...
- Linux驱动实践:一起来梳理中断的前世今生(附代码)
作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...