Spring Cloud学习笔记【三】服务消费者Feign

Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单。它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件。Feign 还提供了 HTTP 请求的模板,通过编写简单的接口和插入注解,我们就可以定义好 HTTP 请求的参数、格式、地址等信息。接下来,Feign 会完全代理 HTTP 的请求,我们只需要像调用方法一样调用它就可以完成服务请求。

Feign 具有如下特性:

  • 可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解
  • 支持可插拔的 HTTP 编码器和解码器
  • 支持 Hystrix 和它的 Fallback
  • 支持 Ribbon 的负载均衡
  • 支持 HTTP 请求和响应的压缩

上一篇中已经有服务提供者了,这次我们继续延用不再另写。

创建一个spring start project,添加以下依赖

 1 <dependencies>
 2    <dependency>
 3       <groupId>org.springframework.boot</groupId>
 4       <artifactId>spring-boot-starter-web</artifactId>
 5    </dependency>
 6    <dependency>
 7       <groupId>org.springframework.cloud</groupId>
 8       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 9    </dependency>
10    <dependency>
11       <groupId>org.springframework.cloud</groupId>
12       <artifactId>spring-cloud-starter-openfeign</artifactId>
13    </dependency>
14 </dependencies>

配置属性(application.yml)

server:
  port: 8083
spring:
  application:
    name: service-consumer-feign
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:123456@localhost:8761/eureka/

启动类开启FeignClients

 1 package com.carry.springcloud;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 import org.springframework.cloud.openfeign.EnableFeignClients;
 7 
 8 @EnableFeignClients
 9 @EnableEurekaClient
10 @SpringBootApplication
11 public class ServiceConsumerFeignApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(ServiceConsumerFeignApplication.class, args);
15     }
16 }

创建一个FeignClient接口

 1 package com.carry.springcloud.api;
 2 
 3 import org.springframework.cloud.openfeign.FeignClient;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 
 6 @FeignClient(name = "service-producer")
 7 public interface ConsumerFeignClient {
 8 
 9     @GetMapping("/getPortInfo")
10     public String produce();
11 }

解释:

  • @FeignClient(name="service-producer") 标明feign调用的微服务名称
  • @GetMapping("/getPortInfo") 对应service-producer微服务中的URL

创建一个Controller注入FeignClient并调用服务

 1 package com.carry.springcloud.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import com.carry.springcloud.api.ConsumerFeignClient;
 8 
 9 @RestController
10 public class ConsumerController {
11     
12     @Autowired
13     private ConsumerFeignClient feignClient;
14     
15     @GetMapping("/getPoducerInfoByFeign")
16     public String getPoducerInfoByFeign() {
17         return feignClient.produce();
18     }
19 
20 }

测试

上图Eureka中服务提供者以及消费者feign已经注册成功

访问localhost:8083/getPoducerInfoByFeign,出现如下结果

再次访问

结果显示轮询访问8080和8081,访问正常。

posted @ 2018-08-14 15:18  CarryChan  阅读(773)  评论(0编辑  收藏  举报