mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Fixes #13
This commit is contained in:
parent
a387ce1554
commit
2466cbddb8
@ -17,7 +17,6 @@
|
|||||||
package org.springframework.cloud.alibaba.nacos.ribbon;
|
package org.springframework.cloud.alibaba.nacos.ribbon;
|
||||||
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
import com.netflix.client.config.IClientConfig;
|
||||||
import com.netflix.loadbalancer.Server;
|
|
||||||
import com.netflix.loadbalancer.ServerList;
|
import com.netflix.loadbalancer.ServerList;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -33,7 +32,7 @@ public class NacosRibbonClientConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public ServerList<Server> ribbonServerList(IClientConfig config) {
|
public ServerList<?> ribbonServerList(IClientConfig config) {
|
||||||
NacosServerList serverList = new NacosServerList(config.getClientName());
|
NacosServerList serverList = new NacosServerList(config.getClientName());
|
||||||
return serverList;
|
return serverList;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.cloud.alibaba.nacos.ribbon;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
|
|
||||||
|
import com.netflix.loadbalancer.Server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiaojing
|
||||||
|
*/
|
||||||
|
public class NacosServer extends Server {
|
||||||
|
|
||||||
|
private final MetaInfo metaInfo;
|
||||||
|
private final Instance instance;
|
||||||
|
private final Map<String, String> metadata;
|
||||||
|
|
||||||
|
public NacosServer(Instance instance) {
|
||||||
|
super(instance.getIp(), instance.getPort());
|
||||||
|
this.instance = instance;
|
||||||
|
this.metaInfo = new MetaInfo() {
|
||||||
|
@Override
|
||||||
|
public String getAppName() {
|
||||||
|
return instance.getService().getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getServerGroup() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getServiceIdForDiscovery() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInstanceId() {
|
||||||
|
return instance.getInstanceId();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.metadata = instance.getMetadata();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MetaInfo getMetaInfo() {
|
||||||
|
return metaInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Instance getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getMetadata() {
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
}
|
@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos.ribbon;
|
package org.springframework.cloud.alibaba.nacos.ribbon;
|
||||||
|
|
||||||
import com.netflix.loadbalancer.Server;
|
import com.netflix.client.config.IClientConfig;
|
||||||
import com.netflix.loadbalancer.ServerList;
|
import com.netflix.loadbalancer.AbstractServerList;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
|
import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
|
||||||
|
|
||||||
@ -29,58 +29,51 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
|
|||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
public class NacosServerList implements ServerList<Server> {
|
public class NacosServerList extends AbstractServerList<NacosServer> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private NacosRegistration registration;
|
private NacosRegistration registration;
|
||||||
|
|
||||||
private String service;
|
private String serviceId;
|
||||||
|
|
||||||
public NacosServerList() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public NacosServerList(String service) {
|
public NacosServerList(String serviceId) {
|
||||||
this.service = service;
|
this.serviceId = serviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Server> getInitialListOfServers() {
|
public List<NacosServer> getInitialListOfServers() {
|
||||||
try {
|
try {
|
||||||
List<Instance> instances = registration.getNacosNamingService().selectInstances(service,true);
|
List<Instance> instances = registration.getNacosNamingService().selectInstances(serviceId,true);
|
||||||
return hostsToServerList(instances);
|
return instancesToServerList(instances);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new IllegalStateException("Can not get nacos hosts, service=" + service, e);
|
throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + serviceId, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Server> getUpdatedListOfServers() {
|
public List<NacosServer> getUpdatedListOfServers() {
|
||||||
return getInitialListOfServers();
|
return getInitialListOfServers();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Server hostToServer(Instance instance) {
|
private List<NacosServer> instancesToServerList(List<Instance> instances) {
|
||||||
Server server = new Server(instance.getIp(), instance.getPort());
|
List<NacosServer> result = new ArrayList<>(instances.size());
|
||||||
return server;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Server> hostsToServerList(List<Instance> instances) {
|
|
||||||
List<Server> result = new ArrayList<Server>(instances.size());
|
|
||||||
for (Instance instance : instances) {
|
for (Instance instance : instances) {
|
||||||
if (instance.isHealthy()) {
|
if (instance.isHealthy()) {
|
||||||
result.add(hostToServer(instance));
|
result.add(new NacosServer(instance));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getService() {
|
public String getServiceId() {
|
||||||
return service;
|
return serviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setService(String service) {
|
@Override
|
||||||
this.service = service;
|
public void initWithNiwsConfig(IClientConfig iClientConfig) {
|
||||||
|
this.serviceId = iClientConfig.getClientName();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by yizhan on 2018/9/14.
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
public class NacosRibbonClientConfigurationTests {
|
public class NacosRibbonClientConfigurationTests {
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ public class NacosRibbonClientConfigurationTests {
|
|||||||
|
|
||||||
this.contextRunner.run(context -> {
|
this.contextRunner.run(context -> {
|
||||||
NacosServerList serverList = context.getBean(NacosServerList.class);
|
NacosServerList serverList = context.getBean(NacosServerList.class);
|
||||||
assertThat(serverList.getService()).isEqualTo("myapp");
|
assertThat(serverList.getServiceId()).isEqualTo("myapp");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user