Spring Cloud Netflix的微服务都是以HTTP接口的形式暴露的,因此能够用Apache的HttpClient或Spring的RestTemplate去调用java
而Feign是一个使用起来更加方便的HTTP客户端,它用起來就好像调用本地方法同样,彻底感受不到是调用的远程方法 git
Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,经过编写简单的接口和插入注解,就能够定义好HTTP请求的参数、格式、地址等信息。 github
Feign会彻底代理HTTP请求,咱们只须要像调用方法同样调用它就能够完成服务请求及相关处理。spring
Feign整合了Ribbon和Hystrix(关于Hystrix咱们后面再讲),可让咱们再也不须要显式地使用这两个组件。 json
总起来讲,Feign具备以下特性: app
https://github.com/Netflix/feign负载均衡
pom.xmlide
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
资源配置文件微服务
## server
server.port=8082
##eureka
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
eureka.instance.instance-id=${spring.application.name}:${server.port}
# 设置微服务调用地址为IP优先(缺省为false)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=30
# 发呆时间,即服务续约到期时间(缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=9
起始文件post
@EnableEurekaClient public class StartMain { public static void main(String[] args) { SpringApplication.run(StartMain.class, args); } }
接口controller,能够没有facade层的接口,这里是为拉给java其余客户端调用设计的。
//controller层 @RestController @RequestMapping(value = "/Promotion",method = RequestMethod.GET) public class PromotionController implements PromotionFacade { @Override @RequestMapping(value = "/delete") public String releasePromotion(@RequestParam int orderID){ return orderID+"-- hello!"; } } //facade层 public interface PromotionFacade { /** * 释放门店优惠券 */ String releasePromotion(@RequestParam int orderID); }
好啦,这个启动起来以后,就注册到啦eureka中啦,等待被调用吧。
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
资源配置文件
## server
server.port=8083
##eureka
eureka.instance.instance-id=${spring.application.name}:${server.port}
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
#租期更新时间间隔(3)
eureka.instance.leaseRenewalIntervalInSeconds=30
#租期到期时间(默认90秒)
eureka.instance.leaseExpirationDurationInSeconds=90
起始文件
@SpringBootApplication @EnableFeignClients(basePackages = { "trade.order.remote" }) @EnableEurekaClient public class StartMain { public static void main(String[] args) { SpringApplication.run(StartMain.class, args); } }
调用类
//remote层 @FeignClient(name = "trade-promotion") public interface PromotionClient { @RequestMapping(value = "/Promotion/delete",method = RequestMethod.GET) String releasePromotion(@RequestParam int orderID) ; } //controller 层 @RestController @RequestMapping(value = "/promotion", method = RequestMethod.GET) public class PromotionController { @Autowired PromotionClient promotionClient; @RequestMapping(value = "/delete") public String delete(@RequestParam int orderID) { return promotionClient.releasePromotion(orderID); } }
note:
1.上述调用只又remote就能够啦。
2.注意路由地址大小写规范,不要觉得本身很牛逼,一会大一会小,要我说,你很挫,一直小就能够啦。。
@PathVariable
/** * 在服务提供者咱们有一个方法是用直接写在连接,SpringMVC中用的@PathVariable * 这里边和SpringMVC中有些有一点点出入,SpringMVC中只有一个参数并且参数名的话是不用额外指定参数名的,而feign中必须指定 */ @RequestMapping(value = "/greet/{dd}",method = RequestMethod.GET) String greetFeign(@PathVariable("dd") String dd);
@RequestParam
/** * 这里说下@RequestParam 注解和SpringMVC中差异也是不大,我认为区别在于Feign中的是参数进入URL或请求体中, * 而SpringMVC中是参数从请求体中到方法中 * @param ids id串,好比“1,2,3” * @return */ @RequestMapping(value = "/users",method = RequestMethod.GET) public List<User> getUsersByIds(@RequestParam("ids") List<Long> ids);
@RequestHeader
/** * 这里是将参数添加到Headers中 * @param name 参数 */ @RequestMapping(value = "/headers") String getParamByHeaders(@RequestHeader("name") String name);
@RequestBody
/** * 调用服务提供者的post方法,接收回来再被服务提供者丢回来 * @param user User对象 */ @RequestMapping(value = "/user", method = RequestMethod.POST) User getUserByRequestBody(@RequestBody User user);
##这两行便可开启压缩
feign.compression.request.enabled=true
feign.compression.response.enabled=true
## 配置压缩具体信息
feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048
Feign对日志的处理很是灵活,可为每一个Feign客户端指定日志记录策略,每一个Feign客户端都会建立一个logger。默认状况下,logger的名称是Feigh接口的完整类名。须要注意的是,Feign的日志打印只会对Debug级别作出响应。
咱们能够为每一个Feign客户端配置各类的Logger.Level对象,告诉Feign记录哪些日志。Logger.Level的值有如下选择。
开启记录
logging.level.com.cnblogs.hellxz.client.EurekaServiceFeign: DEBUG
多看文档吧