Linkerd——loadBalancer负载均衡

我们对Linkerd——本地运行完整案例,进行进一步的扩展,支持负载均衡。

新建两个微服务

两个微服务在同一个目录下,相当于一个服务启动了两个实例。目录结构如下:
+microservice
-response.json
-response1.json

  • response.json

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

启动服务:

$ python -m http.server 9999

  • response1.json

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

启动服务:

$ python -m http.server 9099

新建两个客户端服务

每个客户端负责调用一个微服务。

  • 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负载均衡配置

  • 首先配置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(其实默认是p2c,不用特别配置),你也可以配置成别的,参考官方文档。

启动linkerd

admin UI

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

微服务后台日志

我启动了三个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 -


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 你说 不以物喜 不以己悲 我倒喜欢嵇康,随性而活 不然 三生三世,又有何意义
    随忆a阅读 205评论 1 6
  • 文 | 熊六 超凡探长(一)(二)(三)(四)(五)(六)(七)(八)(九)(十)(十一)(十二)(十三)
    熊六阅读 263评论 0 0
  • 你是那株, 生长在石板路边的小花。 无人问津, 你将根深深往土壤里扎。 任风雨吹打, 你羸弱的花朵, 在倔强里拼命...
    四夕白水阅读 315评论 0 2