我们对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 -
网友评论