Spring Cloud微服務(wù): 服務(wù)注冊發(fā)現(xiàn)Eureka,服務(wù)調(diào)用Feign滤灯,熔斷器Hystrix

1.Spring Cloud簡介

Spring Cloud是一系列框架的有序集合坪稽。它利用Spring Boot的開發(fā)便利性巧妙地簡化了分布式系統(tǒng)基礎(chǔ)設(shè)施的開發(fā),如服務(wù)發(fā)現(xiàn)注冊鳞骤、配置中心窒百、消息總線、負(fù)載均衡豫尽、斷路器篙梢、數(shù)據(jù)監(jiān)控等,都可以用Spring Boot的開發(fā)風(fēng)格做到一鍵啟動(dòng)和部署美旧。

Spring Cloud并沒有重復(fù)制造輪子渤滞,它只是將目前各家公司開發(fā)的比較成熟贬墩、經(jīng)得起實(shí)際考驗(yàn)的服務(wù)框架組合起來,通過Spring Boot風(fēng)格進(jìn)行再封裝妄呕,屏蔽掉了復(fù)雜的配置和實(shí)現(xiàn)原理陶舞,最終給開發(fā)者留出了一套簡單易懂、易部署和易維護(hù)的分布式系統(tǒng)開發(fā)工具包绪励。

2.Spring Cloud使用示例

工具列表:IDEA肿孵、Maven

項(xiàng)目構(gòu)成:服務(wù)注冊中心、服務(wù)提供者疏魏、服務(wù)消費(fèi)者停做。

項(xiàng)目結(jié)構(gòu)如下:

image

2.1.創(chuàng)建空白工程

為了方便管理,我們創(chuàng)建一個(gè)空白工程大莫,我們例子中的3個(gè)項(xiàng)目都作為這個(gè)空白工程的子模塊創(chuàng)建蛉腌。

主要步驟如下:

1.File->New->Project,選擇Maven

image

2.點(diǎn)擊Next只厘,輸入相關(guān)信息

image

3.創(chuàng)建完成后把其它文件都刪掉烙丛,只保留pom.xml即可。

2.2.服務(wù)注冊發(fā)現(xiàn)模塊

此模塊對應(yīng)項(xiàng)目結(jié)構(gòu)中的demo-eureka

主要步驟如下:

1.在主工程上File->New->Module懈凹,選擇Spring Initializr

image

2.點(diǎn)擊Next掏愁,輸入模塊信息

image

3.點(diǎn)擊Next磨总,選擇Cloud Discovery->Eureka Server

image

4.生成的pom文件部分配置

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

