Skip to content

Commit

Permalink
Merge pull request #19 from microsphere-projects/main
Browse files Browse the repository at this point in the history
Merge from main branch
  • Loading branch information
mercyblitz authored Sep 20, 2024
2 parents 99d250e + 4051ce9 commit 5fba792
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 18 deletions.
18 changes: 0 additions & 18 deletions .circleci/config.yml

This file was deleted.

4 changes: 4 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ repositories {
maven {
url = uri("https://maven.aliyun.com/repository/public")
}
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots")
}
maven {
url = uri("https://maven.aliyun.com/repository/gradle-plugin")
}
maven {
url = uri("https://maven.aliyun.com/repository/spring-plugin")
}
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ repositories {
maven {
url = uri("https://maven.aliyun.com/repository/public")
}
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots")
}
maven {
url = uri("https://maven.aliyun.com/repository/gradle-plugin")
}
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ logback-version = "1.3.14"

spring-boot-version = "3.3.2"
spring-cloud-verson = "2023.0.3"
microsphere-spring-boot-dependencies-version = "2.0.0-SNAPSHOT"

[libraries]
apache-http-client = { group = "org.apache.httpcomponents", name = "httpclient", version.ref = "apache-http-client-version" }
Expand All @@ -20,5 +21,6 @@ logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.

spring-boot-dependencies = { group = "org.springframework.boot", name = "spring-boot-dependencies", version.ref = "spring-boot-version" }
spring-cloud-dependencies = { group = "org.springframework.cloud", name = "spring-cloud-dependencies", version.ref = "spring-cloud-verson" }
microsphere-spring-boot-dependencies = { group = "io.github.microsphere-projects", name = "microsphere-spring-boot-dependencies", version.ref = "microsphere-spring-boot-dependencies-version" }

[plugins]
3 changes: 3 additions & 0 deletions microsphere-nacos-discovery-spring-cloud/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ dependencies {
// BOM
implementation(platform(libs.spring.boot.dependencies))
implementation(platform(libs.spring.cloud.dependencies))
implementation(platform(libs.microsphere.spring.boot.dependencies))

// Internal
api(project(":microsphere-nacos-openapi"))

// Third-Party
implementation("io.github.microsphere-projects:microsphere-core-spring-boot-starter")

// Spring Boot
compileOnly("org.springframework.boot:spring-boot-starter-web")
compileOnly("org.springframework.boot:spring-boot-configuration-processor")

// Spring Cloud Commons
compileOnly("org.springframework.cloud:spring-cloud-commons")
compileOnly("org.springframework.cloud:spring-cloud-context")

// Testing
testImplementation(libs.junit.jupiter.engine)
Expand Down
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;
}
}
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);
}
}
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;
}
}
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();
}
}
Loading

0 comments on commit 5fba792

Please sign in to comment.