1. 程式人生 > >[SpringCloud] 入門-第三篇: 服務消費者(feign)

[SpringCloud] 入門-第三篇: 服務消費者(feign)

1. 什麼是feign

上個文章,我們是使用restTemplate + Ribbon 來實現分散式服務之間的生產和消費的呼叫的, 這篇文章來講一講SpringCloud分散式服務之間呼叫的另一個方案, feign

Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支援可插拔的編碼器和解碼器。Feign預設集成了Ribbon,並和Eureka結合,預設實現了負載均衡的效果

重點:

  • Feig採用的是基於介面的註解
  • Feign整合了Ribbon

2. 建立feign的服務

這裡接著上一篇使用Ribbon的文章繼續進行
這個時候我們已經有:
1. eurekaServer — 註冊中心
2. eureka-client-01 — 生產者,客戶端
3. service-ribbon — 使用ribbon呼叫eureka-client-01方法的消費者

新建一個spring-boot工程,(怕麻煩的話其實可以直接複製上面的service-ribbon, 把所有ribbon換成feign, pom檔案改吧改吧就好了)

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId
>
service-feign</artifactId> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

application.yml配置如下

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: service-feign
server:
  port: 8086

啟動類上面打上@EnableFeignClients註解開啟Feign的功能

package com.zgd.springcloud.eurekaClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


/**
 * eureka client 客戶端
 */
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication(scanBasePackages = "com.zgd.springcloud.eurekaClient")
public class FeignApp {

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

}

這裡和ribbon區別的地方就來了

ribbon是直接定義一個serviceImpl, 在裡面用restTemplate去訪問暴露在外面的其他服務提供者的mvc介面

feign直接定義一個介面,通過@ FeignClient(“服務名”),來指定呼叫哪個服務
下面註解中的@RequestMapping的value都是eureka-client-01模組需要呼叫的方法的路由

package com.zgd.springcloud.eurekaClient.interf;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @FeignClient value是指定的服務提供者在eurekaServer中的服務名,也就是eureka-client-01的applicationName: client01
 */
@FeignClient(value = "client01")
@Component
public interface FeignService{

    /**
     * 這裡還是訪問的eureka-client-01的
     *      com.zgd.springcloud.eurekaClient.controller.ProducerController
     *      的hello方法的路由
     * @param name
     * @return
     */
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String hiService(@RequestParam(value = "name") String name);
}

在Web層的controller層, 對外暴露一個api介面

package com.zgd.springcloud.eurekaClient.controller;

import com.zgd.springcloud.eurekaClient.interf.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class FeignController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")

    @Autowired
    FeignService feignService;
    /**
     * 通過service層,用restTemplate訪問Client01路由,間接呼叫了Client01工程的方法
     * @param name
     * @return
     */
    @RequestMapping(value = "/getHi",method = RequestMethod.GET)
    public String getHi(@RequestParam String name) {
        return feignService.hiService( name );
    }

}

這個時候因為介面沒有實現類,不用擔心會報錯空指標異常, 前提是你的app啟動類上確實用了@EnableFeignClients

4. 啟動程式

  1. 啟動eurekaServer
  2. 啟動eureka-client-01, 更改埠號, 再次啟動
  3. 啟動service-feign

多次訪問,可以看到輪詢呼叫了兩個埠

這裡寫圖片描述

這裡寫圖片描述

5.原始碼

相關推薦

[SpringCloud] 入門-: 服務消費者feign)

1. 什麼是feign 上個文章,我們是使用restTemplate + Ribbon 來實現分散式服務之間的生產和消費的呼叫的, 這篇文章來講一講SpringCloud分散式服務之間呼叫的另一個方案, feign Feign是一個宣告式的偽Http客戶端,

SpringCloud教程 | : 服務消費者Feign)(Finchley版本)

lns efault ret java 服務註冊 star tco target hub 上一篇文章,講述了如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個聲明式的偽Http客戶端

【轉載】SpringCloud教程 | : 服務消費者Feign

上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個

史上最簡單的SpringCloud教程 | : 服務消費者Feign

上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。 一、Feign簡介 Feign是一個宣告式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註

SpringCloud教程 | : 服務消費者Feign

上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。 一、Feign簡介 Feign是一個宣告式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,

史上最簡單的SpringCloud教程 | : 服務消費者Feign)(Finchley版本)

上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解

SpringCloud | : 服務消費者Feign

上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有

企業級 SpringCloud 教程 服務消費者Feign

pom https www. ram cat -h 客戶端 tin script 上一篇文章,講述了如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。一、Feign簡介 Feign是一個聲明式的偽Http客戶端,它使得寫

企業分布式微服務SpringCloud SpringBoot mybatis 服務消費者Feign

test 它的 artifact color tor 實現 特性 -- pac 一、Feign簡介 Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創建一個接口並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-

SpringCloud教程 | 第二: 服務消費者Feign

一、Feign簡介Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支援可插拔的編碼器和解碼器。Feign預設集成了Ribbon,並

spring-cloud服務消費者Feign)(Finchley版本)

Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支援可插拔的編碼器和解碼器。Feign預設集成了Ribbon,並和Eureka結合,預設實現了負載均衡的效

springcloud 系列教程四:服務消費者Feign

一、Feign簡介 Feign 的英文表意為 "假裝,偽裝,變形", 是一個http請求呼叫的輕量級框架,可

史上最簡單的SpringCloud教程 | 服務消費者Feign

最新Finchley版本請訪問: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 或者 http://blog.csdn.net/forezp/article/details/810409

SpringCloud Greenwich版本】章:服務消費者Feign

一、SpringCloud版本 本文介紹的Springboot版本為2.1.1.RELEASE,SpringCloud版本為Greenwich.RC1,JDK版本為1.8,整合環境為IntelliJ IDEA 二、Feign介紹 Feign是一個宣告式的Web服務客戶端。這使得W

SpringCloud進擊 | 淺出:服務消費者Feign)【Finchley版本】

1.前言 上一節:SpringCloud進擊 | 二淺出:服務消費者(Ribbon+REST)【Finchley版本】 上一節講述瞭如何通過 Ribbon + RestTemplate 的方式去消費服務,而在實際工作中,我們基本上都是使用 Feign 來完成呼叫。這篇就來說說如何通過

在eclipse上部署springcloud小例子--:斷路器Hystrix)

            在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互呼叫(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來呼叫。為了保證其高可用,

在eclipse上部署springcloud小例子--第二服務消費者feign

上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用

史上最簡單的SpringCloud教程 | 第二: 服務消費者rest+ribbon)(Finchley版本)

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於rib

SpringCloud | 第二: 服務消費者rest+ribbon)

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於r

史上最簡單的SpringCloud教程 | 第二: 服務消費者rest+ribbon)

2017年04月08日 23:25:26 226350人閱讀 評論(226) 收藏