05、SpringCloud Feign

一、代碼示例

說(shuō)明:此處使用的SpringBoot版本為2.1.13.RELEASE,SpringCloud版本為Greenwich.SR5
Feign可以理解為聲明式調(diào)用REST方式船惨。
此處就不再貼server與client的代碼
1、maven依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

2缕陕、application.yml

server:
  port: 9003
spring:
  application:
    name: feign
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: feign-9003
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: feign-9003
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

3粱锐、啟動(dòng)類
啟動(dòng)類增加注解@EnableFeignClients啟動(dòng)Feign

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class Feign9003Application {
    public static void main(String[] args) {
        SpringApplication.run(Feign9003Application.class,args);
    }
}

4、其他java類
Feign對(duì)應(yīng)的接口類
定義一個(gè)接口扛邑,增加@FeignClient(value = "CLIENT")怜浅,@FeignClient對(duì)應(yīng)的VALUE為需要調(diào)用的服務(wù)名,接口中的方法與需要調(diào)用服務(wù)的方法一致并增加@GetMapping("/hello/{name}")(根據(jù)調(diào)用的方式選擇)
@FeignClient會(huì)將接口注入到容器

package org.example.service;

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

@FeignClient(value = "CLIENT")
public interface FeignService {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable(value = "name") String name);
}

Controller類
注入前面定義的Feign接口類

package org.example.controller;

import org.example.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private FeignService feignService;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return feignService.hello(name);
    }
}

2鹿榜、測(cè)試驗(yàn)證

先后啟動(dòng)server海雪、client、feign對(duì)應(yīng)的服務(wù)舱殿,訪問(wèn)http://localhost:7001/

image.png

訪問(wèn)http://localhost:9003/hello/zs
交替出現(xiàn)
image.png

image.png

說(shuō)明Feign默認(rèn)集成了負(fù)載均衡奥裸,若想修改負(fù)載均衡策略,像容器注入IRule即可沪袭,具體見(jiàn)上一文章湾宙。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽(yáng)老師的視頻

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市冈绊,隨后出現(xiàn)的幾起案子侠鳄,更是在濱河造成了極大的恐慌,老刑警劉巖死宣,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伟恶,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡毅该,警方通過(guò)查閱死者的電腦和手機(jī)博秫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)眶掌,“玉大人挡育,你說(shuō)我怎么就攤上這事∑优溃” “怎么了即寒?”我有些...
    開(kāi)封第一講書人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)召噩。 經(jīng)常有香客問(wèn)我母赵,道長(zhǎng),這世上最難降的妖魔是什么蚣常? 我笑而不...
    開(kāi)封第一講書人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任市咽,我火速辦了婚禮,結(jié)果婚禮上抵蚊,老公的妹妹穿的比我還像新娘施绎。我一直安慰自己,他們只是感情好贞绳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布谷醉。 她就那樣靜靜地躺著,像睡著了一般冈闭。 火紅的嫁衣襯著肌膚如雪俱尼。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,763評(píng)論 1 307
  • 那天萎攒,我揣著相機(jī)與錄音遇八,去河邊找鬼矛绘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛刃永,可吹牛的內(nèi)容都是我干的货矮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼斯够,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼囚玫!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起读规,我...
    開(kāi)封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤抓督,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后束亏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铃在,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年枪汪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涌穆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡雀久,死狀恐怖宿稀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赖捌,我是刑警寧澤祝沸,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站越庇,受9級(jí)特大地震影響罩锐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜卤唉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一涩惑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧桑驱,春花似錦竭恬、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至押框,卻和暖如春岔绸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工盒揉, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留晋被,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓刚盈,卻偏偏與公主長(zhǎng)得像墨微,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子扁掸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容