浏览代码

增加负载均衡

chanjunkai 5 年之前
父节点
当前提交
22376100bc
共有 4 个文件被更改,包括 246 次插入7 次删除
  1. 2 1
      azoauth/config.py
  2. 5 1
      azoauth/urls.py
  3. 96 0
      controller/distribution.py
  4. 143 5
      controller/index.py

+ 2 - 1
azoauth/config.py

@@ -15,8 +15,9 @@
 RESP_SERVER_DOMAIN_DATA = {
     'CN': 'rtsp.zositech.xyz',
     # 'en': 'www.zositech.org'
-    'EN': 'rtsp.zositech.org'
+    'EN': 'rtsp.zositech.org',
     #'HK': '119.8.33.56'
+    'EN1':'18.222.107.129'
 }
 SERVER_PREFIX = 'http://www.dvema.com'
 #Easydarwin

+ 5 - 1
azoauth/urls.py

@@ -17,6 +17,7 @@ Including another URLconf
 from django.contrib import admin
 from django.urls import path
 from controller import index
+from controller import distribution
 
 urlpatterns = [
     path('admin/', admin.site.urls),
@@ -31,7 +32,10 @@ urlpatterns = [
     path('oa2/runstream', index.testRunStream),
     #测试服务器是否正常接口
     path('oa2/test', index.test),
-    #压测负载均衡接口
+    #负载均衡接口test(后期可删)
     path('oa2/loadserver', index.loadBalancingServer),
+    #测试分发
+    path('test/select', distribution.selectConnectNum.as_view()),       #查询连接数并返回url
+    path('test/storage', distribution.storageConnectNum.as_view()),       #存储连接数
 
 ]

+ 96 - 0
controller/distribution.py

@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+"""
+import json
+import time
+import requests
+from django.views.generic import TemplateView
+from django.shortcuts import render_to_response
+from django.http import JsonResponse
+from object.ResObject import ResObject
+import subprocess
+# from gevent.pool import Pool
+from model.models import UserModel,UidRtspModel
+from object.tkObject import tkObject
+from service.CommonService import CommonService
+from object.RedisObject import RedisObject
+
+rtspServer = "rtsp.zositech.org,18.222.107.129"
+
+class selectConnectNum(TemplateView):
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = json.loads(request.body.decode('utf-8'))
+        return self.select(request_dict)
+
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = request.GET
+        return self.select(request_dict)
+
+    # def select(self, request_dict):
+    #     uid = 'xixixixiixixixixix'
+    #     response = ResObject()
+    #     redisObj = RedisObject(db=1)
+    #     rtko = tkObject(rank=1)
+    #     rtsp_stream = rtko.encrypt(data=uid)
+    #     res = redisObj.z_revrange('url')
+    #     url = res[0]
+    #     rtspUrl = str(url) + rtsp_stream
+    #     return response.json(0, res=rtspUrl)
+    def select(self, request_dict):
+        response = ResObject()
+        redisObj = RedisObject(db=1)
+        urls = rtspServer.split(',')
+        httpPrefix = 'http://'
+        rtspPrefix = 'rtsp://'
+        postfix = ':10008/api/v1/pushers'
+
+        for url in urls:
+            res = redisObj.get_data(url)
+            print(res)
+
+
+
+class storageConnectNum(TemplateView):
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = json.loads(request.body.decode('utf-8'))
+        return self.storage(request_dict)
+
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = request.GET
+        return self.storage(request_dict)
+
+    def storage(self, request_dict):
+        response = ResObject()
+        redisObj = RedisObject(db=1)
+        urls = rtspServer.split(',')
+        httpPrefix = 'http://'
+        # rtspPrefix = 'rtsp://'
+        postfix = ':10008/api/v1/pushers'
+
+        for url in urls:
+            apiUrl = httpPrefix + url + postfix
+            try:
+                selectRtsp = requests.get(url=apiUrl, timeout=5)
+                connectNum = selectRtsp.json()['total']
+            except Exception as e:
+                connectNum = -1
+            redisObj.set_data(url, connectNum)
+            value1 = redisObj.get_data(key='rtsp.zositech.org')
+            print(value1)
+        return response.json(0, res='yes')
+
+
+
+
+
+
+
+
+
+
+

+ 143 - 5
controller/index.py

@@ -286,7 +286,7 @@ class oa2RtspStartView(TemplateView):
                 'pwd':PWD,
                 'msg':MSG
             }
-            redisObj.set_data(key=key, val=str(res_json), expire=30)
+            redisObj.set_data(key=key, val=str(res_json), expire=15)
             return JsonResponse(res_json, safe=False)
         else:
             return JsonResponse({'msg': 'wrong'})
@@ -458,11 +458,149 @@ def testRunStream(request):
 def test(request):
     return JsonResponse({'msg': 'Server running normal', 'code': 0})
 
-#压测负载均衡接口
+#test接口
 def loadBalancingServer(request):
-    res = requests.get('http://rtsp.zositech.xyz:10008/api/v1/players')
-    return JsonResponse({'msg': res, 'code': 0})
-    # return JsonResponse({'msg': 'tesring', 'code': 0})
+    try:
+        res1 = requests.get('http://rtsp.zositech.org:10008/api/v1/players', timeout=0.001)
+    except Exception as e:
+        res1 = "server error"
+    print(res1)
+    #res2 = requests.get('http://18.222.107.129:10008/api/v1/players', timeout=0.001)
+    print("res1请求时间:%s"%res1)
+    # print("res2请求时间:%s"%res2)
+    # rtsp = 'rtsp.zositech.xyz'
+    # rtsp1 = 'rtsp.zositech.org'
+    # rtsp2 = 'rtsp.zositech.org'
+    # RD = RedisObject(db=9)
+    # # #国内
+    # key = 'CHINA'
+    # # #国外1
+    # key1 = 'USA1'
+    # # #国外2
+    # key2 = 'USA2'
+    # total = RD.get_data(key=key)
+    # total1 = RD.get_data(key=key1)
+    # total2 = RD.get_data(key=key2)
+    from datetime import datetime
+    # res1 = requests.get('http://rtsp.zositech.org:10008/api/v1/players')
+    # time3=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    # print("时间3:%s"%time3)
+    # res2 = requests.get('http://18.222.107.129:10008/api/v1/players')
+    # time4=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    # print("时间4:%s"%time4)
+    # res_data1 = res1.json()
+    # res_data2 = res1.json()
+    # total1 = res_data1['total']
+    # total2 = res_data2['total']
+    # RD.set_data()
+    # mm=min(total,total1)
+    # print(mm)
+    # if total1 == mm:
+    #     return JsonResponse({'msg': rtsp1, 'code': 0})
+    # else:
+    #     return JsonResponse({'msg': rtsp2, 'code': 0})
+
+    # RD = RedisObject(db=9)
+    # rtsp = 'rtsp.zositech.xyz'
+    # rtsp1 = 'rtsp.zositech.org'
+    # rtsp2 = 'rtsp.zositech.org'
+    # print("1")
+    # #国内
+    # key = 'CHINA'
+    # #国外1
+    # key1 = 'USA1'
+    # #国外2
+    # key2 = 'USA2'
+    # total = RD.get_data(key=key)
+    # total1 = RD.get_data(key=key1)
+    # total2 = RD.get_data(key=key2)
+    # print("2")
+    # #读缓存判断在线人数,返回人少的服务器ip
+    # if total:
+    #     print("进缓存了")
+    #     mm = min(total,total1,total2)
+    #     if total == mm:
+    #         return JsonResponse({'msg': rtsp, 'code': 0})
+    #     elif total1 == mm:
+    #         return JsonResponse({'msg': rtsp1, 'code': 0})
+    #     else:
+    #         return JsonResponse({'msg': rtsp2, 'code': 0})
+    # print("3")
+    # from datetime import datetime
+    # time1=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    # print("时间1:%s"%time1)
+    # res = requests.get('http://rtsp.zositech.xyz:10008/api/v1/players')
+    # time2=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    # print("时间2:%s"%time2)
+    # res1 = requests.get('http://rtsp.zositech.org:10008/api/v1/players')
+    # time3=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    # print("时间3:%s"%time3)
+    # res2 = requests.get('http://18.222.107.129:10008/api/v1/players')
+    # time4=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    # print("时间4:%s"%time4)
+    # res_data = res.json()
+    # res_data1 = res1.json()
+    # res_data2 = res2.json()
+    # total = res_data['total']
+    # total1 = res_data1['total']
+    # total2 = res_data2['total']
+    # #redis存内容
+    # RD.set_data(key=key, val=total, expire=10)
+    # RD.set_data(key=key1, val=total1, expire=10)
+    # RD.set_data(key=key2, val=total2, expire=10)
+    # print("4")
+    # mm=min(total,total1,total2)
+    # if total == mm:
+    #     return JsonResponse({'msg': rtsp, 'code': 0})
+    # elif total1 == mm:
+    #     return JsonResponse({'msg': rtsp1, 'code': 0})
+    # else:
+    #     return JsonResponse({'msg': rtsp2, 'code': 0})
+    #
+    try:
+        res1 = requests.get('http://rtsp.zositech.org:10008/api/v1/players', timeout=5)
+    except Exception as e:
+        res1 = -1
+
+    time1=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    print("时间1:%s"%time1)
+    # ---------压测接口demo------------
+    redisObj8 = RedisObject(db=8)
+    key1 = 'USA'
+    key2 = 'USA1'
+    redis_data1 = redisObj8.get_data(key1)
+    redis_data2 = redisObj8.get_data(key2)
+    if redis_data1 and redis_data2:
+        print("进缓存")
+        mm=min(redis_data1,redis_data2)
+        if redis_data1 == mm and redis_data1 != -1:
+            RESP_SERVER_DOMAIN = RESP_SERVER_DOMAIN_DATA['EN']
+        elif redis_data2 == mm and redis_data2 != -1:
+            RESP_SERVER_DOMAIN = RESP_SERVER_DOMAIN_DATA['EN1']
+    #return JsonResponse({'msg': RESP_SERVER_DOMAIN, 'code': 0})
+    # ---------/压测demo------------
+    else:
+        print("第一次")
+        res1 = requests.get('http://rtsp.zositech.org:10008/api/v1/players', timeout=0.001)
+        res2 = requests.get('http://18.222.107.129:10008/api/v1/players', timeout=0.001)
+        res_data1 = res1.json()
+        res_data2 = res1.json()
+        total1 = res_data1['total']
+        total2 = res_data2['total']
+        print(total1)
+        print(total2)
+        redisObj8.set_data(key=key1, val=total1, expire=30)
+        redisObj8.set_data(key=key2, val=total2, expire=30)
+        mm=min(total1,total2)
+        print(mm)
+        if total1 == mm:
+            RESP_SERVER_DOMAIN = RESP_SERVER_DOMAIN_DATA['EN']
+        else:
+            RESP_SERVER_DOMAIN = RESP_SERVER_DOMAIN_DATA['EN1']
+    time1=datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
+    print("时间1:%s"%time1)
+    return JsonResponse({'msg': 'The number of people online is :%s'%res1, 'code': 0})
+