mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
commit
a465e215b5
@ -18,7 +18,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<sentinel.version>0.1.1</sentinel.version>
|
<sentinel.version>0.1.1</sentinel.version>
|
||||||
<oss.version>3.1.0</oss.version>
|
<oss.version>3.1.0</oss.version>
|
||||||
<nacos.version>0.2.0</nacos.version>
|
<nacos.version>0.2.1-RC1</nacos.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 地址
|
2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 地址
|
||||||
|
|
||||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||||
|
|
||||||
3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能
|
3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能
|
||||||
|
|
||||||
@ -51,7 +51,7 @@
|
|||||||
|
|
||||||
3. 在命令行执行如下命令,向 Nacos Server 中添加一条配置。
|
3. 在命令行执行如下命令,向 Nacos Server 中添加一条配置。
|
||||||
|
|
||||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"
|
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"
|
||||||
|
|
||||||
**注:你也可以使用其他方式添加,遵循 HTTP API 规范即可,若您使用的 Nacos 版本自带控制台,建议直接使用控制台进行配置**
|
**注:你也可以使用其他方式添加,遵循 HTTP API 规范即可,若您使用的 Nacos 版本自带控制台,建议直接使用控制台进行配置**
|
||||||
|
|
||||||
@ -89,7 +89,7 @@
|
|||||||
#### 验证动态刷新
|
#### 验证动态刷新
|
||||||
1. 执行如下命令,修改 Nacos Server 端的配置数据
|
1. 执行如下命令,修改 Nacos Server 端的配置数据
|
||||||
|
|
||||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18"
|
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18"
|
||||||
|
|
||||||
2. 在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到应用从 Nacos Server 中获取了最新的数据,age 变成了 18。
|
2. 在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到应用从 Nacos Server 中获取了最新的数据,age 变成了 18。
|
||||||
|
|
||||||
@ -112,15 +112,15 @@ Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `Config
|
|||||||
|
|
||||||
在 Nacos Config Starter 中,dataId 的拼接格式如下
|
在 Nacos Config Starter 中,dataId 的拼接格式如下
|
||||||
|
|
||||||
${prefix} - ${spring.active.profile} . ${content-type}
|
${prefix} - ${spring.active.profile} . ${file-extension}
|
||||||
|
|
||||||
* `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
|
* `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
|
||||||
|
|
||||||
* `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
* `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
||||||
|
|
||||||
**注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${context.type}`**
|
**注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${file-extension}`**
|
||||||
|
|
||||||
* `content-type` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。
|
* `file-extension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension`来配置。
|
||||||
目前只支持 `properties` 类型。
|
目前只支持 `properties` 类型。
|
||||||
|
|
||||||
#### group
|
#### group
|
||||||
@ -168,7 +168,7 @@ Spring Boot 2.x 可以通过访问 http://127.0.0.1:18084/actuator/nacos-config
|
|||||||
服务端地址|spring.cloud.nacos.config.server-addr||
|
服务端地址|spring.cloud.nacos.config.server-addr||
|
||||||
DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name
|
DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name
|
||||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
||||||
dataID后缀及数据格式|spring.cloud.nacos.config.content-type|properties|目前只支持 properties
|
dataID后缀及内容文件格式|spring.cloud.nacos.config.file-extension|properties|dataId的后缀,同时也是配置内容的文件格式,目前只支持 properties
|
||||||
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
|
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
|
||||||
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
|
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
|
||||||
配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
|
配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
|
||||||
|
@ -21,7 +21,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
|
|||||||
|
|
||||||
2. Add Nacos server address configurations to file /src/main/resources/bootstrap.properties
|
2. Add Nacos server address configurations to file /src/main/resources/bootstrap.properties
|
||||||
|
|
||||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||||
|
|
||||||
3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh .
|
3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh .
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@ -50,7 +50,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
|
|||||||
|
|
||||||
3. Execute the following command to add a configuration to Nacos Server.
|
3. Execute the following command to add a configuration to Nacos Server.
|
||||||
|
|
||||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"
|
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"
|
||||||
|
|
||||||
**Note: You can also add it in other ways. If you are using the Nacos version with its own console, it is recommended to configure it directly using the console.**
|
**Note: You can also add it in other ways. If you are using the Nacos version with its own console, it is recommended to configure it directly using the console.**
|
||||||
|
|
||||||
@ -89,7 +89,7 @@ Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to,
|
|||||||
#### Dynamic Refresh
|
#### Dynamic Refresh
|
||||||
1. Run the following command to modify the configuration data on the Nacos Server side.
|
1. Run the following command to modify the configuration data on the Nacos Server side.
|
||||||
|
|
||||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18"
|
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18"
|
||||||
|
|
||||||
2. Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to,
|
2. Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to,
|
||||||
We can see that the app got the latest data from Nacos Server and the age becomes 18.
|
We can see that the app got the latest data from Nacos Server and the age becomes 18.
|
||||||
@ -113,15 +113,15 @@ Nacos Client gets data from Nacos Server through this method. `ConfigService.get
|
|||||||
|
|
||||||
In Nacos Config Starter, the splicing format of dataId is as follows
|
In Nacos Config Starter, the splicing format of dataId is as follows
|
||||||
|
|
||||||
${prefix} - ${spring.active.profile} . ${content-type}
|
${prefix} - ${spring.active.profile} . ${file-extension}
|
||||||
|
|
||||||
* `prefix` default value is `spring.application.name` value, which can also be configured via the configuration item `spring.cloud.nacos.config.prefix`.
|
* `prefix` default value is `spring.application.name` value, which can also be configured via the configuration item `spring.cloud.nacos.config.prefix`.
|
||||||
|
|
||||||
* `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
* `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
||||||
|
|
||||||
**Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${context.type}`**
|
**Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${file-extension}`**
|
||||||
|
|
||||||
* `content-type` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.content-type`.
|
* `file-extension` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.file-extension`.
|
||||||
Currently only the `properties` type is supported.
|
Currently only the `properties` type is supported.
|
||||||
|
|
||||||
#### group
|
#### group
|
||||||
@ -172,7 +172,7 @@ Configuration item|key|default value|Description
|
|||||||
server address|spring.cloud.nacos.config.server-addr||
|
server address|spring.cloud.nacos.config.server-addr||
|
||||||
DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name
|
DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name
|
||||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
||||||
dataID content type|spring.cloud.nacos.config.content-type|properties|currently only support properties
|
dataID suffix|spring.cloud.nacos.config.file-extension|properties|the suffix of nacos config dataId, also the file extension of config content.
|
||||||
encoding |spring.cloud.nacos.config.encode|UTF-8|Content encoding
|
encoding |spring.cloud.nacos.config.encode|UTF-8|Content encoding
|
||||||
timeout|spring.cloud.nacos.config.timeout|3000|Get the configuration timeout period,unit is ms
|
timeout|spring.cloud.nacos.config.timeout|3000|Get the configuration timeout period,unit is ms
|
||||||
namespace|spring.cloud.nacos.config.namespace||One of the common scenarios is the separation of the configuration of different environments, such as the development of the test environment and the resource isolation of the production environment.
|
namespace|spring.cloud.nacos.config.namespace||One of the common scenarios is the separation of the configuration of different environments, such as the development of the test environment and the resource isolation of the production environment.
|
||||||
|
@ -1 +1 @@
|
|||||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
@ -1,4 +1,4 @@
|
|||||||
spring.application.name=service-consumer
|
spring.application.name=service-consumer
|
||||||
server.port=18083
|
server.port=18083
|
||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
@ -1,4 +1,4 @@
|
|||||||
server.port=18082
|
server.port=18082
|
||||||
spring.application.name=service-provider
|
spring.application.name=service-provider
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
2. 在应用的 /src/main/resources/application.properties 配置文件中配置 Nacos Server 地址
|
2. 在应用的 /src/main/resources/application.properties 配置文件中配置 Nacos Server 地址
|
||||||
|
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||||
|
|
||||||
3. 使用 @EnableDiscoveryClient 注解开启服务注册与发现功能
|
3. 使用 @EnableDiscoveryClient 注解开启服务注册与发现功能
|
||||||
|
|
||||||
@ -70,7 +70,7 @@
|
|||||||
### 验证
|
### 验证
|
||||||
|
|
||||||
#### 查询服务
|
#### 查询服务
|
||||||
在浏览器输入此地址 `http://127.0.0.1:8080/nacos/v1/ns/instances?serviceName=service-provider`,并点击跳转,可以看到服务节点已经成功注册到 Nacos Server。
|
在浏览器输入此地址 `http://127.0.0.1:8848/nacos/v1/ns/instances?serviceName=service-provider`,并点击跳转,可以看到服务节点已经成功注册到 Nacos Server。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@ -197,7 +197,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key||
|
|||||||
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
||||||
Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置
|
Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置
|
||||||
日志文件名|spring.cloud.nacos.discovery.log-name||
|
日志文件名|spring.cloud.nacos.discovery.log-name||
|
||||||
接入点|spring.cloud.nacos.discovery.enpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
|
接入点|spring.cloud.nacos.discovery.endpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
|
||||||
是否集成Ribbon|ribbon.nacos.enabled|true|
|
是否集成Ribbon|ribbon.nacos.enabled|true|
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
|
|||||||
|
|
||||||
2. Add Nacos server address configurations to file /src/main/resources/application.properties.
|
2. Add Nacos server address configurations to file /src/main/resources/application.properties.
|
||||||
|
|
||||||
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080
|
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
|
||||||
|
|
||||||
3. Use the @EnableDiscoveryClient annotation to turn on service registration and discovery.
|
3. Use the @EnableDiscoveryClient annotation to turn on service registration and discovery.
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
|
|||||||
|
|
||||||
#### Query Service
|
#### Query Service
|
||||||
|
|
||||||
Enter `http://127.0.0.1:8080/nacos/v1/ns/instances?serviceName=service-provider` in the browser address bar and click Go to, we can see that the service node has been successfully registered to Nacos Server.
|
Enter `http://127.0.0.1:8848/nacos/v1/ns/instances?serviceName=service-provider` in the browser address bar and click Go to, we can see that the service node has been successfully registered to Nacos Server.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@ -205,7 +205,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key||
|
|||||||
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
||||||
Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format
|
Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format
|
||||||
log name|spring.cloud.nacos.discovery.log-name||
|
log name|spring.cloud.nacos.discovery.log-name||
|
||||||
endpoint|spring.cloud.nacos.discovery.enpoint||The domain name of a service, through which the server address can be dynamically obtained.
|
endpoint|spring.cloud.nacos.discovery.endpoint||The domain name of a service, through which the server address can be dynamically obtained.
|
||||||
Integration Ribbon|ribbon.nacos.enabled|true|
|
Integration Ribbon|ribbon.nacos.enabled|true|
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,9 +49,9 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
private String prefix;
|
private String prefix;
|
||||||
/**
|
/**
|
||||||
* the content type of nacos config content.
|
* the suffix of nacos config dataId, also the file extension of config content.
|
||||||
*/
|
*/
|
||||||
private String contentType = "properties";
|
private String fileExtension = "properties";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* timeout for get config from nacos.
|
* timeout for get config from nacos.
|
||||||
@ -59,7 +59,8 @@ public class NacosConfigProperties {
|
|||||||
private int timeout = 3000;
|
private int timeout = 3000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* endpoint for Nacos, the domain name of a service, through which the server address can be dynamically obtained.
|
* endpoint for Nacos, the domain name of a service, through which the server address
|
||||||
|
* can be dynamically obtained.
|
||||||
*/
|
*/
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
|
|
||||||
@ -88,7 +89,7 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
private String clusterName;
|
private String clusterName;
|
||||||
|
|
||||||
//todo sts support
|
// todo sts support
|
||||||
|
|
||||||
public String getServerAddr() {
|
public String getServerAddr() {
|
||||||
return serverAddr;
|
return serverAddr;
|
||||||
@ -106,12 +107,12 @@ public class NacosConfigProperties {
|
|||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContentType() {
|
public String getFileExtension() {
|
||||||
return contentType;
|
return fileExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContentType(String contentType) {
|
public void setFileExtension(String fileExtension) {
|
||||||
this.contentType = contentType;
|
this.fileExtension = fileExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGroup() {
|
public String getGroup() {
|
||||||
@ -188,50 +189,52 @@ public class NacosConfigProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosConfigProperties{" +
|
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
"serverAddr='" + serverAddr + '\'' +
|
+ ", encode='" + encode + '\'' + ", group='" + group + '\'' + ", prefix='"
|
||||||
", encode='" + encode + '\'' +
|
+ prefix + '\'' + ", fileExtension='" + fileExtension + '\''
|
||||||
", group='" + group + '\'' +
|
+ ", timeout=" + timeout + ", endpoint='" + endpoint + '\''
|
||||||
", prefix='" + prefix + '\'' +
|
+ ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\''
|
||||||
", contentType='" + contentType + '\'' +
|
+ ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath
|
||||||
", timeout=" + timeout +
|
+ '\'' + ", clusterName='" + clusterName + '\'' + '}';
|
||||||
", endpoint='" + endpoint + '\'' +
|
|
||||||
", namespace='" + namespace + '\'' +
|
|
||||||
", accessKey='" + accessKey + '\'' +
|
|
||||||
", secretKey='" + secretKey + '\'' +
|
|
||||||
", contextPath='" + contextPath + '\'' +
|
|
||||||
", clusterName='" + clusterName + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void overrideFromEnv(Environment env){
|
public void overrideFromEnv(Environment env) {
|
||||||
|
|
||||||
if(StringUtils.isEmpty(this.getServerAddr())) {
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
|
this.setServerAddr(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getEncode())) {
|
if (StringUtils.isEmpty(this.getEncode())) {
|
||||||
this.setEncode(env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}"));
|
this.setEncode(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getNamespace())) {
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}"));
|
this.setNamespace(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getAccessKey())) {
|
if (StringUtils.isEmpty(this.getAccessKey())) {
|
||||||
this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}"));
|
this.setAccessKey(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getSecretKey())) {
|
if (StringUtils.isEmpty(this.getSecretKey())) {
|
||||||
this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}"));
|
this.setSecretKey(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getContextPath())) {
|
if (StringUtils.isEmpty(this.getContextPath())) {
|
||||||
this.setContextPath(env.resolvePlaceholders("${spring.cloud.nacos.config.context-path:}"));
|
this.setContextPath(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.config.context-path:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getClusterName())) {
|
if (StringUtils.isEmpty(this.getClusterName())) {
|
||||||
this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}"));
|
this.setClusterName(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getEndpoint())) {
|
if (StringUtils.isEmpty(this.getEndpoint())) {
|
||||||
this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}"));
|
this.setEndpoint(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getPrefix())) {
|
if (StringUtils.isEmpty(this.getPrefix())) {
|
||||||
this.setPrefix(env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
|
this.setPrefix(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,19 +69,19 @@ public class NacosPropertySourceBuilder {
|
|||||||
* @param dataId Nacos dataId
|
* @param dataId Nacos dataId
|
||||||
* @param group Nacos group
|
* @param group Nacos group
|
||||||
*/
|
*/
|
||||||
NacosPropertySource build(String dataId, String group, String contentType) {
|
NacosPropertySource build(String dataId, String group, String fileExtension) {
|
||||||
Properties p = loadNacosData(dataId, group, contentType);
|
Properties p = loadNacosData(dataId, group, fileExtension);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new NacosPropertySource(dataId, propertiesToMap(p), new Date());
|
return new NacosPropertySource(dataId, propertiesToMap(p), new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Properties loadNacosData(String dataId, String group, String contentType) {
|
private Properties loadNacosData(String dataId, String group, String fileExtension) {
|
||||||
String data = null;
|
String data = null;
|
||||||
try {
|
try {
|
||||||
data = configService.getConfig(dataId, group, timeout);
|
data = configService.getConfig(dataId, group, timeout);
|
||||||
// todo add content type yaml support
|
// todo add file extension yaml support
|
||||||
if (!StringUtils.isEmpty(data)) {
|
if (!StringUtils.isEmpty(data)) {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos.client;
|
package org.springframework.cloud.alibaba.nacos.client;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -81,14 +80,14 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
Properties properties = getPropertiesFromEnv(env);
|
Properties properties = getPropertiesFromEnv(env);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
configService = NacosFactory.createConfigService(properties);
|
configService = NacosFactory.createConfigService(properties);
|
||||||
}
|
}
|
||||||
catch (NacosException e) {
|
catch (NacosException e) {
|
||||||
// todo 为什么这里是 checkException,什么时候会抛出,什么时候需要关心
|
logger.error("create config service error, nacosConfigProperties:{}, ",
|
||||||
logger.error("create config service error, nacosConfigProperties:{}, ", properties, e);
|
properties, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
beanFactory.registerSingleton("configService", configService);
|
beanFactory.registerSingleton("configService", configService);
|
||||||
|
|
||||||
if (null == configService) {
|
if (null == configService) {
|
||||||
@ -97,7 +96,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
long timeout = nacosConfigProperties.getTimeout();
|
long timeout = nacosConfigProperties.getTimeout();
|
||||||
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout);
|
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
||||||
|
timeout);
|
||||||
|
|
||||||
String applicationName = env.getProperty("spring.application.name");
|
String applicationName = env.getProperty("spring.application.name");
|
||||||
logger.info("Initialize spring.application.name '" + applicationName + "'.");
|
logger.info("Initialize spring.application.name '" + applicationName + "'.");
|
||||||
@ -108,32 +108,34 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
dataIdPrefix = applicationName;
|
dataIdPrefix = applicationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
String contentType = nacosConfigProperties.getContentType();
|
String fileExtension = nacosConfigProperties.getFileExtension();
|
||||||
|
|
||||||
CompositePropertySource composite = new CompositePropertySource(
|
CompositePropertySource composite = new CompositePropertySource(
|
||||||
NACOS_PROPERTY_SOURCE_NAME);
|
NACOS_PROPERTY_SOURCE_NAME);
|
||||||
|
|
||||||
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, contentType);
|
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix,
|
||||||
|
fileExtension);
|
||||||
|
|
||||||
return composite;
|
return composite;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadApplicationConfiguration(
|
private void loadApplicationConfiguration(
|
||||||
CompositePropertySource compositePropertySource, Environment environment,
|
CompositePropertySource compositePropertySource, Environment environment,
|
||||||
String nacosGroup, String dataIdPrefix, String contentType) {
|
String nacosGroup, String dataIdPrefix, String fileExtension) {
|
||||||
loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + contentType,
|
loadNacosDataIfPresent(compositePropertySource,
|
||||||
nacosGroup, contentType);
|
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension);
|
||||||
for (String profile : environment.getActiveProfiles()) {
|
for (String profile : environment.getActiveProfiles()) {
|
||||||
String dataId = dataIdPrefix + SEP1 + profile + DOT + contentType;
|
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
|
||||||
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
||||||
contentType);
|
fileExtension);
|
||||||
}
|
}
|
||||||
// todo multi profile active order and priority
|
// todo multi profile active order and priority
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
||||||
final String dataId, final String group,String contentType) {
|
final String dataId, final String group, String fileExtension) {
|
||||||
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, contentType);
|
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
|
||||||
|
fileExtension);
|
||||||
if (ps != null) {
|
if (ps != null) {
|
||||||
composite.addFirstPropertySource(ps);
|
composite.addFirstPropertySource(ps);
|
||||||
}
|
}
|
||||||
|
@ -9,12 +9,12 @@
|
|||||||
"name": "spring.cloud.nacos.config.prefix",
|
"name": "spring.cloud.nacos.config.prefix",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
"defaultValue": "${spring.application.name}",
|
"defaultValue": "${spring.application.name}",
|
||||||
"description": "the prefix of dataId, nacos config data meta info. dataId = prefix + '-' + ${spring.active.profile} + `-` + ${spring.cloud.nacos.config.content-type}."
|
"description": "the prefix of dataId, nacos config data meta info. dataId = prefix + '-' + ${spring.active.profile} + `.` + ${spring.cloud.nacos.config.file-extension}."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.cloud.nacos.config.content-type",
|
"name": "spring.cloud.nacos.config.file-extension",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
"defaultValue": "properties",
|
"defaultValue": "properties",
|
||||||
"description": "the content type of nacos config content, only support properties now."
|
"description": "the suffix of nacos config dataId, also the file extension of config content, only support properties now."
|
||||||
}
|
}
|
||||||
]}
|
]}
|
@ -4,4 +4,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|||||||
org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration,\
|
org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration,\
|
||||||
org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration
|
org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration
|
||||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||||
org.springframework.cloud.alibaba.nacos..analyzer.NacosConnectionFailureAnalyzerdiagnostics
|
org.springframework.cloud.alibaba.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
@ -71,7 +71,7 @@ public class NacosConfigAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class);
|
NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class);
|
||||||
assertThat(nacosConfigProperties.getContentType()).isEqualTo("properties");
|
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
|
||||||
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
|
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,6 @@
|
|||||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--spring boot-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-actuator</artifactId>
|
<artifactId>spring-boot-actuator</artifactId>
|
||||||
@ -62,13 +61,12 @@
|
|||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>test</scope>
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
@ -63,8 +63,10 @@ public class NacosDiscoveryAutoConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
|
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
|
||||||
public NacosAutoServiceRegistration nacosAutoServiceRegistration(
|
public NacosAutoServiceRegistration nacosAutoServiceRegistration(
|
||||||
NacosServiceRegistry registry, AutoServiceRegistrationProperties properties,
|
NacosServiceRegistry registry,
|
||||||
|
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||||
NacosRegistration registration) {
|
NacosRegistration registration) {
|
||||||
return new NacosAutoServiceRegistration(registry, properties, registration);
|
return new NacosAutoServiceRegistration(registry,
|
||||||
|
autoServiceRegistrationProperties, registration);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -35,7 +35,8 @@ import com.alibaba.nacos.api.naming.pojo.ListView;
|
|||||||
*/
|
*/
|
||||||
public class NacosDiscoveryClient implements DiscoveryClient {
|
public class NacosDiscoveryClient implements DiscoveryClient {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(NacosDiscoveryClient.class);
|
private static final Logger LOGGER = LoggerFactory
|
||||||
|
.getLogger(NacosDiscoveryClient.class);
|
||||||
public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";
|
public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -85,7 +86,7 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|||||||
public List<ServiceInstance> getInstances(String serviceId) {
|
public List<ServiceInstance> getInstances(String serviceId) {
|
||||||
try {
|
try {
|
||||||
NamingService namingService = nacosRegistration.getNacosNamingService();
|
NamingService namingService = nacosRegistration.getNacosNamingService();
|
||||||
List<Instance> instances = namingService.selectInstances(serviceId,true);
|
List<Instance> instances = namingService.selectInstances(serviceId, true);
|
||||||
return hostToServiceInstanceList(instances, serviceId);
|
return hostToServiceInstanceList(instances, serviceId);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
@ -94,25 +95,26 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ServiceInstance hostToServiceInstance(Instance instance, String serviceId) {
|
private static ServiceInstance hostToServiceInstance(Instance instance,
|
||||||
|
String serviceId) {
|
||||||
NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
|
NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
|
||||||
nacosServiceInstance.setHost(instance.getIp());
|
nacosServiceInstance.setHost(instance.getIp());
|
||||||
nacosServiceInstance.setPort(instance.getPort());
|
nacosServiceInstance.setPort(instance.getPort());
|
||||||
nacosServiceInstance.setServiceId(serviceId);
|
nacosServiceInstance.setServiceId(serviceId);
|
||||||
Map<String, String> metadata = new HashMap<String, String>();
|
Map<String, String> metadata = new HashMap<String, String>();
|
||||||
metadata.put("instanceId", instance.getInstanceId());
|
metadata.put("instanceId", instance.getInstanceId());
|
||||||
metadata.put("weight", instance.getWeight()+"");
|
metadata.put("weight", instance.getWeight() + "");
|
||||||
metadata.put("healthy", instance.isHealthy()+"");
|
metadata.put("healthy", instance.isHealthy() + "");
|
||||||
metadata.put("cluster", instance.getCluster()+"");
|
metadata.put("cluster", instance.getCluster() + "");
|
||||||
metadata.putAll(instance.getMetadata());
|
metadata.putAll(instance.getMetadata());
|
||||||
nacosServiceInstance.setMetadata(metadata);
|
nacosServiceInstance.setMetadata(metadata);
|
||||||
return nacosServiceInstance;
|
return nacosServiceInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<ServiceInstance> hostToServiceInstanceList(List<Instance> instances,
|
private static List<ServiceInstance> hostToServiceInstanceList(
|
||||||
String serviceId) {
|
List<Instance> instances, String serviceId) {
|
||||||
List<ServiceInstance> result = new ArrayList<ServiceInstance>(instances.size());
|
List<ServiceInstance> result = new ArrayList<ServiceInstance>(instances.size());
|
||||||
for (Instance instance: instances) {
|
for (Instance instance : instances) {
|
||||||
result.add(hostToServiceInstance(instance, serviceId));
|
result.add(hostToServiceInstance(instance, serviceId));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -123,9 +125,11 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
NamingService namingService = nacosRegistration.getNacosNamingService();
|
NamingService namingService = nacosRegistration.getNacosNamingService();
|
||||||
ListView<String> services = namingService.getServicesOfServer(1, Integer.MAX_VALUE);
|
ListView<String> services = namingService.getServicesOfServer(1,
|
||||||
|
Integer.MAX_VALUE);
|
||||||
return services.getData();
|
return services.getData();
|
||||||
}catch( Exception e){
|
}
|
||||||
|
catch (Exception e) {
|
||||||
LOGGER.error("get service name from nacos server fail,", e);
|
LOGGER.error("get service name from nacos server fail,", e);
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos;
|
package org.springframework.cloud.alibaba.nacos;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
@ -49,7 +48,8 @@ public class NacosDiscoveryProperties {
|
|||||||
private String serverAddr;
|
private String serverAddr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the domain name of a service, through which the server address can be dynamically obtained.
|
* the domain name of a service, through which the server address can be dynamically
|
||||||
|
* obtained.
|
||||||
*/
|
*/
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
|
|
||||||
@ -91,8 +91,8 @@ public class NacosDiscoveryProperties {
|
|||||||
private boolean registerEnabled = true;
|
private boolean registerEnabled = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ip address your want to register for your service instance, needn't to set it if
|
* The ip address your want to register for your service instance, needn't to set it
|
||||||
* the auto detect ip works well
|
* if the auto detect ip works well.
|
||||||
*/
|
*/
|
||||||
private String ip;
|
private String ip;
|
||||||
|
|
||||||
@ -123,7 +123,6 @@ public class NacosDiscoveryProperties {
|
|||||||
private String secretKey;
|
private String secretKey;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@JsonIgnore
|
|
||||||
private InetUtils inetUtils;
|
private InetUtils inetUtils;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
@ -190,10 +189,6 @@ public class NacosDiscoveryProperties {
|
|||||||
this.logName = logName;
|
this.logName = logName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InetUtils getInetUtils() {
|
|
||||||
return inetUtils;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setInetUtils(InetUtils inetUtils) {
|
public void setInetUtils(InetUtils inetUtils) {
|
||||||
this.inetUtils = inetUtils;
|
this.inetUtils = inetUtils;
|
||||||
}
|
}
|
||||||
@ -296,47 +291,45 @@ public class NacosDiscoveryProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosDiscoveryProperties{" +
|
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
"serverAddr='" + serverAddr + '\'' +
|
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
|
||||||
", endpoint='" + endpoint + '\'' +
|
+ ", logName='" + logName + '\'' + ", service='" + service + '\''
|
||||||
", namespace='" + namespace + '\'' +
|
+ ", weight=" + weight + ", clusterName='" + clusterName + '\''
|
||||||
", logName='" + logName + '\'' +
|
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
|
||||||
", service='" + service + '\'' +
|
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
|
||||||
", weight=" + weight +
|
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
|
||||||
", clusterName='" + clusterName + '\'' +
|
+ '\'' + ", secretKey='" + secretKey + '\'' + '}';
|
||||||
", metadata=" + metadata +
|
|
||||||
", registerEnabled=" + registerEnabled +
|
|
||||||
", ip='" + ip + '\'' +
|
|
||||||
", networkInterface='" + networkInterface + '\'' +
|
|
||||||
", port=" + port +
|
|
||||||
", secure=" + secure +
|
|
||||||
", accessKey='" + accessKey + '\'' +
|
|
||||||
", secretKey='" + secretKey + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void overrideFromEnv(Environment env){
|
public void overrideFromEnv(Environment env) {
|
||||||
|
|
||||||
if(StringUtils.isEmpty(this.getServerAddr())) {
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
this.setServerAddr(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getNamespace())) {
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
|
this.setNamespace(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getAccessKey())) {
|
if (StringUtils.isEmpty(this.getAccessKey())) {
|
||||||
this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
|
this.setAccessKey(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getSecretKey())) {
|
if (StringUtils.isEmpty(this.getSecretKey())) {
|
||||||
this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
|
this.setSecretKey(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getLogName())) {
|
if (StringUtils.isEmpty(this.getLogName())) {
|
||||||
this.setLogName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
|
this.setLogName(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getClusterName())) {
|
if (StringUtils.isEmpty(this.getClusterName())) {
|
||||||
this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.discovery.clusterName-name:}"));
|
this.setClusterName(env.resolvePlaceholders(
|
||||||
|
"${spring.cloud.nacos.discovery.clusterName-name:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getEndpoint())) {
|
if (StringUtils.isEmpty(this.getEndpoint())) {
|
||||||
this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
this.setEndpoint(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint")
|
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint")
|
||||||
public class NacosDiscoveryEndpointAutoConfiguration {
|
public class NacosDiscoveryEndpointAutoConfiguration {
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
|
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
|
||||||
|
@ -19,12 +19,11 @@ package org.springframework.cloud.alibaba.nacos.registry;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
|
|
||||||
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
||||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -37,20 +36,26 @@ public class NacosAutoServiceRegistration
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NacosRegistration registration;
|
private NacosRegistration registration;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ApplicationContext context;
|
|
||||||
|
|
||||||
public NacosAutoServiceRegistration(
|
public NacosAutoServiceRegistration(
|
||||||
ServiceRegistry<NacosRegistration> serviceRegistry,
|
ServiceRegistry<NacosRegistration> serviceRegistry,
|
||||||
AutoServiceRegistrationProperties properties,
|
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||||
NacosRegistration registration) {
|
NacosRegistration registration) {
|
||||||
super(serviceRegistry, properties);
|
super(serviceRegistry, autoServiceRegistrationProperties);
|
||||||
this.registration = registration;
|
this.registration = registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public void setPort(int port) {
|
||||||
|
getPort().set(port);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NacosRegistration getRegistration() {
|
protected NacosRegistration getRegistration() {
|
||||||
return registration;
|
if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
|
||||||
|
this.registration.setPort(this.getPort().get());
|
||||||
|
}
|
||||||
|
Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
|
||||||
|
return this.registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -58,6 +63,27 @@ public class NacosAutoServiceRegistration
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void register() {
|
||||||
|
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
|
||||||
|
LOGGER.debug("Registration disabled.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.registration.getPort() < 0) {
|
||||||
|
this.registration.setPort(getPort().get());
|
||||||
|
}
|
||||||
|
super.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void registerManagement() {
|
||||||
|
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.registerManagement();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getConfiguredPort() {
|
protected int getConfiguredPort() {
|
||||||
return this.getPort().get();
|
return this.getPort().get();
|
||||||
@ -70,34 +96,19 @@ public class NacosAutoServiceRegistration
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getConfiguration() {
|
protected Object getConfiguration() {
|
||||||
return null;
|
return this.registration.getNacosDiscoveryProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isEnabled() {
|
protected boolean isEnabled() {
|
||||||
return true;
|
return this.registration.getNacosDiscoveryProperties().isRegisterEnabled();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register the local service with the {@link ServiceRegistry}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void register() {
|
|
||||||
this.registration.setPort(this.getPort().get());
|
|
||||||
this.getServiceRegistry().register(getRegistration());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@EventListener(EmbeddedServletContainerInitializedEvent.class)
|
@SuppressWarnings("deprecation")
|
||||||
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
|
protected String getAppName() {
|
||||||
if (context.equals(event.getApplicationContext())) {
|
String appName = registration.getNacosDiscoveryProperties().getService();
|
||||||
int localPort = event.getEmbeddedServletContainer().getPort();
|
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
|
||||||
if (this.getPort().get() == 0) {
|
|
||||||
LOGGER.info("Updating port to " + localPort);
|
|
||||||
this.getPort().compareAndSet(0, localPort);
|
|
||||||
start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@ import com.alibaba.nacos.api.naming.NamingService;
|
|||||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||||
|
|
||||||
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
@ -54,11 +55,12 @@ public class NacosRegistration implements Registration, ServiceInstance {
|
|||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
|
properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
|
||||||
properties.put(NAMESPACE, nacosDiscoveryProperties.getNamespace());
|
properties.put(NAMESPACE, nacosDiscoveryProperties.getNamespace());
|
||||||
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, nacosDiscoveryProperties.getLogName());
|
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME,
|
||||||
|
nacosDiscoveryProperties.getLogName());
|
||||||
properties.put(ENDPOINT, nacosDiscoveryProperties.getEndpoint());
|
properties.put(ENDPOINT, nacosDiscoveryProperties.getEndpoint());
|
||||||
properties.put(ACCESS_KEY,nacosDiscoveryProperties.getAccessKey());
|
properties.put(ACCESS_KEY, nacosDiscoveryProperties.getAccessKey());
|
||||||
properties.put(SECRET_KEY,nacosDiscoveryProperties.getSecretKey());
|
properties.put(SECRET_KEY, nacosDiscoveryProperties.getSecretKey());
|
||||||
properties.put(CLUSTER_NAME,nacosDiscoveryProperties.getClusterName());
|
properties.put(CLUSTER_NAME, nacosDiscoveryProperties.getClusterName());
|
||||||
try {
|
try {
|
||||||
nacosNamingService = NacosFactory.createNamingService(properties);
|
nacosNamingService = NacosFactory.createNamingService(properties);
|
||||||
}
|
}
|
||||||
@ -128,15 +130,14 @@ public class NacosRegistration implements Registration, ServiceInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setNacosDiscoveryProperties(
|
public void setNacosDiscoveryProperties(
|
||||||
NacosDiscoveryProperties nacosDiscoveryProperties) {
|
NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||||
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosRegistration{" +
|
return "NacosRegistration{" + "nacosDiscoveryProperties="
|
||||||
"nacosDiscoveryProperties=" + nacosDiscoveryProperties +
|
+ nacosDiscoveryProperties + ", nacosNamingService=" + nacosNamingService
|
||||||
", nacosNamingService=" + nacosNamingService +
|
+ '}';
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ public class NacosServer extends Server {
|
|||||||
this.metaInfo = new MetaInfo() {
|
this.metaInfo = new MetaInfo() {
|
||||||
@Override
|
@Override
|
||||||
public String getAppName() {
|
public String getAppName() {
|
||||||
return instance.getService().getName();
|
return instance.serviceName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +36,8 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
|||||||
|
|
||||||
private String serviceId;
|
private String serviceId;
|
||||||
|
|
||||||
public NacosServerList(){}
|
public NacosServerList() {
|
||||||
|
}
|
||||||
|
|
||||||
public NacosServerList(String serviceId) {
|
public NacosServerList(String serviceId) {
|
||||||
this.serviceId = serviceId;
|
this.serviceId = serviceId;
|
||||||
@ -54,11 +55,14 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
|||||||
|
|
||||||
private List<NacosServer> getServers() {
|
private List<NacosServer> getServers() {
|
||||||
try {
|
try {
|
||||||
List<Instance> instances = registration.getNacosNamingService().selectInstances(serviceId, true);
|
List<Instance> instances = registration.getNacosNamingService()
|
||||||
|
.getAllInstances(serviceId);
|
||||||
return instancesToServerList(instances);
|
return instancesToServerList(instances);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new IllegalStateException("Can not get service instances from nacos, serviceId=" + serviceId, e);
|
throw new IllegalStateException(
|
||||||
|
"Can not get service instances from nacos, serviceId=" + serviceId,
|
||||||
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user