Linkerd——loadBalancer負(fù)載均衡

我們對Linkerd——本地運行完整案例,進(jìn)行進(jìn)一步的擴展,支持負(fù)載均衡。

新建兩個微服務(wù)

兩個微服務(wù)在同一個目錄下,相當(dāng)于一個服務(wù)啟動了兩個實例。目錄結(jié)構(gòu)如下:
+microservice
-response.json
-response1.json

  • response.json

{"name":"James","nation":"china","msg":"Hello World."}

啟動服務(wù):

$ python -m http.server 9999

  • response1.json

{"name":"Charlse","nation":"china","msg":"Hello beijing."}

啟動服務(wù):

$ python -m http.server 9099

新建兩個客戶端服務(wù)

每個客戶端負(fù)責(zé)調(diào)用一個微服務(wù)。

  • request.py

import requests
import threading,time

class SendRequest(threading.Thread):
  def __init__(self,interval,count):
    threading.Thread.__init__(self)
    self.interval = interval
    self.count = count
  def run(self):
    try:
      url = "http://localhost:4140/response.json"
      headers={"Host":"web"}
      result = requests.get(url,headers=headers)
      print(time.strftime('%Y-%m-%d %X',time.localtime())+" - "+str(self.count)+" - "+result.text)
      self.count = self.count+1 
    except Exception as e:
      print(e)
    finally:
      SendRequest(self.interval,self.count).start()

if __name__ == '__main__':
  count = 1
  SendRequest(0.1,count).start()

  • request1.py

import requests
import threading,time

class SendRequest(threading.Thread):
  def __init__(self,interval,count):
    threading.Thread.__init__(self)
    self.interval = interval
    self.count = count
  def run(self):
    try:
      url = "http://localhost:4140/response1.json"
      headers={"Host":"web"}
      result = requests.get(url,headers=headers)
      print(time.strftime('%Y-%m-%d %X',time.localtime())+" - "+str(self.count)+" - "+result.text)
      self.count = self.count+1 
    except Exception as e:
      print(e)
    finally:
      SendRequest(self.interval,self.count).start()

if __name__ == '__main__':
  count = 1
  SendRequest(0.1,count).start()

啟動客戶端

$ python request.py
$ python request1.py

最后看linkerd負(fù)載均衡配置

  • 首先配置disco/web
127.0.0.1 9999
127.0.0.1 9099

  • 再配置config/linkerd.yaml
routers:
- protocol: http
  dtab: |
    /wadd => /#/io.l5d.fs;
    /svc => /wadd/web
  httpAccessLog: logs/access.log
  label: web
  servers:
  - port: 4140
    ip: 0.0.0.0
  client:
    failureAccrual:
      kind: io.l5d.successRate
      successRate: 0.99
      requests: 1000
    loadBalancer:
      kind: p2c
      maxEffort: 5

這里loadBalancer算法采用的是p2c(其實默認(rèn)是p2c,不用特別配置),你也可以配置成別的,參考官方文檔。

啟動linkerd

admin UI

火狐截圖_2017-12-26T11-26-06.439Z.png

微服務(wù)后臺日志

我啟動了三個request1.py客戶端,所以看起來后臺response1.json多一些

127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2017 19:27:33] "GET /response1.json HTTP/1.1" 200 -


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 你說 不以物喜 不以己悲 我倒喜歡嵇康,隨性而活 不然 三生三世,又有何意義
    隨憶a閱讀 213評論 1 6
  • 文 | 熊六 超凡探長(一)(二)(三)(四)(五)(六)(七)(八)(九)(十)(十一)(十二)(十三)
    熊六閱讀 275評論 0 0
  • 你是那株, 生長在石板路邊的小花。 無人問津, 你將根深深往土壤里扎。 任風(fēng)雨吹打, 你羸弱的花朵, 在倔強里拼命...
    四夕白水閱讀 355評論 0 2

友情鏈接更多精彩內(nèi)容