Skip to content

Commit 1787879

Browse files
committed
feat(register): add the opt-in server-issued registration lease (#8039)
1 parent a6104b9 commit 1787879

17 files changed

Lines changed: 1749 additions & 14 deletions

File tree

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/config/RegistrationProtocolInterceptor.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package org.apache.shenyu.admin.config;
1919

2020
import org.apache.shenyu.admin.config.properties.RegistrationFencingProperties;
21+
import org.apache.shenyu.admin.config.properties.RegistrationLeaseProperties;
2122
import jakarta.servlet.http.HttpServletRequest;
2223
import jakarta.servlet.http.HttpServletResponse;
2324
import org.apache.shenyu.register.common.protocol.RegistrationFencingProtocol;
25+
import org.apache.shenyu.register.common.protocol.RegistrationLeaseProtocol;
2426
import org.apache.shenyu.register.common.protocol.RegistrationProtocol;
2527
import org.springframework.web.servlet.HandlerInterceptor;
2628

@@ -36,20 +38,25 @@ public final class RegistrationProtocolInterceptor implements HandlerInterceptor
3638

3739
private final RegistrationFencingProperties fencingProperties;
3840

41+
private final RegistrationLeaseProperties leaseProperties;
42+
3943
/**
4044
* Construct the legacy-compatible interceptor for isolated callers.
4145
*/
4246
public RegistrationProtocolInterceptor() {
43-
this(null);
47+
this(null, null);
4448
}
4549

4650
/**
4751
* Construct an interceptor with capability advertisement settings.
4852
*
4953
* @param fencingProperties fencing properties
54+
* @param leaseProperties lease properties
5055
*/
51-
public RegistrationProtocolInterceptor(final RegistrationFencingProperties fencingProperties) {
56+
public RegistrationProtocolInterceptor(final RegistrationFencingProperties fencingProperties,
57+
final RegistrationLeaseProperties leaseProperties) {
5258
this.fencingProperties = fencingProperties;
59+
this.leaseProperties = leaseProperties;
5360
}
5461

5562
@Override
@@ -62,6 +69,9 @@ public boolean preHandle(final HttpServletRequest request, final HttpServletResp
6269
if (Objects.nonNull(fencingProperties) && fencingProperties.isEnabled()) {
6370
capabilities = capabilities + "," + RegistrationFencingProtocol.CAPABILITY;
6471
}
72+
if (Objects.nonNull(leaseProperties) && leaseProperties.isEnabled()) {
73+
capabilities = capabilities + "," + RegistrationLeaseProtocol.CAPABILITY;
74+
}
6575
response.setHeader(RegistrationProtocol.CAPABILITIES_HEADER, capabilities);
6676
List<String> versions = Collections.list(request.getHeaders(RegistrationProtocol.VERSION_HEADER));
6777
try {

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ShenyuAdminConfiguration.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.shenyu.admin.config;
1919

2020
import org.apache.shenyu.admin.config.properties.InstanceRetentionProperties;
21+
import org.apache.shenyu.admin.config.properties.RegistrationLeaseProperties;
2122
import org.apache.shenyu.admin.service.converter.SelectorHandleConverter;
2223
import org.apache.shenyu.admin.service.converter.SelectorHandleConverterFactor;
2324
import org.apache.shenyu.admin.service.publish.InstanceInfoReportEventPublisher;
@@ -41,7 +42,7 @@
4142
*/
4243
@Configuration
4344
@EnableTransactionManagement
44-
@EnableConfigurationProperties(InstanceRetentionProperties.class)
45+
@EnableConfigurationProperties({InstanceRetentionProperties.class, RegistrationLeaseProperties.class})
4546
public class ShenyuAdminConfiguration {
4647

4748
/**

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/config/WebConfiguration.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.shenyu.admin.config;
1919

2020
import org.apache.shenyu.admin.config.properties.RegistrationFencingProperties;
21+
import org.apache.shenyu.admin.config.properties.RegistrationLeaseProperties;
2122
import org.apache.shenyu.register.common.protocol.RegistrationProtocol;
2223
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.context.annotation.Configuration;
@@ -33,18 +34,23 @@ public class WebConfiguration implements WebMvcConfigurer {
3334

3435
private final RegistrationFencingProperties fencingProperties;
3536

37+
private final RegistrationLeaseProperties leaseProperties;
38+
3639
public WebConfiguration() {
3740
this.fencingProperties = new RegistrationFencingProperties();
41+
this.leaseProperties = new RegistrationLeaseProperties();
3842
}
3943

4044
@Autowired
41-
public WebConfiguration(final RegistrationFencingProperties fencingProperties) {
45+
public WebConfiguration(final RegistrationFencingProperties fencingProperties,
46+
final RegistrationLeaseProperties leaseProperties) {
4247
this.fencingProperties = fencingProperties;
48+
this.leaseProperties = leaseProperties;
4349
}
4450

4551
@Override
4652
public void addInterceptors(final InterceptorRegistry registry) {
47-
registry.addInterceptor(new RegistrationProtocolInterceptor(fencingProperties)).addPathPatterns(
53+
registry.addInterceptor(new RegistrationProtocolInterceptor(fencingProperties, leaseProperties)).addPathPatterns(
4854
RegistrationProtocol.META_PATH, RegistrationProtocol.URI_PATH, RegistrationProtocol.API_DOC_PATH,
4955
RegistrationProtocol.DISCOVERY_CONFIG_PATH, RegistrationProtocol.MCP_TOOLS_PATH,
5056
RegistrationProtocol.OFFLINE_PATH, RegistrationProtocol.BEAT_URI_PATH);
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shenyu.admin.config.properties;
19+
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
22+
import java.time.Duration;
23+
24+
/**
25+
* Properties of the opt-in server-issued registration lease
26+
* ({@code registration-lease-v1}). When disabled, the {@code /instance/beat}
27+
* endpoint keeps its exact legacy v1 behavior.
28+
*/
29+
@ConfigurationProperties(prefix = "shenyu.register.lease")
30+
public class RegistrationLeaseProperties {
31+
32+
/**
33+
* Default heartbeat interval when the client does not propose one.
34+
*/
35+
private static final Duration DEFAULT_INTERVAL = Duration.ofSeconds(5);
36+
37+
/**
38+
* Default multiple of the heartbeat interval used as the offline deadline.
39+
*/
40+
private static final int DEFAULT_EXPIRY_FACTOR = 3;
41+
42+
/**
43+
* Whether the registration lease is enabled. When false, no capability is
44+
* advertised and beats keep the legacy v1 semantics.
45+
*/
46+
private boolean enabled;
47+
48+
/**
49+
* Heartbeat interval issued to clients that do not propose one, and the fallback
50+
* for proposals outside the accepted bounds.
51+
*/
52+
private Duration interval = DEFAULT_INTERVAL;
53+
54+
/**
55+
* Smallest heartbeat interval the server is willing to negotiate.
56+
*/
57+
private Duration minInterval = Duration.ofSeconds(2);
58+
59+
/**
60+
* Largest heartbeat interval the server is willing to negotiate.
61+
*/
62+
private Duration maxInterval = Duration.ofSeconds(60);
63+
64+
/**
65+
* Multiple of the negotiated interval used as the offline deadline: an instance
66+
* whose lease is not renewed within {@code expiryFactor * interval} is marked
67+
* offline.
68+
*/
69+
private int expiryFactor = DEFAULT_EXPIRY_FACTOR;
70+
71+
/**
72+
* Whether the lease is enabled.
73+
*
74+
* @return whether enabled
75+
*/
76+
public boolean isEnabled() {
77+
return enabled;
78+
}
79+
80+
/**
81+
* Set whether the lease is enabled.
82+
*
83+
* @param enabled whether enabled
84+
*/
85+
public void setEnabled(final boolean enabled) {
86+
this.enabled = enabled;
87+
}
88+
89+
/**
90+
* Get the default heartbeat interval.
91+
*
92+
* @return the default interval
93+
*/
94+
public Duration getInterval() {
95+
return interval;
96+
}
97+
98+
/**
99+
* Set the default heartbeat interval.
100+
*
101+
* @param interval the default interval
102+
*/
103+
public void setInterval(final Duration interval) {
104+
this.interval = interval;
105+
}
106+
107+
/**
108+
* Get the smallest negotiable heartbeat interval.
109+
*
110+
* @return the minimum interval
111+
*/
112+
public Duration getMinInterval() {
113+
return minInterval;
114+
}
115+
116+
/**
117+
* Set the smallest negotiable heartbeat interval.
118+
*
119+
* @param minInterval the minimum interval
120+
*/
121+
public void setMinInterval(final Duration minInterval) {
122+
this.minInterval = minInterval;
123+
}
124+
125+
/**
126+
* Get the largest negotiable heartbeat interval.
127+
*
128+
* @return the maximum interval
129+
*/
130+
public Duration getMaxInterval() {
131+
return maxInterval;
132+
}
133+
134+
/**
135+
* Set the largest negotiable heartbeat interval.
136+
*
137+
* @param maxInterval the maximum interval
138+
*/
139+
public void setMaxInterval(final Duration maxInterval) {
140+
this.maxInterval = maxInterval;
141+
}
142+
143+
/**
144+
* Get the expiry factor applied to the negotiated interval.
145+
*
146+
* @return the expiry factor
147+
*/
148+
public int getExpiryFactor() {
149+
return expiryFactor;
150+
}
151+
152+
/**
153+
* Set the expiry factor applied to the negotiated interval.
154+
*
155+
* @param expiryFactor the expiry factor
156+
*/
157+
public void setExpiryFactor(final int expiryFactor) {
158+
this.expiryFactor = expiryFactor;
159+
}
160+
}

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/InstanceController.java‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import jakarta.validation.constraints.NotNull;
2222
import org.apache.shenyu.admin.aspect.annotation.RestApi;
2323
import org.apache.shenyu.register.common.dto.InstanceBeatInfoDTO;
24+
import org.apache.shenyu.register.common.protocol.RegistrationLeaseProtocol;
25+
import org.apache.shenyu.register.common.protocol.RegistrationProtocol;
2426
import org.apache.shenyu.admin.model.page.CommonPager;
2527
import org.apache.shenyu.admin.model.page.PageParameter;
2628
import org.apache.shenyu.admin.model.query.InstanceQuery;
@@ -39,8 +41,10 @@
3941
import org.springframework.web.bind.annotation.PathVariable;
4042
import org.springframework.web.bind.annotation.PostMapping;
4143
import org.springframework.web.bind.annotation.RequestBody;
44+
import org.springframework.web.bind.annotation.RequestHeader;
4245
import org.springframework.web.bind.annotation.RequestParam;
4346

47+
import java.util.Arrays;
4448
import java.util.Objects;
4549

4650
/**
@@ -143,17 +147,33 @@ public ShenyuAdminResult instanceDiagnostics(@PathVariable("id") final String id
143147
}
144148

145149
/**
146-
* receive beat.
150+
* receive beat. Legacy v1 callers get the exact legacy plain-text response; a
151+
* caller that advertises the {@code registration-lease-v1} capability receives a
152+
* structured lease response instead.
147153
*
154+
* @param capabilities the client capability header, may be absent
148155
* @param instanceBeatInfoDTO instanceBeatInfoDTO.
149-
* @return {@linkplain ShenyuAdminResult}
156+
* @return the legacy status string, or a structured lease response
150157
*/
151158
@PostMapping("/beat")
152-
public String beat(@Valid @RequestBody final InstanceBeatInfoDTO instanceBeatInfoDTO) {
159+
public Object beat(@RequestHeader(name = RegistrationProtocol.CAPABILITIES_HEADER, required = false)
160+
final String capabilities,
161+
@Valid @RequestBody final InstanceBeatInfoDTO instanceBeatInfoDTO) {
162+
if (presentsLeaseCapability(capabilities)) {
163+
return instanceCheckService.handleLeaseBeat(instanceBeatInfoDTO);
164+
}
153165
boolean accepted = instanceCheckService.handleBeatInfo(instanceBeatInfoDTO);
154166
return accepted ? ShenyuResultMessage.SUCCESS : ShenyuResultMessage.PARAMETER_ERROR;
155167
}
156168

169+
private boolean presentsLeaseCapability(final String capabilities) {
170+
if (Objects.isNull(capabilities) || capabilities.isBlank()) {
171+
return false;
172+
}
173+
return Arrays.stream(capabilities.split(","))
174+
.anyMatch(token -> RegistrationLeaseProtocol.CAPABILITY.equals(token.trim()));
175+
}
176+
157177
/**
158178
* visual instance info.
159179
*

0 commit comments

Comments
 (0)