Skip to content

Commit 027db9c

Browse files
committed
server添加gray lock接口
1 parent f4594cd commit 027db9c

File tree

4 files changed

+152
-14
lines changed

4 files changed

+152
-14
lines changed

spring-cloud-gray-server/src/main/java/cn/springcloud/gray/server/module/gray/GrayServerModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,8 @@ default String getServiceContextPath(String serviceId) {
7474
boolean isActiveGrayInstance(String instanceId);
7575

7676
boolean isActiveGrayInstance(GrayInstance grayInstance);
77+
78+
void closeGrayLock(String instanceId);
79+
80+
void openGrayLock(String instanceId);
7781
}

spring-cloud-gray-server/src/main/java/cn/springcloud/gray/server/module/gray/jpa/JPAGrayServerModule.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,49 @@ public boolean isActiveGrayInstance(GrayInstance grayInstance) {
272272
isLockGray(grayInstance) ||
273273
grayServerProperties.getInstance().getNormalInstanceStatus().contains(grayInstance.getInstanceStatus()));
274274
}
275+
276+
@Override
277+
public void closeGrayLock(String instanceId) {
278+
GrayInstance instance = grayInstanceService.findOneModel(instanceId);
279+
if (Objects.isNull(instance) || Objects.equals(instance.getGrayLock(), GrayInstance.GRAY_UNLOCKED)) {
280+
return;
281+
}
282+
283+
instance.setGrayLock(GrayInstance.GRAY_UNLOCKED);
284+
grayInstanceService.saveModel(instance);
285+
286+
//推送实例变更信息
287+
if (Objects.equals(instance.getGrayStatus(), GrayStatus.CLOSE)) {
288+
return;
289+
}
290+
291+
boolean isNormalInstanceStatus = grayServerProperties.getInstance()
292+
.getNormalInstanceStatus()
293+
.contains(instance.getInstanceStatus());
294+
if (!isNormalInstanceStatus) {
295+
triggerDeleteEvent(instance);
296+
}
297+
}
298+
299+
@Override
300+
public void openGrayLock(String instanceId) {
301+
GrayInstance instance = grayInstanceService.findOneModel(instanceId);
302+
if (Objects.isNull(instance) || Objects.equals(instance.getGrayLock(), GrayInstance.GRAY_LOCKED)) {
303+
return;
304+
}
305+
instance.setGrayLock(GrayInstance.GRAY_LOCKED);
306+
grayInstanceService.saveModel(instance);
307+
308+
//推送实例变更信息
309+
if (Objects.equals(instance.getGrayStatus(), GrayStatus.CLOSE)) {
310+
return;
311+
}
312+
313+
boolean isNormalInstanceStatus = grayServerProperties.getInstance()
314+
.getNormalInstanceStatus()
315+
.contains(instance.getInstanceStatus());
316+
if (!isNormalInstanceStatus) {
317+
triggerEvent(TriggerType.MODIFY, instance);
318+
}
319+
}
275320
}

spring-cloud-gray-server/src/main/java/cn/springcloud/gray/server/resources/rest/GrayInstanceResource.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,32 @@ public ApiRes<Void> switchGrayStatus(@RequestParam("id") String instanceId,
120120
}
121121
}
122122

123+
@RequestMapping(value = "/switchLock", method = RequestMethod.PUT)
124+
public ApiRes<Void> switchGrayLock(@RequestParam("id") String instanceId,
125+
@ApiParam(value = "开关{0: unlock, 1: lock}", defaultValue = "0") @RequestParam("switch") int onoff) {
126+
127+
if (StringUtils.isNotEmpty(userModule.getCurrentUserId())) {
128+
if (!serviceManageModule.hasServiceAuthority(
129+
grayServiceIdFinder.getServiceId(GrayModelType.INSTANCE, instanceId))) {
130+
return ApiResHelper.notAuthority();
131+
}
132+
}
133+
134+
switch (onoff) {
135+
case 1:
136+
grayServerModule.openGrayLock(instanceId);
137+
return ApiRes.<Void>builder()
138+
.code(CODE_SUCCESS)
139+
.build();
140+
case 0:
141+
grayServerModule.closeGrayLock(instanceId);
142+
return ApiRes.<Void>builder()
143+
.code(CODE_SUCCESS)
144+
.build();
145+
default:
146+
throw new UnsupportedOperationException("不支持的开关类型");
147+
}
148+
}
149+
123150

