天天看點

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就講到這裡。