代码如下,三种方法:

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的更多相关文章

  1. Ribbon负载均衡及Feign消费者调用服务

    微服务调用Ribbon 简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netf ...

  2. 小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择

    笔记 6.Feign核心源码解读和服务调用方式ribbon和Feign选择         简介: 讲解Feign核心源码解读和 服务间的调用方式ribbon.feign选择             ...

  3. 【一起学源码-微服务】Feign 源码二:Feign动态代理构造过程

    前言 前情回顾 上一讲主要看了@EnableFeignClients中的registerBeanDefinitions()方法,这里面主要是 将EnableFeignClients注解对应的配置属性注 ...

  4. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  5. SpringCloud(5)---Feign服务调用

    SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...

  6. Feign来调用服务

    Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Feign支持可 ...

  7. Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决

    timed-out and no fallback available: 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该服务能不能通,如果通了就不管了,调用在指定时间内超时时,就会通过 ...

  8. Springcloud 整合Hystrix 断路器,支持Feign客户端调用

    1,在这篇博文中,已经大致说过了Springcloud服务保护框架 Hystrix在服务隔离,服务降级,以及服务熔断中的使用 https://www.cnblogs.com/pickKnow/p/11 ...

  9. Spring Cloud Alibaba(8)---Feign服务调用

    Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...

随机推荐

  1. college-ruled notebook

    TBBT.s3.e10: Sheldon: Where's your notebook?Penny: Um, I don't have one.Sheldon: How are you going t ...

  2. docker配置国内阿里云镜像源

    使用docker默认镜像源下载镜像会很慢,因此很多情况下,我们在安装完docker以后都会修改为国内的镜像,这样在下载镜像的时候就不用等那么长时间了. 配置docker的镜像为阿里云镜像 方法一 $ ...

  3. 大数据处理系列之(一)Java线程池使用

    前言:最近在做分布式海量数据处理项目,使用到了java的线程池,所以搜集了一些资料对它的使用做了一下总结和探究, 前面介绍的东西大多都是从网上搜集整理而来.文中最核心的东西在于后面两节无界队列线程池和 ...

  4. MySQL(2):数据管理

    一. 外键概念: 如果公共关键字在一个关系中是主关键字,那么这个公共关键字被称为另一个关系的外键.由此可见,外键表示了两个关系之间的相关联系.以另一个关系的外键作主关键字的表被称为主表,具有此外键的表 ...

  5. Dubbo多版本控制

    当系统进行升级时,一般都是采用"灰度发布(又称为金丝雀发布)"过程.即在低压力时段,让部分消费者先调用新的提供者实现类,其余的仍然调用老的实现类,在新的实现类运行没有问题的情况下, ...

  6. GO类型转换

    golang []byte转string golang中,字符切片[]byte转换成string最简单的方式是 package main import ( "fmt" _ &quo ...

  7. 【Java 8】Stream中flatMap方法

    在java 8 Stream中,flatMap方法是一个维度升降的方法 举例说明 给 定 单 词 列 表["Hello","World"] ,要返回列表 [&q ...

  8. 4.Vue.js-起步

    Vue.js 起步 每个 Vue 应用都需要通过实例化 Vue 来实现. 语法格式如下: var vm = new Vue({ // 选项 }) 接下来让我们通过实例来看下 Vue 构造器中需要哪些内 ...

  9. JDK的简介,卸载和安装过程

    JDK的简介,卸载和安装过程 JDK JRE JVM JDK:Java Development Kit JRE:Java Runtime Environment JVM:Java Virtual Ma ...

  10. Docker从入门到精通(二)——安装Docker

    通过上面文章,我们大概知道了什么是Docker,但那都是文字功夫,具体想要理解,还得实操,于是这篇文章带着大家来手动安装Docker. 1.官方教程 https://docs.docker.com/e ...