Spring Cloud Ribbon

Spring Cloud Ribbon

  • 客戶端負(fù)載均衡組件Ribbon 負(fù)載均衡就是當(dāng)client端發(fā)送請(qǐng)求得時(shí)候會(huì)均衡得分發(fā)到每個(gè)server端

創(chuàng)建spring-cloud-02-ribbon-eureka 就是個(gè)服務(wù)端 參考前面得copy過來的

創(chuàng)建spring-cloud-02-ribbon-client1 項(xiàng)目

  • 引入pom.xml
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
  • UserController
@RestController
public class UserController {


    @RequestMapping(value = "/getUser", method = {RequestMethod.GET})
    public User getUser(@RequestParam("id") String id) {
        System.err.println("client1 GET-------------" + id);
        return new User(id, "20", "張三");
    }
}
  • application.properties
spring.application.name=client-service
server.port=1003
server.context-path=/
eureka.client.service-url.defaultZone=http://eureka1:8003/eureka/
spring.output.ansi.enabled=always
  • copy client1項(xiàng)目一份

創(chuàng)建 spring-cloud-02-ribbon-request

  • 引入pom.xml
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
  • application.properties
spring.application.name=request-service
server.context-path=/
server.port=1005
eureka.client.service-url.defaultZone=http://eureka1:8003/eureka/
spring.output.ansi.enabled=always
  • 啟動(dòng)所有 request訪問http://localhost:1005/get 當(dāng)請(qǐng)求得次數(shù)一樣時(shí)燥爷,會(huì)發(fā)現(xiàn)分別在client1和client2端會(huì)打印出響應(yīng)得次數(shù)秒梳,每個(gè)都打印出一半
  • 這就是負(fù)載均衡 Ribbon

在這里在介紹下retry重試機(jī)制

spring-cloud-02-ribbon-retry

  • pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
  • application.properties配置
spring.application.name=retry-service
server.context-path=/
server.port=1006
eureka.client.service-url.defaultZone=http://eureka1:8003/eureka/
#啟動(dòng)重試機(jī)制
spring.cloud.loadbalancer.retry.enabled=true 
##斷路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=14000
##連接超時(shí)時(shí)間
#client-service.ribbon.ConnectTimeout=1000
##處理超時(shí)時(shí)間
#client-service.ribbon.ReadTimeout=1000

req.fact.connect-timeout=1000 
req.fact.connection-request-timeout=1000
req.fact.read-timeout=3000
#是否對(duì)所有請(qǐng)求都進(jìn)行重試
client-service.ribbon.OKToRetryOnAllOperations=true
#重試切換實(shí)例得次數(shù)
client-service.ribbon.MaxAutoRetriesNextServer=1
#重試切次數(shù)
client-service.ribbon.MaxAutoRetries=5
  • 注意:
client-service.ribbon.ConnectTimeout=1000
client-service.ribbon.ReadTimeout=1000
這樣配置沒起作用  建議使用RestTemplate原始配置
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:斷路器得超時(shí)時(shí)間需要大于ribbon得超時(shí)時(shí)間,不然不會(huì)發(fā)生重試。
  • RetryApplication類
@EnableCircuitBreaker //開啟斷路器
@EnableRetry    //啟動(dòng)Retry框架重試機(jī)制
@EnableDiscoveryClient //我是服務(wù) 注冊(cè)到服務(wù)中心上
@SpringBootApplication
public class RetryApplication {


    @Bean
    @ConfigurationProperties(prefix = "req.fact")
    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){
        return new HttpComponentsClientHttpRequestFactory();
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate(httpComponentsClientHttpRequestFactory());
    }
    public static void main(String[] args) {
        SpringApplication.run(RetryApplication.class, args);
    }
}
  • IndexController類
@RestController
public class IndexController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/retry", method = {RequestMethod.GET})
    @HystrixCommand(fallbackMethod = "retryFailback")
    public String retry(){
        ResponseEntity<String> response = restTemplate.getForEntity("http://client-service/retry", String.class);
        System.err.println("body:" + response.getBody());
        return "----- retry ok -----";
    }

    public String retryFailback(){
        System.err.println("-----啟動(dòng)降級(jí)策略----");
        return "-----啟動(dòng)降級(jí)策略----";
    }
}
  • 在spring-cloud-02-ribbon-client1項(xiàng)目得Controller中添加
 @RequestMapping(value = "/retry", method = {RequestMethod.GET})
    public String retry() {
        System.err.println("client1  1 call-------------");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "client 1";
    }
  • 在spring-cloud-02-ribbon-client2項(xiàng)目得Controller中添加
 @RequestMapping(value = "/retry", method = {RequestMethod.GET})
    public String retry() {
        System.err.println("client1  2 call-------------");
        return "client 2";
    }
  • 在啟動(dòng)http://localhost:1006/retry時(shí)枷踏,通過Ribbon負(fù)載均衡會(huì)訪問client1或者client2項(xiàng)目得retry方法,當(dāng)訪問到client1得retry方法時(shí),配置得讀取超時(shí)時(shí)間為3ms斥赋,重試次數(shù)為5次,retry方法睡眠為4ms绢慢,就是每次訪問retry方法時(shí)重試灿渴,當(dāng)重試次數(shù)6次后,會(huì)采用降級(jí)策略胰舆,因?yàn)槿蹟嗥髋渲玫脼?4ms骚露,超過熔斷器得時(shí)間就會(huì)采用降級(jí)策略。
  • GitHub地址
  • hystrix 先入個(gè)門
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末缚窿,一起剝皮案震驚了整個(gè)濱河市棘幸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌倦零,老刑警劉巖误续,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異扫茅,居然都是意外死亡蹋嵌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門葫隙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來栽烂,“玉大人,你說我怎么就攤上這事恋脚∠侔欤” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵糟描,是天一觀的道長(zhǎng)怀喉。 經(jīng)常有香客問我,道長(zhǎng)船响,這世上最難降的妖魔是什么躬拢? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任躲履,我火速辦了婚禮,結(jié)果婚禮上估灿,老公的妹妹穿的比我還像新娘崇呵。我一直安慰自己,他們只是感情好馅袁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布域慷。 她就那樣靜靜地躺著,像睡著了一般汗销。 火紅的嫁衣襯著肌膚如雪犹褒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天弛针,我揣著相機(jī)與錄音叠骑,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的菊卷。 我是一名探鬼主播娩鹉,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼驮履,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤诅病,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后粥烁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贤笆,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年讨阻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了芥永。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡钝吮,死狀恐怖恤左,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情搀绣,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布戳气,位于F島的核電站链患,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏瓶您。R本人自食惡果不足惜麻捻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一纲仍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贸毕,春花似錦郑叠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至摊腋,卻和暖如春沸版,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背兴蒸。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工视粮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人橙凳。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓蕾殴,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親岛啸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子钓觉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

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