场景:指定NACOS注册中心中spring cloud微服务应用的IP
spring:cloud:nacos:discovery:ip: 127.0.0.1
修改完成、并重启服务之后在nacos查看的地址如下:
场景:curl请求NACOS常用功能接口
- 测试版本
- nacos-client : 2.0.3
- nacos-server : 2.1.2
- 推荐文献
- https://nacos.io/zh-cn/docs/open-api.html
登录/获取 accessToken
# 获取 accessToken
curl -X POST '127.0.0.1:8848/nacos/v1/auth/login' -d 'username=nacos&password=nacos'
响应内容
{"accessToken":"xxx.xxx.xxxx-xxx-xxx","tokenTtl":86400000,"globalAdmin":false,"username":"read_bdp"}
获取配置
# 获取配置 | NACOS-SERVER 2.1.2 实测
curl -X GET 'https://config.xx.com/nacos/v1/cs/configs?show=all&dataId=application-xx-service.yml&group=XXX_GROUP&tenant=bdp_office&namespaceId=bdp_office&accessToken=xxxxxxx'
服务注册
# 服务注册 | NACOS-SERVER 2.1.2 实测
accessToken="xxxxxxx"
echo "https://nacos-config.xx.com/nacos/v1/ns/instance?port=8848&healthy=true&ip=11.11.11.11&weight=1.0&serviceName=nacos.test.3&encoding=GBK&namespaceId=bigdata_office&accessToken=${accessToken}"
curl -X POST "https://nacos-config.xx.com/nacos/v1/ns/instance?port=8848&healthy=true&ip=11.11.11.11&weight=1.0&serviceName=nacos.test.3&encoding=GBK&namespaceId=bdp_office&accessToken=$accessToken"
- 关键源码
- nacos-client : 2.0.3
- nacos-server : 2.1.2
com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate#registerService--> com.alibaba.nacos.client.naming.remote.NamingClientProxy#registerService--> com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy#registerService@Overridepublic void registerService(String serviceName, String groupName, Instance instance) throws NacosException {NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,instance);redoService.cacheInstanceForRedo(serviceName, groupName, instance);doRegisterService(serviceName, groupName, instance);}--> com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy#doRegisterServicepublic void doRegisterService(String serviceName, String groupName, Instance instance) throws NacosException {InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName,NamingRemoteConstants.REGISTER_INSTANCE, instance);requestToServer(request, Response.class); // 调用 :NamingGrpcClientProxy#requestToServerredoService.instanceRegistered(serviceName, groupName);}--> com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy#requestToServerprivate <T extends Response> T requestToServer(AbstractNamingRequest request, Class<T> responseClass)throws NacosException {try {request.putAllHeader(getSecurityHeaders());request.putAllHeader(getSpasHeaders(NamingUtils.getGroupedNameOptional(request.getServiceName(), request.getGroupName())));Response response =requestTimeout < 0 ? rpcClient.request(request) : rpcClient.request(request, requestTimeout);if (ResponseCode.SUCCESS.getCode() != response.getResultCode()) {throw new NacosException(response.getErrorCode(), response.getMessage());}if (responseClass.isAssignableFrom(response.getClass())) {return (T) response;}NAMING_LOGGER.error("Server return unexpected response '{}', expected response should be '{}'",response.getClass().getName(), responseClass.getName());} catch (Exception e) {throw new NacosException(NacosException.SERVER_ERROR, "Request nacos server failed: ", e);}throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");}
- 注册成功的响应:
https://nacos-config.xx.com/nacos/v1/ns/instance?port=8848&healthy=true&ip=11.11.11.11&weight=1.0&serviceName=nacos.test.3&encoding=GBK&namespaceId=bdp_office&accessToken=xxxxxxx
关键日志
[TID: N/A] [my-xxl-job-executor] [system] [2024/09/06 11:18:21.248] [INFO ] [main] [NamingGrpcClientProxy] registerService:112__||__[REGISTER-SERVICE] bdp_office registering service my-xxl-job-executor with instance Instance{instanceId='null', ip='192.168.19.181', port=9527, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName='DEFAULT', serviceName='null', metadata={management.endpoints.web.base-path=/actuator, preserved.register.source=SPRING_CLOUD}}
[TID: N/A] [my-xxl-job-executor] [system] [2024/09/06 11:18:21.260] [INFO ] [main] [NacosServiceRegistry] register:75__||__nacos registry, BDP_GROUP my-xxl-job-executor-data-distribute 192.168.19.181:9527 register finished
- 当前用户无服务注册权限的响应: (即 可写权限)
以 NACOS SERVER 2.1.2 为例,此错误发生在 强制启用身份认证之后
{"timestamp":"2024-09-06T10:42:11.602+08:00","status":403,"error":"Forbidden","message":"authorization failed!","path":"/nacos/v1/ns/instance"}
关键日志
...
Caused by: java.lang.reflect.UndeclaredThrowableExceptionat org.springframework.util.ReflectionUtils.rethrowRuntimeException(ReflectionUtils.java:147) ~[spring-core-5.2.15.RELEASE.jar!/:5.2.15.RELEASE]at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:82) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) ~[spring-cloud-commons-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:78) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.7.RELEASE....
......
Caused by: com.alibaba.nacos.api.exception.NacosException: Request nacos server failed: at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:279) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doRegisterService(NamingGrpcClientProxy.java:129) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:115) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:95) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:145) ~[nacos-client-2.0.3.jar!/:?]...
...Caused by: com.alibaba.nacos.api.exception.NacosException: authorization failed! at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:271) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doRegisterService(NamingGrpcClientProxy.java:129) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:115) ~[nacos-client-2.0.3.jar!/:?] at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:95) ~[nacos-client-2.0.3.jar!/:?]...
...
获取注册的服务列表
protocol="https"
endpoint="config.xxxx.com:443"
username=nacos
password=xxxxxxcurl -X GET "$protocol://$endpoint/nacos/v1/ns/service/list?namespaceId=bigdata_office&pageNo=1&pageSize=5&accessToken=$accessToken"
response
{"count":0,"doms":[]}
X 参考文献
- Nacos
- https://nacos.io/zh-cn/docs/open-api.html