Skip to content

Commit e2f3c40

Browse files
authored
[#690]fix issues when service is deleted and registered again (#692)
1 parent f50b966 commit e2f3c40

File tree

5 files changed

+152
-153
lines changed

5 files changed

+152
-153
lines changed

spring-cloud-huawei-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/ribbon/ServiceCombIPing.java

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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 com.huaweicloud.servicecomb.discovery.ribbon;
19+
20+
import java.util.List;
21+
22+
import com.netflix.loadbalancer.AbstractServerList;
23+
import com.netflix.loadbalancer.ILoadBalancer;
24+
import com.netflix.loadbalancer.IRule;
25+
import com.netflix.loadbalancer.Server;
26+
27+
public class ServiceCombLoadBalancer implements ILoadBalancer {
28+
private final IRule rule;
29+
30+
private final AbstractServerList<Server> serverList;
31+
32+
public ServiceCombLoadBalancer(IRule rule,
33+
AbstractServerList<Server> serverList) {
34+
this.rule = rule;
35+
this.rule.setLoadBalancer(this);
36+
this.serverList = serverList;
37+
}
38+
39+
@Override
40+
public void addServers(List<Server> newServers) {
41+
// do nothing
42+
}
43+
44+
@Override
45+
public Server chooseServer(Object key) {
46+
return this.rule.choose(key);
47+
}
48+
49+
@Override
50+
public void markServerDown(Server server) {
51+
// do nothing
52+
}
53+
54+
@Override
55+
@SuppressWarnings("deprecation")
56+
public List<Server> getServerList(boolean availableOnly) {
57+
return serverList.getUpdatedListOfServers();
58+
}
59+
60+
@Override
61+
public List<Server> getReachableServers() {
62+
return serverList.getUpdatedListOfServers();
63+
}
64+
65+
@Override
66+
public List<Server> getAllServers() {
67+
return serverList.getUpdatedListOfServers();
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,55 @@
11
/*
22
3-
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4-
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* 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-
*/
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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+
*/
1717

1818
package com.huaweicloud.servicecomb.discovery.ribbon;
1919

2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
21-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2221
import org.springframework.cloud.client.discovery.DiscoveryClient;
2322
import org.springframework.context.annotation.Bean;
2423
import org.springframework.context.annotation.Configuration;
2524

2625
import com.huaweicloud.servicecomb.discovery.registry.ServiceCombRegistration;
2726
import com.netflix.client.config.IClientConfig;
28-
import com.netflix.loadbalancer.IPing;
29-
import com.netflix.loadbalancer.ServerList;
30-
import com.netflix.loadbalancer.ServerListUpdater;
31-
32-
/**
33-
* @Author wangqijun
34-
* @Date 17:15 2019-07-11
35-
**/
27+
import com.netflix.loadbalancer.AbstractServerList;
28+
import com.netflix.loadbalancer.ILoadBalancer;
29+
import com.netflix.loadbalancer.IRule;
30+
import com.netflix.loadbalancer.Server;
31+
3632
@Configuration
3733
public class ServiceCombRibbonClientConfiguration {
3834
@Bean
3935
@ConditionalOnMissingBean
40-
public ServerList<?> ribbonServerList(IClientConfig config, DiscoveryClient discoveryClient,
36+
public ILoadBalancer ribbonLoadBalancer(
37+
IRule rule, AbstractServerList<Server> serverList) {
38+
return new ServiceCombLoadBalancer(rule, serverList);
39+
}
40+
41+
@Bean
42+
@ConditionalOnMissingBean
43+
public AbstractServerList<Server> ribbonServerList(IClientConfig config, DiscoveryClient discoveryClient,
4144
ServiceCombRegistration serviceCombRegistration) {
4245
ServiceCombServerList serverList = new ServiceCombServerList(discoveryClient, serviceCombRegistration);
4346
serverList.initWithNiwsConfig(config);
4447
return serverList;
4548
}
4649

4750
@Bean
48-
@ConditionalOnProperty(value = "spring.cloud.servicecomb.discovery.ping")
49-
public IPing ping() {
50-
return new ServiceCombIPing();
51-
}
52-
53-
@Bean
54-
public ServerListUpdater serviceCombServerListUpdater() {
55-
return new ServiceCombServerListUpdater();
51+
@ConditionalOnMissingBean
52+
public IRule ribbonRule() {
53+
return new ServiceCombRoundRobinRule();
5654
}
5755
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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 com.huaweicloud.servicecomb.discovery.ribbon;
19+
20+
import java.util.List;
21+
import java.util.concurrent.atomic.AtomicInteger;
22+
23+
import org.springframework.util.CollectionUtils;
24+
25+
import com.netflix.loadbalancer.ILoadBalancer;
26+
import com.netflix.loadbalancer.IRule;
27+
import com.netflix.loadbalancer.Server;
28+
29+
public class ServiceCombRoundRobinRule implements IRule {
30+
private final AtomicInteger counter = new AtomicInteger(0);
31+
32+
private ILoadBalancer loadBalancer;
33+
34+
@Override
35+
public Server choose(Object key) {
36+
List<Server> servers = loadBalancer.getReachableServers();
37+
if (CollectionUtils.isEmpty(servers)) {
38+
return null;
39+
}
40+
int index = Math.abs(counter.getAndIncrement()) % servers.size();
41+
return servers.get(index);
42+
}
43+
44+
@Override
45+
public void setLoadBalancer(ILoadBalancer lb) {
46+
this.loadBalancer = lb;
47+
}
48+
49+
@Override
50+
public ILoadBalancer getLoadBalancer() {
51+
return this.loadBalancer;
52+
}
53+
}

spring-cloud-huawei-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/ribbon/ServiceCombServerListUpdater.java

-80
This file was deleted.

0 commit comments

Comments
 (0)