-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from microsphere-projects/main
Merge from main branch
- Loading branch information
Showing
16 changed files
with
605 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...rc/main/java/io/microsphere/nacos/client/discovery/spring/cloud/NacosDiscoveryClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 io.microsphere.nacos.client.discovery.spring.cloud; | ||
|
||
import io.microsphere.nacos.client.common.discovery.InstanceClient; | ||
import io.microsphere.nacos.client.common.discovery.ServiceClient; | ||
import io.microsphere.nacos.client.common.discovery.model.Instance; | ||
import io.microsphere.nacos.client.common.discovery.model.InstancesList; | ||
import io.microsphere.nacos.client.common.model.Page; | ||
import io.microsphere.nacos.client.spring.boot.NacosClientProperties; | ||
import io.microsphere.nacos.client.transport.OpenApiHttpClient; | ||
import io.microsphere.nacos.client.v1.discovery.OpenApiInstanceClient; | ||
import io.microsphere.nacos.client.v1.discovery.OpenApiServiceClient; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.discovery.DiscoveryClient; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* The {@link DiscoveryClient} class for Nacos Discovery | ||
* | ||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> | ||
* @see InstanceClient | ||
* @see ServiceClient | ||
* @since 1.0.0 | ||
*/ | ||
public class NacosDiscoveryClient implements DiscoveryClient { | ||
|
||
private final ServiceClient serviceClient; | ||
|
||
private final InstanceClient instanceClient; | ||
|
||
private final NacosClientProperties nacosClientProperties; | ||
|
||
private final String namespaceId; | ||
|
||
public NacosDiscoveryClient(OpenApiHttpClient openApiHttpClient, NacosClientProperties nacosClientProperties) { | ||
this.serviceClient = new OpenApiServiceClient(openApiHttpClient, nacosClientProperties); | ||
this.instanceClient = new OpenApiInstanceClient(openApiHttpClient, nacosClientProperties); | ||
this.nacosClientProperties = nacosClientProperties; | ||
this.namespaceId = nacosClientProperties.getDiscovery().getNamespaceId(); | ||
} | ||
|
||
@Override | ||
public String description() { | ||
return "DiscoveryClient - Nacos"; | ||
} | ||
|
||
@Override | ||
public List<ServiceInstance> getInstances(String serviceId) { | ||
String serviceName = serviceId; | ||
InstancesList instancesList = instanceClient.getInstancesList(namespaceId, serviceName); | ||
List<Instance> instances = instancesList.getHosts(); | ||
return instances.stream().map(NacosServiceInstance::new).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public List<String> getServices() { | ||
List<String> allServiceNames = new LinkedList<>(); | ||
|
||
int pageNumber = 0; | ||
Page<String> serviceNames = serviceClient.getServiceNames(namespaceId, pageNumber); | ||
|
||
allServiceNames.addAll(serviceNames.getElements()); | ||
|
||
while (serviceNames.hasNext()) { | ||
pageNumber = serviceNames.getPageSize(); | ||
serviceNames = serviceClient.getServiceNames(namespaceId, pageNumber); | ||
allServiceNames.addAll(serviceNames.getElements()); | ||
} | ||
|
||
return allServiceNames; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
.../java/io/microsphere/nacos/client/discovery/spring/cloud/NacosDiscoveryConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 io.microsphere.nacos.client.discovery.spring.cloud; | ||
|
||
import io.microsphere.nacos.client.spring.NacosClientConfiguration; | ||
import io.microsphere.nacos.client.spring.boot.NacosClientProperties; | ||
import io.microsphere.nacos.client.transport.OpenApiHttpClient; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
/** | ||
* The default {@link Configuration Configuration} class for Nacos Discovery | ||
* | ||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> | ||
* @since Configuration | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@EnableConfigurationProperties(NacosClientProperties.class) | ||
@Import(NacosClientConfiguration.class) | ||
public class NacosDiscoveryConfiguration { | ||
|
||
@Bean | ||
public NacosDiscoveryClient nacosDiscoveryClient(OpenApiHttpClient openApiHttpClient, | ||
NacosClientProperties nacosClientProperties) { | ||
return new NacosDiscoveryClient(openApiHttpClient, nacosClientProperties); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...java/io/microsphere/nacos/client/discovery/spring/cloud/NacosDiscoveryContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 io.microsphere.nacos.client.discovery.spring.cloud; | ||
|
||
import org.springframework.cloud.context.named.NamedContextFactory; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.PropertySource; | ||
|
||
import static io.microsphere.nacos.client.spring.boot.NacosClientProperties.PREFIX; | ||
import static io.microsphere.nacos.client.spring.util.NacosClientUtils.getNacosCilentPropertySource; | ||
|
||
/** | ||
* {@link NamedContextFactory} Class for Nacos Discovery | ||
* | ||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> | ||
* @since NamedContextFactory | ||
*/ | ||
public class NacosDiscoveryContextFactory extends NamedContextFactory<NacosDiscoverySpecification> { | ||
|
||
private final ConfigurableApplicationContext parentContext; | ||
|
||
private static final String NACOS_DISCOVERY_PROPERTY_NAME = "nacos-discovery"; | ||
|
||
public NacosDiscoveryContextFactory(ConfigurableApplicationContext parentContext) { | ||
super(NacosDiscoveryConfiguration.class, NACOS_DISCOVERY_PROPERTY_NAME, PREFIX + "name"); | ||
setApplicationContext(parentContext); | ||
this.parentContext = parentContext; | ||
} | ||
|
||
@Override | ||
public GenericApplicationContext buildContext(String name) { | ||
GenericApplicationContext context = super.buildContext(name); | ||
PropertySource nacosClientPropertySource = getNacosCilentPropertySource(this.parentContext.getEnvironment(), name); | ||
ConfigurableEnvironment environment = context.getEnvironment(); | ||
environment.getPropertySources().addAfter(NACOS_DISCOVERY_PROPERTY_NAME, nacosClientPropertySource); | ||
return context; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
.../java/io/microsphere/nacos/client/discovery/spring/cloud/NacosDiscoverySpecification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 io.microsphere.nacos.client.discovery.spring.cloud; | ||
|
||
import org.springframework.cloud.context.named.NamedContextFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
/** | ||
* {@link NamedContextFactory.Specification} Class for Nacos Discovery | ||
* | ||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> | ||
* @since NamedContextFactory.Specification | ||
*/ | ||
public class NacosDiscoverySpecification implements NamedContextFactory.Specification { | ||
|
||
private String name; | ||
|
||
private Class<?>[] configuration; | ||
|
||
NacosDiscoverySpecification() { | ||
} | ||
|
||
public NacosDiscoverySpecification(String name, Class<?>... configuration) { | ||
this.name = name; | ||
this.configuration = configuration; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Class<?>[] getConfiguration() { | ||
return this.configuration; | ||
} | ||
|
||
public void setConfiguration(Class<?>[] configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
NacosDiscoverySpecification that = (NacosDiscoverySpecification) o; | ||
return Objects.equals(this.name, that.name) && Arrays.equals(this.configuration, that.configuration); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.name, this.configuration); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("NacosDiscoverySpecification{").append("name='").append(this.name).append("', ") | ||
.append("configuration=").append(Arrays.toString(this.configuration)).append("}").toString(); | ||
} | ||
} |
Oops, something went wrong.