124151
}

spring-cloud-gray-webui/src/views/gray-instance/complex-table.vue

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,38 @@
4444
</el-table-column>
4545
<el-table-column label="Gray Status" class-name="status-col" min-width="100px">
4646
<template slot-scope="scope">
47-
<el-tag>{{ scope.row.grayStatus }}</el-tag>
47+
<el-dropdown trigger="click">
48+
<el-tag style="cursor:pointer">{{ scope.row.grayStatus }}</el-tag>
49+
<el-dropdown-menu slot="dropdown">
50+
<el-dropdown-item v-if="scope.row.grayStatus=='CLOSE'" @click.native="handleSwitchGrayStatus(scope.row, 1)">Open</el-dropdown-item>
51+
<el-dropdown-item v-if="scope.row.grayStatus=='OPEN'" @click.native="handleSwitchGrayStatus(scope.row, 0)">Close</el-dropdown-item>
52+
</el-dropdown-menu>
53+
</el-dropdown>
4854
</template>
4955
</el-table-column>
5056
<el-table-column label="Gray Lock" class-name="status-col" min-width="100px">
5157
<template slot-scope="scope">
52-
<el-tag :type="scope.row.grayLock | grayLockStatusFilter">{{ scope.row.grayLock | grayLockFilter }}</el-tag>
58+
<el-dropdown trigger="click">
59+
<el-tag style="cursor:pointer" :type="scope.row.grayLock | grayLockStatusFilter">{{ scope.row.grayLock | grayLockFilter }}</el-tag>
60+
<el-dropdown-menu slot="dropdown">
61+
<el-dropdown-item v-if="scope.row.grayLock==0" @click.native="handleSwitchGrayLock(scope.row, 1)">Lock</el-dropdown-item>
62+
<el-dropdown-item v-if="scope.row.grayLock==1" @click.native="handleSwitchGrayLock(scope.row, 0)">UnLock</el-dropdown-item>
63+
</el-dropdown-menu>
64+
</el-dropdown>
5365
</template>
5466
</el-table-column>
5567
<el-table-column label="Instance Status" min-width="110px">
5668
<template slot-scope="scope">
57-
<el-tag>{{ scope.row.instanceStatus }}</el-tag>
69+
<el-dropdown trigger="click">
70+
<el-tag style="cursor:pointer">{{ scope.row.instanceStatus }}</el-tag>
71+
<el-dropdown-menu slot="dropdown">
72+
<el-dropdown-item @click.native="changeInstanceStatus(scope.row, 'STARTING')">STARTING</el-dropdown-item>
73+
<el-dropdown-item @click.native="changeInstanceStatus(scope.row, 'UP')">UP</el-dropdown-item>
74+
<el-dropdown-item @click.native="changeInstanceStatus(scope.row, 'OUT_OF_SERVICE')">OUT_OF_SERVICE</el-dropdown-item>
75+
<el-dropdown-item @click.native="changeInstanceStatus(scope.row, 'DOWN')">DOWN</el-dropdown-item>
76+
<el-dropdown-item @click.native="changeInstanceStatus(scope.row, 'UNKNOWN')">UNKNOWN</el-dropdown-item>
77+
</el-dropdown-menu>
78+
</el-dropdown>
5879
</template>
5980
</el-table-column>
6081
<el-table-column label="Des" class-name="status-col" min-width="100px">
@@ -79,17 +100,21 @@
79100
</el-table-column>-->
80101
<el-table-column label="Actions" align="center" class-name="small-padding fixed-width">
81102
<template slot-scope="{row}">
82-
<el-button type="primary" size="mini" class="list-button" @click="handleUpdate(row)">
83-
Edit
84-
</el-button>
103+
<el-dropdown trigger="click">
104+
<el-button size="mini" type="primary" style="width:80px" class="list-button">
105+
编辑
106+
<i class="el-icon-arrow-down" />
107+
</el-button>
108+
<el-dropdown-menu slot="dropdown">
109+
<el-dropdown-item @click.native="handleUpdate(row)">修改</el-dropdown-item>
110+
<el-dropdown-item @click.native="handleDelete(row)">删除</el-dropdown-item>
111+
</el-dropdown-menu>
112+
</el-dropdown>
85113
<router-link :to="`/routingPolicy/instanceGrayPolicyList?ns=${ns}&moduleId=${row.serviceId}&resource=${escapeStr(row.instanceId)}`">
86114
<el-button size="mini" type="success" class="list-button">
87115
策略
88116
</el-button>
89117
</router-link>
90-
<el-button size="mini" type="danger" class="list-button" @click="handleDelete(row)">
91-
Delete
92-
</el-button>
93118
<el-dropdown trigger="click">
94119
<el-button size="mini" type="info" style="width:80px" class="list-button">
95120
实例状态
@@ -138,11 +163,6 @@
138163
<el-option v-for="item in grayStatusOptions" :key="item" :label="item" :value="item" />
139164
</el-select>
140165
</el-form-item>
141-
<el-form-item label="Gray Lock" prop="grayStatus">
142-
<el-select v-model="temp.grayLock" class="filter-item" placeholder="Please select">
143-
<el-option v-for="item in grayLockOptions" :key="item.value" :label="item.label" :value="item.value" />
144-
</el-select>
145-
</el-form-item>
146166
<el-form-item label="Describe" prop="describe">
147167
<el-input v-model="temp.des" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
148168
</el-form-item>
@@ -183,6 +203,7 @@
183203

