cluster_single.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('SentinelClusterSingleController', ['$scope', '$stateParams', 'ngDialog',
  3. 'MachineService', 'ClusterStateService',
  4. function ($scope, $stateParams, ngDialog, MachineService, ClusterStateService) {
  5. $scope.app = $stateParams.app;
  6. const UNSUPPORTED_CODE = 4041;
  7. const CLUSTER_MODE_CLIENT = 0;
  8. const CLUSTER_MODE_SERVER = 1;
  9. $scope.macsInputConfig = {
  10. searchField: ['text', 'value'],
  11. persist: true,
  12. create: false,
  13. maxItems: 1,
  14. render: {
  15. item: function (data, escape) {
  16. return '<div>' + escape(data.text) + '</div>';
  17. }
  18. },
  19. onChange: function (value, oldValue) {
  20. $scope.macInputModel = value;
  21. }
  22. };
  23. function convertSetToString(set) {
  24. if (set === undefined) {
  25. return '';
  26. }
  27. let s = '';
  28. for (let i = 0; i < set.length; i++) {
  29. s = s + set[i];
  30. if (i < set.length - 1) {
  31. s = s + ',';
  32. }
  33. }
  34. return s;
  35. }
  36. function convertStrToNamespaceSet(str) {
  37. if (str === undefined || str === '') {
  38. return [];
  39. }
  40. let arr = [];
  41. let spliced = str.split(',');
  42. spliced.forEach((v) => {
  43. arr.push(v.trim());
  44. });
  45. return arr;
  46. }
  47. function fetchMachineClusterState() {
  48. if (!$scope.macInputModel || $scope.macInputModel === '') {
  49. return;
  50. }
  51. let mac = $scope.macInputModel.split(':');
  52. ClusterStateService.fetchClusterUniversalStateSingle($scope.app, mac[0], mac[1]).success(function (data) {
  53. if (data.code == 0 && data.data) {
  54. $scope.loadError = undefined;
  55. $scope.stateVO = data.data;
  56. $scope.stateVO.currentMode = $scope.stateVO.stateInfo.mode;
  57. if ($scope.stateVO.server && $scope.stateVO.server.namespaceSet) {
  58. $scope.stateVO.server.namespaceSetStr = convertSetToString($scope.stateVO.server.namespaceSet);
  59. }
  60. } else {
  61. $scope.stateVO = {};
  62. if (data.code === UNSUPPORTED_CODE) {
  63. $scope.loadError = {message: '机器 ' + mac[0] + ':' + mac[1] + ' 的 Sentinel 客户端版本不支持集群限流,请升级至 1.4.0 以上版本并引入相关依赖。'}
  64. } else {
  65. $scope.loadError = {message: data.msg};
  66. }
  67. }
  68. }).error((data, header, config, status) => {
  69. $scope.loadError = {message: '未知错误'};
  70. });
  71. }
  72. fetchMachineClusterState();
  73. function checkValidClientConfig(stateVO) {
  74. if (!stateVO.client || !stateVO.client.clientConfig) {
  75. alert('不合法的配置');
  76. return false;
  77. }
  78. let config = stateVO.client.clientConfig;
  79. if (!config.serverHost || config.serverHost.trim() == '') {
  80. alert('请输入有效的 Token Server IP');
  81. return false;
  82. }
  83. if (config.serverPort === undefined || config.serverPort <= 0 || config.serverPort > 65535) {
  84. alert('请输入有效的 Token Server 端口');
  85. return false;
  86. }
  87. if (config.requestTimeout === undefined || config.requestTimeout <= 0) {
  88. alert('请输入有效的请求超时时长');
  89. return false;
  90. }
  91. return true;
  92. }
  93. function sendClusterClientRequest(stateVO) {
  94. if (!checkValidClientConfig(stateVO)) {
  95. return;
  96. }
  97. if (!$scope.macInputModel) {
  98. return;
  99. }
  100. let mac = $scope.macInputModel.split(':');
  101. let request = {
  102. app: $scope.app,
  103. ip: mac[0],
  104. port: mac[1],
  105. };
  106. request.mode = CLUSTER_MODE_CLIENT;
  107. request.clientConfig = stateVO.client.clientConfig;
  108. ClusterStateService.modifyClusterConfig(request).success(function (data) {
  109. if (data.code == 0 && data.data) {
  110. alert('修改集群限流客户端配置成功');
  111. window.location.reload();
  112. } else {
  113. if (data.code === UNSUPPORTED_CODE) {
  114. alert('机器 ' + mac[0] + ':' + mac[1] + ' 的 Sentinel 客户端版本不支持集群限流客户端,请升级至 1.4.0 以上版本并引入相关依赖。');
  115. } else {
  116. alert('修改失败:' + data.msg);
  117. }
  118. }
  119. }).error((data, header, config, status) => {
  120. alert('未知错误');
  121. });
  122. }
  123. function checkValidServerConfig(stateVO) {
  124. if (!stateVO.server || !stateVO.server.transport) {
  125. alert('不合法的配置');
  126. return false;
  127. }
  128. if (stateVO.server.namespaceSetStr === undefined || stateVO.server.namespaceSetStr == '') {
  129. alert('请输入有效的命名空间集合(多个 namespace 以 , 分隔)');
  130. return false;
  131. }
  132. let transportConfig = stateVO.server.transport;
  133. if (transportConfig.port === undefined || transportConfig.port <= 0 || transportConfig.port > 65535) {
  134. alert('请输入有效的 Token Server 端口');
  135. return false;
  136. }
  137. let flowConfig = stateVO.server.flow;
  138. if (flowConfig.maxAllowedQps === undefined || flowConfig.maxAllowedQps < 0) {
  139. alert('请输入有效的最大允许 QPS');
  140. return false;
  141. }
  142. // if (transportConfig.idleSeconds === undefined || transportConfig.idleSeconds <= 0) {
  143. // alert('请输入有效的连接清理时长 (idleSeconds)');
  144. // return false;
  145. // }
  146. return true;
  147. }
  148. function sendClusterServerRequest(stateVO) {
  149. if (!checkValidServerConfig(stateVO)) {
  150. return;
  151. }
  152. if (!$scope.macInputModel) {
  153. return;
  154. }
  155. let mac = $scope.macInputModel.split(':');
  156. let request = {
  157. app: $scope.app,
  158. ip: mac[0],
  159. port: mac[1],
  160. };
  161. request.mode = CLUSTER_MODE_SERVER;
  162. request.flowConfig = stateVO.server.flow;
  163. request.transportConfig = stateVO.server.transport;
  164. request.namespaceSet = convertStrToNamespaceSet(stateVO.server.namespaceSetStr);
  165. ClusterStateService.modifyClusterConfig(request).success(function (data) {
  166. if (data.code == 0 && data.data) {
  167. alert('修改集群限流服务端配置成功');
  168. window.location.reload();
  169. } else {
  170. if (data.code === UNSUPPORTED_CODE) {
  171. alert('机器 ' + mac[0] + ':' + mac[1] + ' 的 Sentinel 客户端版本不支持集群限流服务端,请升级至 1.4.0 以上版本并引入相关依赖。');
  172. } else {
  173. alert('修改失败:' + data.msg);
  174. }
  175. }
  176. }).error((data, header, config, status) => {
  177. alert('未知错误');
  178. });
  179. }
  180. $scope.saveConfig = () => {
  181. let ok = confirm('是否确定修改集群限流配置?');
  182. if (!ok) {
  183. return;
  184. }
  185. let mode = $scope.stateVO.stateInfo.mode;
  186. if (mode != 1 && mode != 0) {
  187. alert('未知的集群限流模式');
  188. return;
  189. }
  190. if (mode == 0) {
  191. sendClusterClientRequest($scope.stateVO);
  192. } else {
  193. sendClusterServerRequest($scope.stateVO);
  194. }
  195. };
  196. function queryAppMachines() {
  197. MachineService.getAppMachines($scope.app).success(
  198. function (data) {
  199. if (data.code === 0) {
  200. // $scope.machines = data.data;
  201. if (data.data) {
  202. $scope.machines = [];
  203. $scope.macsInputOptionsOrigin = [];
  204. $scope.macsInputOptions = [];
  205. data.data.forEach(function (item) {
  206. if (item.healthy) {
  207. $scope.macsInputOptionsOrigin.push({
  208. text: item.ip + ':' + item.port,
  209. value: item.ip + ':' + item.port
  210. });
  211. }
  212. });
  213. $scope.macsInputOptions = $scope.macsInputOptionsOrigin;
  214. }
  215. if ($scope.macsInputOptions.length > 0) {
  216. $scope.macInputModel = $scope.macsInputOptions[0].value;
  217. }
  218. } else {
  219. $scope.macsInputOptions = [];
  220. }
  221. }
  222. );
  223. }
  224. queryAppMachines();
  225. $scope.$watch('searchKey', function () {
  226. if (!$scope.macsInputOptions) {
  227. return;
  228. }
  229. if ($scope.searchKey) {
  230. $scope.macsInputOptions = $scope.macsInputOptionsOrigin
  231. .filter((e) => e.value.indexOf($scope.searchKey) !== -1);
  232. } else {
  233. $scope.macsInputOptions = $scope.macsInputOptionsOrigin;
  234. }
  235. if ($scope.macsInputOptions.length > 0) {
  236. $scope.macInputModel = $scope.macsInputOptions[0].value;
  237. } else {
  238. $scope.macInputModel = '';
  239. }
  240. });
  241. $scope.$watch('macInputModel', function () {
  242. if ($scope.macInputModel) {
  243. fetchMachineClusterState();
  244. }
  245. });
  246. }]);