04、SpringCloud Ribbon

一、Client代碼示例

說明:此處使用的SpringBoot版本為2.1.13.RELEASE朋其,SpringCloud版本為Greenwich.SR5
為了演示負載均衡就需要有多個client,此處我們只需要將之前的client復(fù)制一份久又,稍作修改即可
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>
    </dependencies>

2、application.yml
client

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

client2

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

3膜眠、啟動類
啟動類是一樣的岩臣,此處代碼就只貼了一份

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

4、其他java類
Controller類

package org.example.controller;

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 {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return "hello,"+name;
    }
}
package example.controller;

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 {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return "hello2,"+name;
    }
}

兩個服務(wù)的Controller類實際上應(yīng)該是一樣的宵膨,此處為了演示負載均衡架谎,稍作修改一個為“return "hello,"+name;”,另一個為“return "hello2,"+name;”

二辟躏、代碼示例

說明:此處使用的SpringBoot版本為2.1.13.RELEASE谷扣,SpringCloud版本為Greenwich.SR5
SpringCloud實現(xiàn)負載均衡比較簡單 ,只需要在RestTemplate類上加上一個注解@LoadBalanced,并修改下RestTemplate調(diào)用方式捎琐,原始使用的是url直接調(diào)用会涎,現(xiàn)在改為通過服務(wù)調(diào)用
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>
    </dependencies>

2瑞凑、application.yml

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

3末秃、啟動類

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Ribbon9002Application {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4、其他java類
Controller
調(diào)用地址格式:http://{服務(wù)}/hello/name

package org.example.controller;

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;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return restTemplate.getForObject("http://client/hello/"+name,String.class);
    }
}

2籽御、測試驗證

先后啟動server练慕、client、consumer,訪問http://localhost:7001/

image.png

多次訪問http://localhost:9002/hello/zs
交替出現(xiàn)
image.png

image.png

SpringCloud默認使用輪詢策略進行負載均衡技掏,如果想修改策略铃将,只需要增加IRue注入到Spring容器

    @Bean
    IRule iRule(){
        return new RandomRule();//IRule實現(xiàn)類,此處為隨機
    }

再次多次訪問http://localhost:9002/hello/zs哑梳,不再試交替出現(xiàn)“hello,zs”和“hello2,zs”劲阎,而是隨機出現(xiàn)
我們也可以自己定義負載均衡算法,大家可以參照:https://blog.csdn.net/www1056481167/article/details/81151064

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/
還有尚硅谷周陽老師的視頻

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鸠真,一起剝皮案震驚了整個濱河市悯仙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌弧哎,老刑警劉巖雁比,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異撤嫩,居然都是意外死亡偎捎,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茴她,“玉大人寻拂,你說我怎么就攤上這事≌衫危” “怎么了祭钉?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長己沛。 經(jīng)常有香客問我慌核,道長,這世上最難降的妖魔是什么申尼? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任垮卓,我火速辦了婚禮,結(jié)果婚禮上师幕,老公的妹妹穿的比我還像新娘粟按。我一直安慰自己,他們只是感情好霹粥,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布灭将。 她就那樣靜靜地躺著,像睡著了一般后控。 火紅的嫁衣襯著肌膚如雪庙曙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天忆蚀,我揣著相機與錄音矾利,去河邊找鬼。 笑死馋袜,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的舶斧。 我是一名探鬼主播欣鳖,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼茴厉!你這毒婦竟也來了泽台?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤矾缓,失蹤者是張志新(化名)和其女友劉穎怀酷,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嗜闻,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡蜕依,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片样眠。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡友瘤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出檐束,到底是詐尸還是另有隱情辫秧,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布被丧,位于F島的核電站盟戏,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甥桂。R本人自食惡果不足惜柿究,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望格嘁。 院中可真熱鬧笛求,春花似錦、人聲如沸糕簿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽懂诗。三九已至蜂嗽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間殃恒,已是汗流浹背植旧。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留离唐,地道東北人病附。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像亥鬓,于是被迫代替她去往敵國和親完沪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

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