SpringCloud之服务消费者(Feign)(Finchley版本)

avatar 2018年07月25日12:21:47 6 2514 views
博主分享免费Java教学视频,B站账号:Java刘哥
在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章讲解下基于 feign 。  

一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。 简而言之:
  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力
 

二、准备工作

继续用上一节的工程, 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.  

三、创建一个feign的服务

新建一个spring-boot工程,取名为serice-feign,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-netflix-eureka-client、Web的起步依赖spring-boot-starter-web,代码如下:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.liuyanzhao.cloud</groupId>
  5.     <artifactId>service-feign</artifactId>
  6.     <version>0.0.1-SNAPSHOT</version>
  7.     <packaging>jar</packaging>
  8.     <name>service-feign</name>
  9.     <description>Demo project for Spring Boot</description>
  10.     <parent>
  11.         <groupId>com.liuyanzhao.cloud</groupId>
  12.         <artifactId>SpringCloud</artifactId>
  13.         <version>0.0.1-SNAPSHOT</version>
  14.     </parent>
  15.     <dependencies>
  16.         <dependency>
  17.             <groupId>org.springframework.cloud</groupId>
  18.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  19.         </dependency>
  20.         <dependency>
  21.             <groupId>org.springframework.boot</groupId>
  22.             <artifactId>spring-boot-starter-web</artifactId>
  23.         </dependency>
  24.         <dependency>
  25.             <groupId>org.springframework.cloud</groupId>
  26.             <artifactId>spring-cloud-starter-openfeign</artifactId>
  27.         </dependency>
  28.     </dependencies>
  29. </project>
  在工程的配置文件 application.yml 文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:
  1. eureka:
  2.   client:
  3.     serviceUrl:
  4.       defaultZone: http://localhost:8761/eureka/
  5. server:
  6.   port: 8765
  7. spring:
  8.   application:
  9.     name: service-feign
  10. feign:
  11.   hystrix:
  12.     enabled: true
  在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:
  1. package com.liuyanzhao.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.openfeign.EnableFeignClients;
  7. @SpringBootApplication
  8. @EnableEurekaClient
  9. @EnableDiscoveryClient
  10. @EnableFeignClients
  11. public class SericeFeignApplication {
  12.     public static void main(String[] args) {
  13.         SpringApplication.run(SericeFeignApplication.class, args);
  14.     }
  15. }
  定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:
  1. package com.liuyanzhao.cloud.service;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. /**
  7.  * @author 言曌
  8.  * @date 2018/7/24 下午3:35
  9.  */
  10. @FeignClient(value = "service-hi")
  11. public interface SchedualServiceHi {
  12.     @RequestMapping(value = "/hi", method = RequestMethod.GET)
  13.     String sayHiFromClientOne(@RequestParam(value = "name") String name);
  14. }
  在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端 SchedualServiceHi 来消费服务。代码如下:
  1. package com.liuyanzhao.cloud.controller;
  2. import com.liuyanzhao.cloud.service.SchedualServiceHi;
  3. import com.liuyanzhao.cloud.service.SchedualServiceLu;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9.  * @author 言曌
  10.  * @date 2018/7/24 下午3:46
  11.  */
  12. @RestController
  13. public class HiController {
  14.     //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
  15.     @Autowired
  16.     SchedualServiceHi schedualServiceHi;
  17.     @Autowired
  18.     SchedualServiceLu schedualServiceLu;
  19.     @GetMapping(value = "/hi")
  20.     public String sayHi(@RequestParam String name) {
  21.         return schedualServiceHi.sayHiFromClientOne( name );
  22.     }
  23. }
启动程序,多次访问http://localhost:8765/hi?name=言曌,浏览器交替显示: hi 言曌,i am from port:8080 hi 言曌,i am from port:8081     代码地址:https://github.com/saysky/Spring-Cloud-Study-Demo
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0