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);
    }
}
发布了143 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/TreeShu321/article/details/103317728
今日推荐