184204
<script>
185205
import { fetchList, deleteInstance, createInstance, updateInstance, tryChangeInstanceStatus } from '@/api/gray-instance'
206+
import { putData } from '@/api/api-request'
186207
import { getServiceAllInfos, getAllDefinitions } from '@/api/gray-client'
187208
import waves from '@/directive/waves' // waves directive
188209
import { parseTime } from '@/utils'
@@ -427,6 +448,47 @@ export default {
427448
})
428449
})
429450
},
451+
handleSwitchGrayStatus(row, switchVal) {
452+
const warningMSG = switchVal === 0 ? 'Confirm to close gray status by the record?' : 'Confirm to open gray status by the record?'
453+
this.$confirm(warningMSG, 'Warning', {
454+
confirmButtonText: 'Confirm',
455+
cancelButtonText: 'Cancel',
456+
type: 'warning'
457+
}).then(async() => {
458+
const url = '/gray/instance/switchStatus?id=' + escape(row.instanceId) + '&switch=' + switchVal
459+
putData(url).then(() => {
460+
this.dialogFormVisible = false
461+
const status = switchVal === 0 ? 'CLOSE' : 'OPEN'
462+
row.grayStatus = status
463+
this.$notify({
464+
title: 'Success',
465+
message: 'Operate Successfully',
466+
type: 'success',
467+
duration: 2000
468+
})
469+
})
470+
})
471+
},
472+
handleSwitchGrayLock(row, switchVal) {
473+
const warningMSG = switchVal === 0 ? 'Confirm to close gray lock by the record?' : 'Confirm to open gray lock by the record?'
474+
this.$confirm(warningMSG, 'Warning', {
475+
confirmButtonText: 'Confirm',
476+
cancelButtonText: 'Cancel',
477+
type: 'warning'
478+
}).then(async() => {
479+
const url = '/gray/instance/switchLock?id=' + escape(row.instanceId) + '&switch=' + switchVal
480+
putData(url).then(() => {
481+
this.dialogFormVisible = false
482+
row.grayLock = switchVal
483+
this.$notify({
484+
title: 'Success',
485+
message: 'Operate Successfully',
486+
type: 'success',
487+
duration: 2000
488+
})
489+
})
490+
})
491+
},
430492
async changeInstanceStatus(row, status) {
431493
tryChangeInstanceStatus(row, status).then(() => {
432494
this.dialogFormVisible = false

0 commit comments

Comments
 (0)