首页  

springboot2 feign 实例     所属分类 springcloud 浏览量 1532
注意类名 feign 写错了 ,暂时不改了

服务提供者

@RestController
public class FeginHelloController {

    @RequestMapping(value = "/fegin/hello")
    public Data hello(@RequestBody  Data data) throws Exception {    	
    	return data; 	
    }
          
    @RequestMapping(value = "/fegin/hello2")
    public String hello2(@RequestParam("name")  String name) throws Exception { 
    	// 模拟异常
    	if(name!=null && name.indexOf("error")>=0){
    		throw new Exception(name);
    	}
    	return "hello,"+name+" from "+HostInfoUtil.getHostIp()+","+LocalDateTime.now(); 	
    }
}

Data 是一个比较复杂的数据对象 ,用于测试 序列化与反序列化
注意 Data 里嵌套的对象一定要用泛型声明好类型

配置服务名
spring.application.name=provider


服务消费者 添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> @FeignClient(name = "provider") public interface FeginHello { @RequestMapping(value="/fegin/hello") Data hello(Data data); @RequestMapping(value="/fegin/hello2") String hello2(@RequestParam("name") String name) throws Exception; } 注意增加注解 @EnableFeignClients @EnableFeignClients public class ConsumerApplication {
测试步骤 启动 eureka 注册中心 启动服务提供者 启动服务消费者 http://127.0.0.1:9032/fegin/hello 使用postman 发送请求 post body raw {"str1":"abc"} 设置header Content-Type application/json http://127.0.0.1:9032/fegin/hello2?name=cat 也可以使用 postman 发送 post请求 x-www-form-urlencoded 打开监控端点 服务端可查看 http 请求和响应报文 /actuator/httptrace /fegin/hello POST请求 /fegin/hello2 get请求 请求参数 超出长度 会报错 譬如 11K status 400 reading FeginHello#hello2(String); HTTP Status 400 – Bad Request 使用RequestParam注解 , post 发送报文 ,服务端还是会报错 java.lang.IllegalArgumentException: Request header is too large 会把参数拼到url里 method: POST uri: http://10.57.240.38:9031/fegin/hello2?name=tiger 复杂参数 超长报文 建议使用 RequestBody + 数据对象 传递参数 推荐使用 /fegin/hello 接口形式 具体步骤请看 springboot2微服务实例

完整代码 https://gitee.com/dyyx/hellocode/tree/master/demo/springcloud
参考资料 springboot2微服务实例 Spring Cloud Eureka 常用配置及说明 springboot2网关zuul实例 springcloud DiscoveryClient使用说明 微服务架构技术栈 postman中form-data x-www-form-urlencoded raw binary的区别 springboot2 @RequestBody注解使用说明

上一篇     下一篇
linux kill 命令

springboot依赖本地jar打包配置

eclipse安装lombok

feign消费端注解使用说明

JVM异常优化Fast Throw 与 OmitStackTraceInFastThrow 参数

arthas实战之生产环境空指针排查