Skip to content

Commit

Permalink
Merge branch 'alibaba:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhouyu authored Dec 7, 2024
2 parents 01a3890 + cb3685d commit a5236cc
Show file tree
Hide file tree
Showing 62 changed files with 2,612 additions and 799 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

Nacos (official site: [nacos.io](https://nacos.io)) is an easy-to-use platform designed for dynamic service discovery and configuration and service management. It helps you to build cloud native applications and microservices platform easily.

Service is a first-class citizen in Nacos. Nacos supports almost all type of services,for example,[Dubbo/gRPC service](https://nacos.io/en-us/docs/use-nacos-with-dubbo.html), [Spring Cloud RESTFul service](https://nacos.io/en-us/docs/use-nacos-with-springcloud.html) or [Kubernetes service](https://nacos.io/en-us/docs/use-nacos-with-kubernetes.html).
Service is a first-class citizen in Nacos. Nacos supports almost all type of services,for example,[Dubbo/gRPC service](https://nacos.io/docs/latest/ecology/use-nacos-with-dubbo/), [Spring Cloud RESTFul service](https://nacos.io/docs/latest/ecology/use-nacos-with-spring-cloud/) or [Kubernetes service](https://nacos.io/docs/latest/quickstart/quick-start-kubernetes/).

Nacos provides four major functions.

Expand Down Expand Up @@ -68,23 +68,23 @@ On the **Windows** platform, run the following command to start server with stan
startup.cmd -m standalone
```

For more details, see [quick-start.](https://nacos.io/en-us/docs/quick-start.html)
For more details, see [quick-start.](https://nacos.io/docs/latest/quickstart/quick-start/)

## Quick start for other open-source projects:
* [Quick start with Nacos command and console](https://nacos.io/en-us/docs/quick-start.html)
* [Quick start with Nacos command and console](https://nacos.io/docs/latest/quickstart/quick-start/)

* [Quick start with dubbo](https://nacos.io/en-us/docs/use-nacos-with-dubbo.html)
* [Quick start with dubbo](https://nacos.io/docs/latest/ecology/use-nacos-with-dubbo/)

* [Quick start with spring cloud](https://nacos.io/en-us/docs/quick-start-spring-cloud.html)
* [Quick start with spring cloud](https://nacos.io/docs/latest/ecology/use-nacos-with-spring-cloud/)

* [Quick start with kubernetes](https://nacos.io/en-us/docs/use-nacos-with-kubernetes.html)
* [Quick start with kubernetes](https://nacos.io/docs/latest/quickstart/quick-start-kubernetes/)


## Documentation

You can view the full documentation from the [Nacos website](https://nacos.io/en-us/docs/v2/what-is-nacos.html).
You can view the full documentation from the [Nacos website](https://nacos.io/docs/latest/overview/).

You can also read this online eBook from the [NACOS ARCHITECTURE & PRINCIPLES](https://www.yuque.com/nacos/ebook/kbyo6n).
You can also read this online eBook from the [NACOS ARCHITECTURE & PRINCIPLES](https://nacos.io/docs/ebook/kbyo6n/).

All the latest and long-term notice can also be found here from [GitHub notice issue](https://github.com/alibaba/nacos/labels/notice).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public enum AuthErrorCode {
* invalid auth type.
*/
INVALID_TYPE(50001,
"Invalid auth type, Please set `nacos.core.auth.system.type`, detail: https://nacos.io/zh-cn/docs/v2/plugin/auth-plugin.html"),
"Invalid auth type, Please set `nacos.core.auth.system.type`, detail: https://nacos.io/docs/latest/manual/admin/auth/"),

EMPTY_IDENTITY(50002,
"Empty identity, Please set `nacos.core.auth.server.identity.key` and `nacos.core.auth.server.identity.value`, detail: https://nacos.io/zh-cn/docs/v2/guide/user/auth.html");
"Empty identity, Please set `nacos.core.auth.server.identity.key` and `nacos.core.auth.server.identity.value`, detail: https://nacos.io/docs/latest/manual/admin/auth/");

private final Integer code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ConfigTransportClient(NacosClientProperties properties, ConfigServerListM
this.tenant = properties.getProperty(PropertyKeyConst.NAMESPACE);
this.serverListManager = serverListManager;
this.properties = properties.asProperties();
this.securityProxy = new SecurityProxy(serverListManager.getServerList(),
this.securityProxy = new SecurityProxy(serverListManager,
ConfigHttpClientManager.getInstance().getNacosRestTemplate());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void init(Properties properties) throws NacosException {
InitUtils.initWebRootContext(nacosClientProperties);
serverListManager = new NamingServerListManager(nacosClientProperties, namespace);
serverListManager.start();
securityProxy = new SecurityProxy(serverListManager.getServerList(),
securityProxy = new SecurityProxy(serverListManager,
NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy(properties);
serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, nacosClientProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.selector.NamingContext;
import com.alibaba.nacos.api.naming.selector.NamingResult;
import com.alibaba.nacos.api.naming.selector.NamingSelector;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.api.selector.AbstractSelector;
Expand All @@ -37,6 +39,7 @@
import com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate;
import com.alibaba.nacos.client.naming.selector.NamingSelectorFactory;
import com.alibaba.nacos.client.naming.selector.NamingSelectorWrapper;
import com.alibaba.nacos.client.naming.selector.ServiceInfoContext;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
Expand Down Expand Up @@ -324,38 +327,67 @@ private List<Instance> selectInstances(ServiceInfo serviceInfo, boolean healthy)
return list;
}

private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, String clusterString) {
return serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString);
private ServiceInfo getServiceInfo(String serviceName, String groupName, List<String> clusters, boolean subscribe)
throws NacosException {
ServiceInfo serviceInfo;
NamingSelector clusterSelector = NamingSelectorFactory.newClusterSelector(clusters);
if (serviceInfoHolder.isFailoverSwitch()) {
serviceInfo = getServiceInfoByFailover(serviceName, groupName, clusterSelector);
if (serviceInfo != null && !serviceInfo.getHosts().isEmpty()) {
NAMING_LOGGER.debug("getServiceInfo from failover,serviceName: {} data:{}", serviceName,
JacksonUtils.toJson(serviceInfo.getHosts()));
return serviceInfo;
}
}
serviceInfo = getServiceInfoBySubscribe(serviceName, groupName, clusters, clusterSelector, subscribe);
return serviceInfo;
}

private ServiceInfo getServiceInfoBySubscribe(String serviceName, String groupName, String clusterString,
boolean subscribe) throws NacosException {
private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, NamingSelector clusterSelector) {
ServiceInfo result = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, StringUtils.EMPTY);
return doSelectInstance(result, clusterSelector);
}

private ServiceInfo getServiceInfoBySubscribe(String serviceName, String groupName, List<String> clusters,
NamingSelector selector, boolean subscribe) throws NacosException {
ServiceInfo serviceInfo;
if (subscribe) {
serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString);
if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) {
serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString);
}
serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, StringUtils.EMPTY);
serviceInfo = tryToSubscribe(serviceName, groupName, serviceInfo);
serviceInfo = doSelectInstance(serviceInfo, selector);
} else {
String clusterString = NamingSelectorFactory.getUniqueClusterString(clusters);
serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false);
}
return serviceInfo;
}

private ServiceInfo getServiceInfo(String serviceName, String groupName, List<String> clusters, boolean subscribe)
throws NacosException {
ServiceInfo serviceInfo;
String clusterString = StringUtils.join(clusters, ",");
if (serviceInfoHolder.isFailoverSwitch()) {
serviceInfo = getServiceInfoByFailover(serviceName, groupName, clusterString);
if (serviceInfo != null && serviceInfo.getHosts().size() > 0) {
NAMING_LOGGER.debug("getServiceInfo from failover,serviceName: {} data:{}", serviceName,
JacksonUtils.toJson(serviceInfo.getHosts()));
return serviceInfo;
}
private ServiceInfo tryToSubscribe(String serviceName, String groupName, ServiceInfo cachedServiceInfo) throws NacosException {
// not found in cache, service never subscribed.
if (null == cachedServiceInfo) {
return clientProxy.subscribe(serviceName, groupName, StringUtils.EMPTY);
}

serviceInfo = getServiceInfoBySubscribe(serviceName, groupName, clusterString, subscribe);
// found in cache, and subscribed.
if (clientProxy.isSubscribed(serviceName, groupName, StringUtils.EMPTY)) {
return cachedServiceInfo;
}
// found in cached, but not subscribed, such as cached from local file when starting.
ServiceInfo result = cachedServiceInfo;
try {
result = clientProxy.subscribe(serviceName, groupName, StringUtils.EMPTY);
} catch (NacosException e) {
NAMING_LOGGER.warn("Subscribe from Server failed, will use local cache. fail message: ", e);
}
return result;
}

private ServiceInfo doSelectInstance(ServiceInfo serviceInfo, NamingSelector clusterSelector) {
if (null == serviceInfo) {
return null;
}
NamingContext context = new ServiceInfoContext(serviceInfo);
NamingResult result = clusterSelector.select(context);
serviceInfo.setHosts(result.getResult());
return serviceInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfo
this.serverListManager = new NamingServerListManager(properties, namespace);
this.serverListManager.start();
this.serviceInfoHolder = serviceInfoHolder;
this.securityProxy = new SecurityProxy(this.serverListManager.getServerList(),
this.securityProxy = new SecurityProxy(this.serverListManager,
NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy(properties);
this.httpClientProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.client.naming.selector;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.selector.NamingContext;

import java.util.List;

/**
* Service info context.
*
* @author xiweng.yy
*/
public class ServiceInfoContext implements NamingContext {

private final ServiceInfo serviceInfo;

public ServiceInfoContext(ServiceInfo serviceInfo) {
this.serviceInfo = serviceInfo;
}

@Override
public String getServiceName() {
return serviceInfo.getName();
}

@Override
public String getGroupName() {
return serviceInfo.getGroupName();
}

@Override
public String getClusters() {
return serviceInfo.getClusters();
}

@Override
public List<Instance> getInstances() {
return serviceInfo.getHosts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
package com.alibaba.nacos.client.security;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.address.AbstractServerListManager;
import com.alibaba.nacos.client.address.ServerListChangeEvent;
import com.alibaba.nacos.client.auth.impl.NacosAuthLoginConstant;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthPluginManager;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthService;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthPluginManager;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand All @@ -45,15 +49,25 @@ public class SecurityProxy implements Closeable {
private ClientAuthPluginManager clientAuthPluginManager;

/**
* Construct from serverList, nacosRestTemplate, init client auth plugin.
* // TODO change server list to serverListManager after serverListManager refactor and unite.
* Construct from serverListManager, nacosRestTemplate, init client auth plugin.
*
* @param serverList a server list that client request to.
* @param serverListManager a server list manager that client request to.
* @Param nacosRestTemplate http request template.
*/
public SecurityProxy(List<String> serverList, NacosRestTemplate nacosRestTemplate) {
public SecurityProxy(AbstractServerListManager serverListManager, NacosRestTemplate nacosRestTemplate) {
clientAuthPluginManager = new ClientAuthPluginManager();
clientAuthPluginManager.init(serverList, nacosRestTemplate);
clientAuthPluginManager.init(serverListManager.getServerList(), nacosRestTemplate);
NotifyCenter.registerSubscriber(new Subscriber<ServerListChangeEvent>() {
@Override
public void onEvent(ServerListChangeEvent event) {
clientAuthPluginManager.refreshServerList(serverListManager.getServerList());
}

@Override
public Class<? extends Event> subscribeType() {
return ServerListChangeEvent.class;
}
});
}

/**
Expand Down
Loading

0 comments on commit a5236cc

Please sign in to comment.