5.啟動(dòng)類,加上@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class DemoEurekaApplication {

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

6.配置文件application.properties添加如下配置

# 端口號
server.port=8000
# 關(guān)閉自我保護(hù)
eureka.server.enable-self-preservation=false
# 清理服務(wù)器時(shí)間間隔[5s]
eureka.server.eviction-interval-timer-in-ms=5000
# 主機(jī)名
eureka.instance.hostname=localhost
# 是否將自己作為客戶端注冊到Eureka Server[當(dāng)前模塊只是作為Eureka Server服務(wù)端所以設(shè)為false]
eureka.client.register-with-eureka=false
# 是否從Eureka Server獲取注冊信息[當(dāng)前是單點(diǎn)的Eureka Server所以不需要同步其它節(jié)點(diǎn)的數(shù)據(jù)]
eureka.client.fetch-registry=false
# Eureka Server[查詢注冊服務(wù)]地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

2.3.服務(wù)提供者模塊

此模塊對應(yīng)項(xiàng)目結(jié)構(gòu)中的demo-provider

1.創(chuàng)建過程不再贅述航罗,與創(chuàng)建服務(wù)注冊發(fā)現(xiàn)模塊的區(qū)別是爬舰,依賴中選擇:

  • Web->Web
  • Cloud Discovery->Eureka Server

2.pom文件依賴配置如下

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

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

3.啟動(dòng)類们陆,加上@EnableDiscoveryClient,表示其作為服務(wù)發(fā)現(xiàn)客戶端

@SpringBootApplication
@EnableDiscoveryClient
public class DemoProviderApplication {

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

}

4.application.properties添加如下配置

# 端口號
server.port=8010
# 應(yīng)用名稱
spring.application.name=provider
# Eureka Server服務(wù)器地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

5.定義MyController類

@RestController
public class MyController {

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String getDefaultInfo() {
        try {
            //休眠2秒情屹,測試超時(shí)服務(wù)熔斷[直接關(guān)閉服務(wù)提供者亦可]
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "Hello provider";
    }

    @RequestMapping(value = "/info/{name}", method = RequestMethod.POST)
    public String getInfo(@PathVariable String name) {
        return "Hello " + name;
    }

}

2.3.服務(wù)消費(fèi)者模塊

此模塊對應(yīng)項(xiàng)目結(jié)構(gòu)中的demo-consumer

1.創(chuàng)建過程不再贅述坪仇,與創(chuàng)建服務(wù)注冊發(fā)現(xiàn)模塊的區(qū)別是,依賴中選擇:

  • Web->Web
  • Cloud Discovery->Eureka Server
  • Cloud Routing->Feign

2.pom文件依賴配置如下

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

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

3.啟動(dòng)類垃你,加上@EnableFeignClients和@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class DemoConsumerApplication {

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

}

4.application.properties添加如下配置

# 端口號
server.port=8020
# 應(yīng)用名稱
spring.application.name=consumer
# Eureka Server服務(wù)器地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
# 高版本spring-cloud-openfeign請求分為兩層椅文,先ribbon控制,后hystrix控制.
# Ribbon請求連接的超時(shí)時(shí)間
ribbon.ConnectionTimeout=5000
# Ribbon請求處理的超時(shí)時(shí)間.
ribbon.ReadTimeout=5000
# 設(shè)置服務(wù)熔斷超時(shí)時(shí)間[默認(rèn)1s]
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
# 開啟hystrix以支持服務(wù)熔斷[高版本默認(rèn)false關(guān)閉]惜颇,如果置為false皆刺,則請求超時(shí)交給ribbon控制.
# feign.hystrix.enabled=true

5.定義服務(wù)接口類InfoClient,作為調(diào)用遠(yuǎn)程服務(wù)的本地入口

/**
 * FeignClient配置說明
 * 1.name:被調(diào)用的服務(wù)應(yīng)用名稱
 * 2.fallback: InfoFallBack作為熔斷實(shí)現(xiàn)凌摄,當(dāng)請求provider失敗時(shí)調(diào)用其中的方法
 * 3.configuration: feign配置
 */
@FeignClient(name = "provider", fallback = InfoFallBack.class, configuration = MyFeignConfig.class)
public interface InfoClient {

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    String getDefaultInfo();

    @RequestMapping(value = "/info/{name}", method = RequestMethod.POST)
    String getInfo(@PathVariable("name") String name);

}

6.定義熔斷類InfoFallBack羡蛾,如果遠(yuǎn)程服務(wù)無法成功請求,則調(diào)用指定的本地邏輯方法

@Component
public class InfoFallBack implements InfoClient {

    @Override
    public String getDefaultInfo() {
        return "fallback info";
    }

    @Override
    public String getInfo(String name) {
        return "fallback default info";
    }

}

7.定義feign配置類MyFeignConfig

@Configuration
public class MyFeignConfig {

    /**
     * Feign打印日志等級
     *
     * @return
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}

注意這里的Logger用的是feign下面的锨亏,需要引入

import feign.Logger;

8.定義服務(wù)調(diào)用類ConsumerController,通過本地方法入口調(diào)用遠(yuǎn)程服務(wù)

@RestController
public class ConsumerController {

    @Autowired
    private InfoClient infoClient;

    @RequestMapping(value = "/call/info", method = RequestMethod.GET)
    public String consumerInfo(){
        return infoClient.getDefaultInfo();
    }

    @RequestMapping(value = "/call/info/{name}", method = RequestMethod.GET)
    public String consumerInfo(@PathVariable String name){
        return infoClient.getInfo(name);
    }
}

2.4.測試

1.依次啟動(dòng)demo-eureka痴怨、demo-provider忙干、demo-consumer
2.訪問http://127.0.0.1:8000/
你會(huì)看到我們的提供者、消費(fèi)者都已經(jīng)注冊


image.png

3.訪問消費(fèi)者

image.png
image.png

4.測試服務(wù)熔斷捐迫,application.properties配置修改如下

# 設(shè)置服務(wù)熔斷超時(shí)時(shí)間[默認(rèn)1s]
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
# 開啟hystrix以支持服務(wù)熔斷[高版本默認(rèn)false關(guān)閉],如果置為false珠移,則請求超時(shí)交給ribbon控制.
feign.hystrix.enabled=true
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末弓乙,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子钧惧,更是在濱河造成了極大的恐慌暇韧,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浓瞪,死亡現(xiàn)場離奇詭異懈玻,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)乾颁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門涂乌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人英岭,你說我怎么就攤上這事湾盒。” “怎么了诅妹?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵罚勾,是天一觀的道長。 經(jīng)常有香客問我吭狡,道長尖殃,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任划煮,我火速辦了婚禮送丰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘弛秋。我一直安慰自己器躏,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布蟹略。 她就那樣靜靜地躺著登失,像睡著了一般。 火紅的嫁衣襯著肌膚如雪科乎。 梳的紋絲不亂的頭發(fā)上壁畸,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼捏萍。 笑死太抓,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的令杈。 我是一名探鬼主播走敌,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼逗噩!你這毒婦竟也來了掉丽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤异雁,失蹤者是張志新(化名)和其女友劉穎捶障,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纲刀,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡项炼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了示绊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锭部。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖面褐,靈堂內(nèi)的尸體忽然破棺而出拌禾,到底是詐尸還是另有隱情,我是刑警寧澤展哭,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布湃窍,位于F島的核電站,受9級特大地震影響摄杂,放射性物質(zhì)發(fā)生泄漏坝咐。R本人自食惡果不足惜循榆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一析恢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秧饮,春花似錦映挂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至泼各,卻和暖如春鞍时,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工逆巍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留及塘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓锐极,卻偏偏與公主長得像笙僚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子灵再,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

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