springCloud4---feign
JAX-WS : soap是遵循这个标准。
JAX-RS : 标准,jersey框架遵循这个标准。
Feign是一个声明式的web service客户端。Feign使用的负载均衡也是ribbon,Feign整合了Ribbon和Eureka。
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //Feign的客户端
public class ConsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieFeignApplication.class, args);
}
}
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.UserFeignClient; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; /* 原来是通过restTemplate来调用
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServicePath + id, User.class);
}*/ @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/test")
public User testPost(User user) {
return this.userFeignClient.postUser(user);
} @GetMapping("/test-get")
public User testGet(User user) {
return this.userFeignClient.getUser(user);
}
}
package com.itmuch.cloud.feign; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.itmuch.cloud.entity.User; @FeignClient("microservice-provider-user") //请求microservice-provider-user服务
public interface UserFeignClient {
@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)//microservice-provider-user服务的/simple/{id}
public User findById(@PathVariable("id") Long id); // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value @RequestMapping(value = "/user", method = RequestMethod.POST) //post对象
public User postUser(@RequestBody User user); // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。
// 如勘误,请@lilizhou2008 eacdy0000@126.com
@RequestMapping(value = "/get-user", method = RequestMethod.GET)
public User getUser(User user);
}
spring:
application:
name: microservice-consumer-movie #本服务的名字
server:
port: 7905
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka #注册到的eureka的地址
instance:
prefer-ip-address: true
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <artifactId>microservice-consumer-movie-feign</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- 引入feign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
复写feign的默认配置,
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
//默认只扫描这个类所在的包和子包
public class ConsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieFeignApplication.class, args);
}
}
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.FeignClient2;
import com.itmuch.cloud.feign.UserFeignClient; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @Autowired
private FeignClient2 feignClient2; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
package com.itmuch.cloud.feign; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import com.itmuch.config.Configuration2; //Configuration2作为他的配置文件
@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class)
public interface FeignClient2 {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
package com.itmuch.cloud.feign; import org.springframework.cloud.netflix.feign.FeignClient; import com.itmuch.cloud.entity.User;
import com.itmuch.config.Configuration1; import feign.Param;
import feign.RequestLine; //Configuration1作为他的配置文件
@FeignClient(name = "microservice-provider-user", configuration = Configuration1.class)
public interface UserFeignClient {
@RequestLine("GET /simple/{id}")
public User findById(@Param("id") Long id);
}
package com.itmuch.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import feign.Contract;
import feign.Logger; @Configuration
public class Configuration1 {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
} @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
package com.itmuch.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import feign.auth.BasicAuthRequestInterceptor; @Configuration
public class Configuration2 {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password123");
}
}
spring:
application:
name: microservice-consumer-movie
server:
port: 7901
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
logging:
level:
com.itmuch.cloud.feign.UserFeignClient: DEBUG #日志是debug级别 # 解决第一次请求报超时异常的方案:
# hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
feign.hystrix.enabled: false ## 索性禁用feign的hystrix # 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <artifactId>microservice-consumer-movie-feign-customizing</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
springCloud4---feign的更多相关文章
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- Feign使用Hystrix无效原因及解决方法
最近项目重构使用了Spring Boot和Spring Cloud.这两者结合确实给项目带来了方便,同时也遇到了一些问题.其中使用feign作为服务消费,但是断路器hystrix一直不起作用让人很费解 ...
- 在dropwizard中使用feign,使用hystrix
前言 用惯了spring全家桶之后,试试dropwizard的Hello World也别有一帆风味.为了增强对外访问API的能力,需要引入open feign.这里简单在dropwizard中使用fe ...
- spring cloud feign不支持@RequestBody+ RequestMethod.GET,报错
1.问题梳理: 异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not ...
- 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign
上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...
- Spring Cloud Feign 整合 Hystrix
在前面随笔Spring Cloud 之 Feign的feign工程基础上进行改造 1.pom.xml依赖不变 2.application.yml文件添加feign.hystrix.enabled=tr ...
- Spring Cloud 之 Feign
新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...
- SpringCloud Feign对Hystrix(断路由)的支持
第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...
- SpringCloud Feign使用详解
添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- 工作随笔——自动重发的凶手--feign
公司使用的feign(https://github.com/OpenFeign/feign)作为http客户端. 开发时debug后端程序,发现同一个请求会多次收到. 为了判断是谁在搞鬼,在客户端和服 ...
随机推荐
- 打印出现:You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:
解决办法: 第一种:Product -> Target -> Capabilities -> Background Modes -> 勾选Remote notification ...
- OSG addEventHandler W键显示网格 L键控制光照 F键切换全屏窗口 S键显示统计数据 事件处理器
#include <osgGA/StateSetManipulator> #include <osgViewer/ViewerEventHandlers> // add the ...
- 深入理解ByteBuffer
ByteBuffer类是在Java NIO中常常使用的一个缓冲区类,使用它可以进行高效的IO操作,但是,如果对常用方法的理解有错误,那么就会出现意想不到的bug. ByteBuffer类的常用方法 先 ...
- Vue基础-匿名插槽与作用域插槽的合并和覆盖行为
Vue 测试版本:Vue.js v2.5.13 Vue 文档: <slot> 元素可以用一个特殊的特性 name 来进一步配置如何分发内容.多个插槽可以有不同的名字.具名插槽将匹配内容片段 ...
- 获取 js DOM元素中绑定的所有事件,模仿 chrome getEventListeners
偶尔看到了这个问题,如何用JS获取元素某一事件上绑定的所有Listener? 突然觉得好像是有解决办法的,查了下,在 chrome 下,支持 window.getEventListeners(obj) ...
- 04.Curator Leader选举
在分布式计算中,leader election是很重要的一个功能,这个选举过程是这样子的:指派一个进程作为组织者,将任务分发给各节点.在任务开始前,哪个节点都不知道谁是leader或者coor ...
- cocos2d-x常用工具
这些工具平常也用到,不过没有像这样整理出来,这是我在网上看到的.就记录一下. 位图字体工具Bitmap Font Tools BMFont (Windows)FonteditorGlyph Desig ...
- 160229-02、Sublime Text 3 快捷键总结
选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...
- mysql数据类型字段插入空字符串自动填充为0报错
有一条这样的sql语句, insert into a(id,user_name) values('','abc'); 表示插入字段的时候ID插入是空字符串.这样的情况在mysql5.6版本上执行报错. ...
- Ubuntu 下 chromium浏览器的标签乱码
最近使用ubuntu系统,安装了个chromium浏览器,标题不会乱码,不过标签乱码,火狐浏览器没有这个问题,强大的互联网告诉我是少字体的原因,执行下面的指令即可. sudo apt-get inst ...