SpringCloud教程- 服务消费者(Feign)(SpringCloud版本Finchley)

前言:上篇文章讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何使用Feign去消费服务

代码地址:github-spring-cloud地址

一、Feign简介

Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

总结:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon

二、 环境准备

使用上一章功能启动eureka-server 和eureka-hello

三、创建基于Feign服务

定义启动类

注解@EnableFeignClients表明通过feign方式调用服务

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }

}

pom文件配置

<?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">
    <parent>
        <artifactId>spring-cloud-learn</artifactId>
        <groupId>com.sl.learn.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sl.learn.cloud</groupId>
    <artifactId>eureka-feign</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

注意SpringCloud版本为Finchley,feign版本用的是
spring-cloud-starter-openfeign

配置文件application.yml

server:
  port: 8086

spring:
  application:
    name: eureka-feign

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
    shutdown:
      enabled: true

定义一个feign接口

通过@ FeignClient(“服务名”),来指定调用哪个服务

@FeignClient("eureka-hello")
public interface FeignService {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

定义一个controller

在Web层的controller层,对外暴露一个"/hi"的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务

@RestController
public class FeignController {

    @Autowired
    FeignService feignService;

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String feign(@RequestParam(value = "name") String name) {
        return feignService.sayHiFromClientOne(name);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

境里婆娑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值