天天看点

SpringCloud学习之路(四) 服务消费者Feign (Finchley版本)

SpringCloud学习之路(四) 服务消费者Feign (Finchley版本)

一、Feign简介

Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地调用HTTP API。

在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。

Spring Cloud对Feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。

总结:Feign整合了ribbon让服务之间的调用更加简单 方便。

二 、搭建工程

在原先的项目下 新建一个model,起名为service-feign 如下图:

SpringCloud学习之路(四) 服务消费者Feign (Finchley版本)

pom.xml 如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.scfhy</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.scfhy</groupId>
        <artifactId>sc-f-finchley</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

           

然后建立如下图的目录

SpringCloud学习之路(四) 服务消费者Feign (Finchley版本)

先说第一个MyRule这个类吧 其实这个在上一篇已经讲过了 而且代码也是一样的 ,如下:

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MyIRule {
    @Bean
    public IRule rule() {
      //  return new RandomRule(); //按照顺序选择服务(默认策略)
        return new RandomRule(); //随机选择服务
    }
}

           

然后是对外暴露的方法FeignTestController 代码如下:

import com.scfhy.servicefeign.server.FeignTestServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignTestController {
    @Autowired
    FeignTestServer feignTestServer;

    @GetMapping("/hi")
    public String hi(String str) {
        return feignTestServer.getTest(str);
    }

}
           

最后是 FeignTestServer 这个类 这里里面就比较重要了,是调用其它服务的方法,然后大家可以对比上一篇文章 ribbon 他们两个之间调用的不同点。

代码如下:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "SERVICE-CLIENT")
public interface FeignTestServer {

    @GetMapping(value = "/getTest")
    String getTest(@RequestParam(value = "str") String str);

}

           

通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了eureka-client服务的“/getTest”接口

注意:eureka-client服务的这个getTest 接口我在前面的文章已经写了。不知道的看前面的文章。

三、

在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能

代码如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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


}
           

忘记还要配置类里面代码了了。

application.yml

如下:

server:
  port: 8085

spring:
  application:
    name: service-feign

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

  instance:
    instance-id: service-feign-8085
    prefer-ip-address: true


info:
  app.name: com.scfhy
  company.name: www.baidu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


           

最后启动工程

启动eureka-server,端口为8082; 启动eureka-client 两次,端口分别为8083、8086 ;再启动service-feign,端口号为:8085

然后在浏览器访问:http://localhost:8085/hi?str=fast 多次访问

浏览器交替显示:

fast:8083

fast:8086

好了 这个Feign就讲到这里。