Skip to content

Commit d7f4dc1

Browse files
authored
Add connection num feature (#1038)
1 parent faa68b0 commit d7f4dc1

File tree

9 files changed

+141
-0
lines changed

9 files changed

+141
-0
lines changed

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ConsumerConfigHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public ConsumerConfig getConsumerConfig(Contract contract, RpcBinding binding) {
7777
Object callbackHandler = param.getCallbackHandler();
7878
String genericInterface = param.getGenericInterface();
7979
String loadBalancer = param.getLoadBalancer();
80+
Integer connectionNum = param.getConnectionNum();
8081
Boolean lazy = param.getLazy();
8182
Boolean check = param.getCheck();
8283
String mockMode = param.getMockMode();
@@ -119,6 +120,9 @@ public ConsumerConfig getConsumerConfig(Contract contract, RpcBinding binding) {
119120
if (StringUtils.hasText(loadBalancer)) {
120121
consumerConfig.setLoadBalancer(loadBalancer);
121122
}
123+
if (connectionNum != null) {
124+
consumerConfig.setConnectionNum(connectionNum);
125+
}
122126
if (lazy != null) {
123127
consumerConfig.setLazy(lazy);
124128
}

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/binding/RpcBindingXmlConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class RpcBindingXmlConstants {
3535
public static final String TAG_TIMEOUT = "timeout";
3636
public static final String TAG_ADDRESS_WAIT_TIME = "address-wait-time";
3737
public static final String TAG_CONNECT_TIMEOUT = "connect.timeout";
38+
public static final String TAG_CONNECT_NUM = "connect.num";
3839
public static final String TAG_RETRIES = "retries";
3940
public static final String TAG_TYPE = "type";
4041
public static final String TAG_CALLBACK_CLASS = "callback-class";

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/converter/RpcBindingConverter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ protected void parseGlobalAttrs(Element element, RpcBindingParam param,
207207
.getAttribute(RpcBindingXmlConstants.TAG_ADDRESS_WAIT_TIME));
208208
Integer connectTimeout = SofaBootRpcParserUtil.parseInteger(element
209209
.getAttribute(RpcBindingXmlConstants.TAG_CONNECT_TIMEOUT));
210+
Integer connectionNum = SofaBootRpcParserUtil.parseInteger(element
211+
.getAttribute(RpcBindingXmlConstants.TAG_CONNECT_NUM));
210212
Integer retries = SofaBootRpcParserUtil.parseInteger(element
211213
.getAttribute(RpcBindingXmlConstants.TAG_RETRIES));
212214
String type = element.getAttribute(RpcBindingXmlConstants.TAG_TYPE);
@@ -273,6 +275,10 @@ protected void parseGlobalAttrs(Element element, RpcBindingParam param,
273275
if (StringUtils.hasText(loadBalancer)) {
274276
param.setLoadBalancer(loadBalancer);
275277
}
278+
279+
if (connectionNum != null) {
280+
param.setConnectionNum(connectionNum);
281+
}
276282
if (lazy != null) {
277283
param.setLazy(lazy);
278284
}
@@ -549,6 +555,7 @@ protected void convertReferenceAnnotation(RpcBindingParam bindingParam,
549555
if (StringUtils.hasText(callbackRef)) {
550556
bindingParam.setCallbackHandler(applicationContext.getBean(callbackRef));
551557
}
558+
bindingParam.setConnectionNum(sofaReferenceBindingAnnotation.connectionNum());
552559
bindingParam.setLazy(sofaReferenceBindingAnnotation.lazy());
553560

554561
String registryAlias = sofaReferenceBindingAnnotation.registry();

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/runtime/param/RpcBindingParam.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public abstract class RpcBindingParam implements BindingParam {
4343

4444
protected Integer connectTimeout;
4545

46+
protected Integer connectionNum;
47+
4648
protected Integer retries;
4749

4850
protected String type;
@@ -144,6 +146,24 @@ public void setConnectTimeout(Integer connectTimeout) {
144146
this.connectTimeout = connectTimeout;
145147
}
146148

149+
/**
150+
* Getter method for property <code>connectionNum</code>.
151+
*
152+
* @return property value of connectionNum
153+
*/
154+
public Integer getConnectionNum() {
155+
return connectionNum;
156+
}
157+
158+
/**
159+
* Setter method for property <code>connectionNum</code>.
160+
*
161+
* @param connectionNum value to be assigned to property connectionNum
162+
*/
163+
public void setConnectionNum(Integer connectionNum) {
164+
this.connectionNum = connectionNum;
165+
}
166+
147167
/**
148168
* Getter method for property <code>retries</code>.
149169
*
@@ -488,6 +508,10 @@ public boolean equals(Object o) {
488508
: that.connectTimeout != null) {
489509
return false;
490510
}
511+
if (connectionNum != null ? !connectionNum.equals(that.connectionNum)
512+
: that.connectionNum != null) {
513+
return false;
514+
}
491515
if (retries != null ? !retries.equals(that.retries) : that.retries != null) {
492516
return false;
493517
}
@@ -557,6 +581,7 @@ public int hashCode() {
557581
int result = timeout != null ? timeout.hashCode() : 0;
558582
result = 31 * result + (addressWaitTime != null ? addressWaitTime.hashCode() : 0);
559583
result = 31 * result + (connectTimeout != null ? connectTimeout.hashCode() : 0);
584+
result = 31 * result + (connectionNum != null ? connectionNum.hashCode() : 0);
560585
result = 31 * result + (retries != null ? retries.hashCode() : 0);
561586
result = 31 * result + (type != null ? type.hashCode() : 0);
562587
result = 31 * result + (callbackClass != null ? callbackClass.hashCode() : 0);

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/java/com/alipay/sofa/rpc/boot/test/SofaBootRpcAllTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.alipay.sofa.rpc.boot.container.ConsumerConfigContainer;
2323
import com.alipay.sofa.rpc.boot.runtime.param.RestBindingParam;
2424
import com.alipay.sofa.rpc.boot.test.bean.annotation.AnnotationService;
25+
import com.alipay.sofa.rpc.boot.test.bean.connectionnum.ConnectionNumService;
2526
import com.alipay.sofa.rpc.boot.test.bean.direct.DirectService;
2627
import com.alipay.sofa.rpc.boot.test.bean.dubbo.DubboService;
2728
import com.alipay.sofa.rpc.boot.test.bean.filter.FilterService;
@@ -124,6 +125,9 @@ public class SofaBootRpcAllTest {
124125
@Autowired
125126
private LazyService lazyServiceDubbo;
126127

128+
@Autowired
129+
private ConnectionNumService connectionNumService;
130+
127131
@Autowired
128132
@Qualifier("sofaGreeterTripleRef")
129133
private SofaGreeterTriple.IGreeter sofaGreeterTripleRef;
@@ -143,6 +147,9 @@ public class SofaBootRpcAllTest {
143147
@SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", timeout = 1000), jvmFirst = false, uniqueId = "timeout")
144148
private AnnotationService annotationConsumerTimeoutService;
145149

150+
@SofaReference(binding = @SofaReferenceBinding(bindingType = "rest", connectionNum = 100), jvmFirst = false, uniqueId = "connectionNum")
151+
private AnnotationService annotationConsumerConnectionNumService;
152+
146153
@SofaClientFactory
147154
private ClientFactory clientFactory;
148155

@@ -291,6 +298,31 @@ public void testLoadBalancerAnnotation() throws NoSuchFieldException, IllegalAcc
291298
Assert.assertTrue("Found roundrobin reference", found);
292299
}
293300

301+
@Test
302+
public void testConnectionNum() throws NoSuchFieldException, IllegalAccessException {
303+
Field consumerConfigMapField = ConsumerConfigContainer.class
304+
.getDeclaredField("consumerConfigMap");
305+
consumerConfigMapField.setAccessible(true);
306+
ConcurrentMap<Binding, ConsumerConfig> consumerConfigMap = (ConcurrentMap<Binding, ConsumerConfig>) consumerConfigMapField
307+
.get(consumerConfigContainer);
308+
309+
boolean found1 = false;
310+
boolean found2 = false;
311+
for (ConsumerConfig consumerConfig : consumerConfigMap.values()) {
312+
if ("connectionNum".equals(consumerConfig.getUniqueId())
313+
&& AnnotationService.class.getName().equals(consumerConfig.getInterfaceId())) {
314+
found1 = true;
315+
Assert.assertEquals(100, consumerConfig.getConnectionNum());
316+
} else if (connectionNumService.getClass().getName()
317+
.startsWith(consumerConfig.getInterfaceId())) {
318+
found2 = true;
319+
Assert.assertEquals(300, consumerConfig.getConnectionNum());
320+
}
321+
}
322+
Assert.assertTrue("Found annotation reference", found1);
323+
Assert.assertTrue("Found xml reference", found2);
324+
}
325+
294326
@Test
295327
public void testRestSwagger() throws IOException {
296328
HttpClient httpClient = HttpClientBuilder.create().build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
package com.alipay.sofa.rpc.boot.test.bean.connectionnum;
18+
19+
import javax.ws.rs.*;
20+
21+
@Path("/webapi")
22+
@Consumes("application/json;charset=UTF-8")
23+
@Produces("application/json;charset=UTF-8")
24+
public interface ConnectionNumService {
25+
@GET
26+
@Path("/connectionNumService/{num}")
27+
String sayConnectionNum(@PathParam("num") String string);
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
package com.alipay.sofa.rpc.boot.test.bean.connectionnum;
18+
19+
public class ConnectionNumServiceImpl implements ConnectionNumService {
20+
@Override
21+
public String sayConnectionNum(String string) {
22+
return string;
23+
}
24+
}

sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/test/resources/spring/test_all.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@
162162
</sofa:binding.dubbo>
163163
</sofa:reference>
164164

165+
<!-- connectionNum -->
166+
<bean id="connectionNumServiceImpl" class="com.alipay.sofa.rpc.boot.test.bean.connectionnum.ConnectionNumServiceImpl"/>
167+
<sofa:service ref="connectionNumServiceImpl" interface="com.alipay.sofa.rpc.boot.test.bean.connectionnum.ConnectionNumService">
168+
<sofa:binding.rest/>
169+
</sofa:service>
170+
171+
<sofa:reference jvm-first="false" id="connectionNumService" interface="com.alipay.sofa.rpc.boot.test.bean.connectionnum.ConnectionNumService">
172+
<sofa:binding.rest>
173+
<sofa:global-attrs connect.num="300"/>
174+
</sofa:binding.rest>
175+
</sofa:reference>
176+
165177

166178
<!-- invoke sync-->
167179
<bean id="sofaGreeterTripleImpl" class="com.alipay.sofa.rpc.boot.test.bean.triple.TripleGreeterImpl"/>

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/api/annotation/SofaReferenceBinding.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
*/
101101
String registry() default "";
102102

103+
/**
104+
* the number of long connections per ref
105+
*
106+
* @return
107+
*/
108+
int connectionNum() default 1;
109+
103110
/**
104111
* delay init connection
105112
*

0 commit comments

Comments
 (0)