Compare commits

..

69 Commits

Author SHA1 Message Date
liaochuntao ead0847b66
Merge pull request #99 from chuntaojun/1.x
adding default values
2019-12-17 23:02:09 +08:00
chuntaojun 1a88009ad7 refactor: adding default values 2019-12-14 15:04:50 +08:00
chuntaojun e5bb1b8c7e refactor: code specification and adding default values 2019-12-14 15:02:13 +08:00
liaochuntao e3409c7eed
Merge pull request #93 from chuntaojun/1.x
update nacos-client to 1.1.4
2019-10-29 19:38:56 +08:00
chuntaojun 1ba2491ccd fix: Repair of guava rely on version 2019-10-29 19:35:31 +08:00
chuntaojun 47671df1ad fix(config): Error repair method signature 2019-10-29 18:57:57 +08:00
chuntaojun aff2180ea9 fix(config): fix issue #81 2019-10-25 14:09:29 +08:00
chuntaojun c9427d7fbb refactor(config): Optimizing the allocation of binding code 2019-10-23 15:37:40 +08:00
chuntaojun 0f6a8baa17 fix(config): Repair to remove useless attribute logic 2019-10-21 14:52:36 +08:00
chuntaojun 250138025b style: Add code format file 2019-10-18 13:56:41 +08:00
chuntaojun 9dfa99c988 fix(config): Fix configuration error filter 2019-09-20 22:21:04 +08:00
liaochuntao 31db2c5e44
Merge pull request #82 from chuntaojun/1.x
refactor(config)
2019-09-15 08:44:58 +08:00
chuntaojun 4dded4e4b1 refactor(config): Code optimization 2019-09-15 08:41:13 +08:00
chuntaojun 0001763472 fix(config): Repair the load error log level configuration 2019-09-12 14:42:11 +08:00
liaochuntao bdc4a8ae43
Merge pull request #79 from chuntaojun/1.x
refactor(all):
2019-09-12 12:44:26 +08:00
chuntaojun 0ab2da5283 refactor(all): 2019-09-12 12:32:49 +08:00
liaochuntao 68cdf77f54
Merge pull request #78 from leshalv/1.x
⬆️ 升级 parent模块中 maven-compiler-plugin 版本为3.8.0,删除maven-source…
2019-09-12 10:34:40 +08:00
SanLi 227a3809f3 ⬆️ 升级 parent模块中 maven-compiler-plugin 版本为3.8.0,删除maven-source-plugin.version 声明,添加主项目 pom.xml properties 标签 maven-source-plugin.version 声明,指定版本为3.1.0,并优化maven-source-plugin 插件配置
#71 #72
2019-09-12 10:15:52 +08:00
liaochuntao 506f8fdc26
Merge pull request #75 from chuntaojun/fix_issue_73
feat(config): feat issue #73
2019-09-10 22:58:57 +08:00
chuntaojun 2a3497bae8 feat(config): feat issue #73 2019-09-10 22:31:53 +08:00
liaochuntao 61b5538da6
Merge pull request #69 from chuntaojun/1.x
docs(config):
2019-09-10 09:43:07 +08:00
chuntaojun c53d45d750 docs(config): 2019-09-10 09:40:20 +08:00
liaochuntao ad133c87dd
Merge pull request #68 from chuntaojun/feat_auto_deregister_1.x
feat(config、naming): Support services automatically logout and optimi…
2019-09-10 09:26:27 +08:00
chuntaojun 5146e555f7 fix(config): Repair the config module configuration pull and automatic monitoring error 2019-09-10 00:08:23 +08:00
chuntaojun 6f2c259114 fix(config): 2019-09-09 23:56:23 +08:00
chuntaojun 18e33fb098 feat(config、naming): Support services automatically logout and optimizing the config experience 2019-09-08 16:07:30 +08:00
liaochuntao 953eed2d42
Merge pull request #48 from chuntaojun/1.x
refactor: Optimize configuration information read functions
2019-08-16 12:42:15 +08:00
chuntaojun 774b308b3a refactor: Depend on the upgrade nacos-spring-project to 0.3.3 2019-08-16 12:26:07 +08:00
chuntaojun c83a201fca refactor(update version to 0.1.3): 2019-08-16 10:48:41 +08:00
chuntaojun c66547bad9 refactor(config): Optimize configuration information read functions 2019-08-13 20:02:55 +08:00
chuntaojun 4916289bcb feat(config): add ramRoleName 2019-08-12 09:09:48 +08:00
chuntaojun 1d42a4bf8c feat: 2019-08-09 17:26:23 +08:00
liaochuntao c7f9ede864
Merge pull request #45 from chuntaojun/1.x_log
feat(config): support read log setting
2019-08-06 11:58:52 +08:00
chuntaojun 02675b3167 test: fix test case bug 2019-08-06 11:52:33 +08:00
chuntaojun 333f0d7755 feat(config): support read log setting 2019-08-06 11:39:33 +08:00
liaochuntao ae79de4b97
Merge pull request #38 from chuntaojun/1.x
1.x
2019-07-25 12:14:14 +08:00
chuntaojun 3cc49b3d8e fix(config): fix NacosConfigAutoConfiguration 2019-07-25 11:45:33 +08:00
chuntaojun 7b78753758 fix(config): remove beanfactory register singleton object 2019-07-25 11:43:12 +08:00
chuntaojun 4e102e56d1 fix: 2019-07-25 11:31:40 +08:00
chuntaojun f94a675959 refactor: update version to 0.1.2 2019-07-25 11:26:39 +08:00
liaochuntao 842b0e2135
Merge pull request #33 from chuntaojun/1.x
support new feature and fix issue #18
2019-07-19 11:45:22 +08:00
chuntaojun c63c09f580 ci: 2019-07-19 11:42:20 +08:00
chuntaojun 9e91534831 ci: 2019-07-19 11:31:19 +08:00
chuntaojun 8baae0e520 ci: 2019-07-19 11:27:34 +08:00
chuntaojun 5b389c1b84 ci: 2019-07-19 11:06:26 +08:00
chuntaojun 4eeb85fc6f ci: 2019-07-19 11:02:48 +08:00
chuntaojun c5bce068fa fix(config): remove unuse package 2019-07-19 10:58:02 +08:00
chuntaojun 8b3f1a37c2 feat: 2019-07-19 10:51:43 +08:00
chuntaojun 844242b493 feat(config): support new nacos-client feature and spring-nacos-project feature 2019-07-19 10:41:12 +08:00
chuntaojun 0a9f54f215 feat: 2019-07-19 09:35:14 +08:00
fangjian0423 21972f263b fix test case for 1.x branch 2019-01-29 16:13:08 +08:00
fangjian0423 483271f2ee remove sk in healthindicator and endpoint 2019-01-16 11:39:27 +08:00
fangjian0423 77a0b1ef6d remove duplicate maven plugin 2018-12-13 20:42:39 +08:00
fangjian0423 5bfc05876a update version for 1.x branch 2018-12-13 20:32:37 +08:00
fangjian0423 050d10ab5a add conditional to NacosConfigAutoConfiguration, NacosDiscoveryAutoConfiguration for springboot 1.x 2018-09-26 21:51:35 +08:00
format c5498e440d
Update README.md 2018-09-26 19:04:51 +08:00
fangjian0423 546dd7df39 Integrate with Travis CI 2018-09-26 17:44:34 +08:00
fangjian0423 6cf028d929 Integrate with Travis CI 2018-09-26 17:41:00 +08:00
fangjian0423 83e0bc6809 update nacos spring version 2018-09-26 17:29:58 +08:00
fangjian0423 d839e5c9a6 Integrate with Travis CI 2018-09-26 17:24:34 +08:00
fangjian0423 1f7bcb2736 add nacos server detail info in health indicator 2018-09-25 15:46:19 +08:00
fangjian0423 e4c4d39f18 update samples 2018-09-25 15:17:33 +08:00
fangjian0423 4ff3b44268 modify conditional class in nacos config, discovery actuator 2018-09-25 15:17:21 +08:00
fangjian0423 8fc2b59dbe fix test case 2018-09-22 14:32:07 +08:00
fangjian0423 b84631340f Update documents 2018-09-20 11:44:08 +08:00
fangjian0423 09833490d2 Update documents 2018-09-20 11:39:55 +08:00
fangjian0423 21f9944b6f modify nacos config sample, update docs, modify endpoints 2018-09-19 16:37:20 +08:00
fangjian0423 ac97b09c18 Update documents 2018-09-18 21:23:27 +08:00
fangjian0423 a28b60f1b3 commit 1.x branch to support springboot 1.x 2018-09-18 17:36:53 +08:00
120 changed files with 1127 additions and 9049 deletions

2
.gitignore vendored
View File

@ -29,5 +29,3 @@ hs_err_pid*
.settings .settings
target target
.DS_Store
.flattened-pom.xml

View File

@ -6,10 +6,12 @@ notifications:
on_success: change on_success: change
on_failure: always on_failure: always
dist: trusty
language: java language: java
jdk: jdk:
- openjdk10
- openjdk8 - openjdk8
- openjdk7
script: script:

View File

@ -1,206 +0,0 @@
## Nacos AOT
### prerequisites
- GraalVM22.0+ (JDK17)
- native-image
- Nacos Server
If you haven't downloaded GraalVM, you can download it from [Download GraalVM](https://www.graalvm.org/downloads/).
If you don't have a native-image environment, you can configure according to [Native Image](https://www.graalvm.org/latest/reference-manual/native-image/).
You have to start a Nacos Server in backend , If you don't know steps, you can learn about [quick start](https://nacos.io/en-us/docs/quick-start.html).
### Quick Start
The complete code for this example can be found at: [`nacos-aot-sample`](nacos-spring-boot-samples/nacos-aot-sample)
1. Because SpringBoot2 does not support aot, you must specify SpringBoot as SpringBoot3 in dependency management:
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
```
2. You would add [`nacos-config-spring-boot-starter`](nacos-config-spring-boot-starter) or [`nacos-discovery-spring-boot-starter`](nacos-discovery-spring-boot-starter) in your Spring application's dependencies :
```xml
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
```
3. In order to use the process-aot feature of SpringBoot and more conveniently compile native-image programs, we also need `spring-boot-maven-plugin` and `native-maven-plugin`:
```xml
<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.25</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<mainClass>com.alibaba.boot.nacos.sample.AotApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
```
4. You could define some configurations in `application.properties`:
```properties
nacos.config.server-addr=localhost:8848
nacos.discovery.server-addr=localhost:8848
```
> `nacos.config.server-addr` and `nacos.discovery.server-addr` attribute configure "\${host}:${port}" of your Nacos Server
5. You could using `@SpringBootApplication` to annotate main class like normal SpringBoot Application and startup:
```java
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class AotApplication {
public static void main(String[] args) {
SpringApplication.run(AotApplication.class, args);
}
}
```
Note: `@NacosPropertySource` is used to specify the data-id of the configuration.
6. Let's try the functions of `@NacosInjected` and `@NacosValue`, for more usage, see [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project).
```java
@Controller
public class AotController {
@NacosInjected
private ConfigService configService;
@NacosInjected
private NamingService namingService;
@NacosValue(value = "${flag:false}", autoRefreshed = true)
private boolean flag;
@ResponseBody
@RequestMapping(value = "/config/get", method = GET)
public String getConfig() throws NacosException {
return configService.getConfig("example", "DEFAULT_GROUP", 5000);
}
@ResponseBody
@RequestMapping(value = "/naming/get", method = GET)
public List<Instance> getNaming(@RequestParam("serviceName") String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
@ResponseBody
@RequestMapping(value = "/flag/get", method = GET)
public boolean getFlag() {
return flag;
}
}
```
7. Publish the configuration to the Nacos Server by calling the Nacos Open API, with dataId of example and content of `flag=true`
```shell
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=flag=true"
```
8. Enable the tracing agent on the command line with the `java` command from the GraalVM JDK:
```shell
$JAVA_HOME/bin/java -agentlib:native-image-agent=config-output-dir=/path/to/config-dir/ ...
```
Note: For more information about tracing agent, see [Collect Metadata with the Tracing Agent](https://www.graalvm.org/latest/reference-manual/native-image/metadata/AutomaticMetadataCollection/).
9. Run the java program and follow the steps below to use the program:
1. Open `localhost:8080/config/get` with a browser, and the browser responds with `flag=true`.
2. Open `localhost:8080/flag/get` with a browser, and the browser responds with `true`.
3. Publish the configuration to the Nacos Server by calling the Nacos Open API, with dataId of example and content of `flag=false`
```shell
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=flag=false"
```
4. Open `localhost:8080/flag/get` with a browser, and the browser responds with `false`.
5. Register a service named example with Nacos server by calling Nacos Open API.
```shell
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=example&ip=127.0.0.1&port=8080'
```
6. Open `localhost:8080/naming/get?serviceName=example` with a browser, and the browser responds with information about the service you just registered.
10. Close the program and copy the file just generated to the `classpath:META-INF/native-image/` folder, The file path is specified in step 8.
Note: [`nacos-aot-sample`](nacos-spring-boot-samples/nacos-aot-sample) already includes these files, but you can still use the files you just generated.
11. Run the following command and you will find the compiled executable program in the `target` folder.
```shell
mvn -DskipTests=true clean package -Pnative
```
12. This executable program is what we want! Run the program and try step 9 again.
## Relative Projects
* [Alibaba Nacos](https://github.com/alibaba/nacos)
* [Alibaba Spring Context Support](https://github.com/alibaba/spring-context-support)
* [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project)
* [Nacos Spring Cloud Project](https://github.com/spring-cloud-incubator/spring-cloud-alibaba)

View File

@ -1,144 +0,0 @@
### Nacos Config
First, you have to start a Nacos Server in backend , If you don't know steps, you can learn about [quick start](https://nacos.io/en-us/docs/quick-start.html).
Suppose your Nacos Server is startup, you would add [`nacos-config-spring-boot-starter`](nacos-config-spring-boot-starter) in your Spring application's dependencies :
```xml
<dependencies>
...
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
...
</dependencies>
```
Note: Version [0.2.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter) is compatible with the Spring Boot 2.x and the Spring Boot 3.x. Version [0.1.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter) is compatible with the Spring Boot 1.x.
After that, you could define some configurations in `application.properties`:
```properties
nacos.config.server-addr=localhost:8848
```
> `nacos.config.server-addr` attribute configures "\${host}:${port}" of your Nacos Server
Then you could using `@SpringBootApplication` to annotate main class like normal SpringBoot Application and startup:
```java
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
```
If you'd like to use "Distributed Configuration" features, `ConfigService` is a core service interface to get or publish config, you could use "Dependency Injection" to inject `ConfigService` instance in your Spring Beans.
```java
@Service
public class ConfigServiceDemo {
@NacosInjected
private ConfigService configService;
public void demoGetConfig() {
try {
String dataId = "{dataId}";
String group = "{group}";
String content = configService.getConfig(dataId, groupId, 5000);
System.out.println(content);
} catch (NacosException e) {
e.printStackTrace();
}
}
...
}
```
above code equals below one:
```java
try {
// Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.
String serverAddr = "{serverAddr}";
String dataId = "{dataId}";
String group = "{group}";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
// Actively get the configuration.
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
} catch (NacosException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
```
## Endpoint
Nacos config starter support the implementation of Spring Boot actuator endpoints.
**Prerequisite:**
Adding `nacos-config-spring-boot-actuator` to your pom.xml in Nacos Config Project.
Then Configure your endpoint security strategy.
* Spring Boot1.x
```properties
management.security.enabled=false
```
* Spring Boot2.x or Spring Boot3.x
```properties
management.endpoints.web.exposure.include=*
```
To view the endpoint information, visit the following URLS:
* Spring Boot1.x: URL is http://127.0.0.1:10011/nacos-config.
* Spring Boot2.x or Spring Boot3.x: URL is http://127.0.0.1:10011/actuator/nacos-config.
## Health Checks
`nacos-config-spring-boot-actuator` support the standard Spring Boot `HealthIndicator` as a production-ready feature , which will be aggregated into Spring Boot's `Health` and exported on `HealthEndpoint` that works MVC (Spring Web MVC) if it is available.
Suppose a Spring Boot Web application did not specify `management.server.port`(SpringBoot1.x using `management.port`), you can access http://localhost:{port}/actuator/health (SpringBoot1.x visit http://localhost:{port}/health) via Web Client and will get a response with JSON format is like below :
```json
{
"status": "UP",
"details": {
"nacosConfig": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"details": {
"total": 250140434432,
"free": 52323512320,
"threshold": 10485760
}
}
}
}
```
For more information about Nacos Spring, see [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project).
## Relative Projects
* [Alibaba Nacos](https://github.com/alibaba/nacos)
* [Alibaba Spring Context Support](https://github.com/alibaba/spring-context-support)
* [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project)
* [Nacos Spring Cloud Project](https://github.com/spring-cloud-incubator/spring-cloud-alibaba)

View File

@ -1,131 +0,0 @@
### Nacos Discovery
First, you have to start a Nacos Server in backend , If you don't know steps, you can learn about [quick start](https://nacos.io/en-us/docs/quick-start.html).
Suppose your Nacos Server is startup, you would add [`nacos-discovery-spring-boot-starter`](nacos-discovery-spring-boot-starter) in your Spring application's dependencies :
```xml
<dependencies>
...
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
...
</dependencies>
```
Note: Version [0.2.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.boot/nacos-discovery-spring-boot-starter) is compatible with the Spring Boot 2.x and the Spring Boot 3.x. Version [0.1.x.RELEASE](https://mvnrepository.com/artifact/com.alibaba.boot/nacos-discovery-spring-boot-starter) is compatible with the Spring Boot 1.x.
After that, you could define some configurations in `application.properties`:
```properties
nacos.discovery.server-addr=localhost:8848
```
> `nacos.discovery.server-addr` attribute configures "\${host}:${port}" of your Nacos Server
Then you could using `@SpringBootApplication` to annotate main class like normal SpringBoot Application and startup:
```java
@SpringBootApplication
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
```
If you'd like to use "Service Registry" features, `NamingService` is a core service interface to get or publish config, you could use "Dependency Injection" to inject `NamingService` instance in your Spring Beans.
```java
@Service
public class NamingServiceDemo {
@NacosInjected
private NamingService namingService;
public void demoRegisterService() {
try {
namingService.registerInstance("test-service", "1.1.1.1", 8080);
} catch (NacosException e) {
e.printStackTrace();
}
}
...
}
```
above code equals below one:
```java
try {
// Initialize the naming service, and the console automatically obtains the following parameters through the sample code.
String serverAddr = "{serverAddr}";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
NamingService naming = NamingFactory.createNamingService(properties);
namingService.registerInstance("test-service", "1.1.1.1", 8080);
} catch (NacosException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
```
For more information about Nacos Spring, see [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project).
## Endpoint
Nacos discovery starter also support the implementation of Spring Boot actuator endpoints.
**Prerequisite:**
Adding `nacos-discovery-spring-boot-actuator` to your pom.xml in Nacos Discovery Project.
Then Configure your endpoint security strategy.
* Spring Boot1.x
```properties
management.security.enabled=false
```
* Spring Boot2.x or Spring Boot3.x
```properties
management.endpoints.web.exposure.include=*
```
To view the endpoint information, visit the following URLS:
* Spring Boot1.x: URL is http://127.0.0.1:10012/nacos-discovery.
* Spring Boot2.x or Spring Boot3.x: URL is http://127.0.0.1:10012/actuator/nacos-discovery.
## Health Checks
`nacos-discovery-spring-boot-actuator` support the standard Spring Boot `HealthIndicator` as a production-ready feature , which will be aggregated into Spring Boot's `Health` and exported on `HealthEndpoint` that works MVC (Spring Web MVC) if it is available.
Suppose a Spring Boot Web application did not specify `management.server.port`(SpringBoot1.x using `management.port`), you can access http://localhost:{port}/actuator/health (SpringBoot1.x visit http://localhost:{port}/health) via Web Client and will get a response with JSON format is like below :
```json
{
"status": "UP",
"details": {
"nacosDiscovery": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"details": {
"total": 250140434432,
"free": 52323680256,
"threshold": 10485760
}
}
}
}
```

252
README.md
View File

@ -18,48 +18,276 @@ Nacos Spring Boot Project consist of two parts: `nacos-config-spring-boot` and `
`nacos-discovery-spring-boot` module is using for Service Discovery, Service Health Check and Dynamic DNS Service. `nacos-discovery-spring-boot` module is using for Service Discovery, Service Health Check and Dynamic DNS Service.
Additionally, Nacos Spring Boot Project already supports native-image.
## Samples ## Samples
- [Nacos Config Sample](https://github.com/nacos-group/nacos-spring-boot-project/tree/master/nacos-spring-boot-samples/nacos-config-sample) - [Nacos Config Sample](https://github.com/nacos-group/nacos-spring-boot-project/tree/master/nacos-spring-boot-samples/nacos-config-sample)
- [Nacos Discovery Sample](https://github.com/nacos-group/nacos-spring-boot-project/tree/master/nacos-spring-boot-samples/nacos-discovery-sample) - [Nacos Discovery Sample](https://github.com/nacos-group/nacos-spring-boot-project/tree/master/nacos-spring-boot-samples/nacos-discovery-sample)
- [Nacos AOT Sample](https://github.com/nacos-group/nacos-spring-boot-project/tree/master/nacos-spring-boot-samples/nacos-aot-sample)
## Dependencies & Compatibility ## Dependencies & Compatibility
**Version: 0.2.x / 2.x.x ( branch master )** **master branch**
| Dependencies | Compatibility | | Dependencies | Compatibility |
| -------------- | ------------- | | -------------- | ------------- |
| Java | 1.8+ | | Java | 1.8+ |
| Spring Boot | 2.0.3.RELEASE | | Spring Boot | 2.0.3.RELEASE |
| Nacos-Spring-Context | 1.1.0 |
**Version: 0.1.x / 1.x.x ( branch: 1.x )** **1.x branch**
| Dependencies | Compatibility | | Dependencies | Compatibility |
| -------------- | ------------- | | -------------- | ------------- |
| Java | 1.7+ | | Java | 1.7+ |
| Spring Boot | 1.4.1.RELEASE | | Spring Boot | 1.4.1.RELEASE |
| Nacos-Spring-Context | 1.1.0 |
## Quick Start ## Quick Start
### Nacos Config
- [Nacos Config Quick Start](https://github.com/nacos-group/nacos-spring-boot-project/blob/master/NACOS-CONFIG-QUICK-START.md) First, you have to start a Nacos Server in backend , If you don't know steps, you can learn about [quick start](https://nacos.io/en-us/docs/quick-start.html).
- [Nacos Discovery Quick Start](https://github.com/nacos-group/nacos-spring-boot-project/blob/master/NACOS-DISCOVERY-QUICK-START.md) Suppose your Nacos Server is startup, you would add [`nacos-config-spring-boot-starter`](nacos-config-spring-boot-starter) in your Spring application's dependencies :
- [Nacos AOT Quick Start](https://github.com/nacos-group/nacos-spring-boot-project/blob/master/NACOS-AOT-QUICK-START.md) ```xml
<dependencies>
...
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
...
</dependencies>
```
After that, you could define some configurations in `application.properties`:
```properties
nacos.config.server-addr=localhost
```
> `nacos.config.server-addr` attribute configures "\${host}:${port}" of your Nacos Server
Then you could using `@SpringBootApplication` to annotate main class like normal SpringBoot Application and startup:
```java
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
```
If you'd like to use "Distributed Configuration" features, `ConfigService` is a core service interface to get or publish config, you could use "Dependency Injection" to inject `ConfigService` instance in your Spring Beans.
```java
@Service
public class ConfigServiceDemo {
@NacosInjected
private ConfigService configService;
public void demoGetConfig() {
try {
String dataId = "{dataId}";
String group = "{group}";
String content = configService.getConfig(dataId, groupId, 5000);
System.out.println(content);
} catch (NacosException e) {
e.printStackTrace();
}
}
...
}
```
above code equals below one:
```java
try {
// Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.
String serverAddr = "{serverAddr}";
String dataId = "{dataId}";
String group = "{group}";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
// Actively get the configuration.
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
} catch (NacosException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
```
### Nacos Discovery
First, you have to start a Nacos Server in backend , If you don't know steps, you can learn about [quick start](https://nacos.io/en-us/docs/quick-start.html).
Suppose your Nacos Server is startup, you would add [`nacos-discovery-spring-boot-starter`](nacos-discovery-spring-boot-starter) in your Spring application's dependencies :
```xml
<dependencies>
...
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
...
</dependencies>
```
After that, you could define some configurations in `application.properties`:
```properties
nacos.discovery.server-addr=localhost
```
> `nacos.discovery.server-addr` attribute configures "\${host}:${port}" of your Nacos Server
Then you could using `@SpringBootApplication` to annotate main class like normal SpringBoot Application and startup:
```java
@SpringBootApplication
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
```
If you'd like to use "Service Registry" features, `NamingService` is a core service interface to get or publish config, you could use "Dependency Injection" to inject `NamingService` instance in your Spring Beans.
```java
@Service
public class NamingServiceDemo {
@NacosInjected
private NamingService namingService;
public void demoRegisterService() {
try {
namingService.registerInstance("test-service", "1.1.1.1", 8080);
} catch (NacosException e) {
e.printStackTrace();
}
}
...
}
```
above code equals below one:
```java
try {
// Initialize the naming service, and the console automatically obtains the following parameters through the sample code.
String serverAddr = "{serverAddr}";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
NamingService naming = NamingFactory.createNamingService(properties);
namingService.registerInstance("test-service", "1.1.1.1", 8080);
} catch (NacosException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
```
For more information about Nacos Spring, see [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project).
## Endpoint
Nacos config starter and Nacos discovery starter also support the implementation of Spring Boot actuator endpoints.
**Prerequisite:**
Adding `nacos-config-spring-boot-actuator` to your pom.xml in Nacos Config Project.
Adding `nacos-discovery-spring-boot-actuator` to your pom.xml in Nacos Discovery Project.
Then Configure your endpoint security strategy.
* Spring Boot1.x
```properties
management.security.enabled=false
```
* Spring Boot2.x
```properties
management.endpoints.web.exposure.include=*
```
To view the endpoint information, visit the following URLS:
Nacos Config Project :
* Spring Boot1.x: Nacos Config Endpoint URL is http://127.0.0.1:10011/nacos-config.
* Spring Boot2.x: Nacos Config Endpoint URL is http://127.0.0.1:10011/actuator/nacos-config.
Nacos Discovery Project:
* Spring Boot1.x: Nacos Discovery Endpoint URL is http://127.0.0.1:10012/nacos-discovery.
* Spring Boot2.x: Nacos Discovery Endpoint URL is http://127.0.0.1:10012/actuator/nacos-discovery.
## Health Checks
`nacos-config-spring-boot-actuator` and `nacos-discovery-spring-boot-actuator` support the standard Spring Boot `HealthIndicator` as a production-ready feature , which will be aggregated into Spring Boot's `Health` and exported on `HealthEndpoint` that works MVC (Spring Web MVC) if it is available.
Suppose a Spring Boot Web application did not specify `management.server.port`(SpringBoot1.x using `management.port`), you can access http://localhost:{port}/actuator/health (SpringBoot1.x visit http://localhost:{port}/health) via Web Client and will get a response with JSON format is like below :
Nacos Config Project:
```json
{
"status": "UP",
"details": {
"nacosConfig": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"details": {
"total": 250140434432,
"free": 52323512320,
"threshold": 10485760
}
}
}
}
```
Nacos Discovery Project:
```json
{
"status": "UP",
"details": {
"nacosDiscovery": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"details": {
"total": 250140434432,
"free": 52323680256,
"threshold": 10485760
}
}
}
}
```
For more information about Nacos Spring, see [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project). For more information about Nacos Spring, see [Nacos Spring Project](https://github.com/nacos-group/nacos-spring-project).
For more information about user guide, see [User Guide](https://github.com/nacos-group/nacos-spring-boot-project/wiki/%E7%94%A8%E6%88%B7%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3).
## Relative Projects ## Relative Projects
* [Alibaba Nacos](https://github.com/alibaba/nacos) * [Alibaba Nacos](https://github.com/alibaba/nacos)

View File

@ -18,7 +18,7 @@
<parent> <parent>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent/pom.xml</relativePath> <relativePath>../nacos-spring-boot-parent/pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -81,18 +81,6 @@
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<optional>true</optional> <optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency> </dependency>
<!-- Test Dependencies --> <!-- Test Dependencies -->

View File

@ -17,8 +17,11 @@
package com.alibaba.boot.nacos.actuate.autoconfigure; package com.alibaba.boot.nacos.actuate.autoconfigure;
import com.alibaba.boot.nacos.actuate.endpoint.NacosConfigEndpoint; import com.alibaba.boot.nacos.actuate.endpoint.NacosConfigEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; import com.alibaba.boot.nacos.config.NacosConfigConstants;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -30,11 +33,12 @@ import org.springframework.context.annotation.Configuration;
* @see Endpoint * @see Endpoint
*/ */
@Configuration @Configuration
@ConditionalOnClass(Endpoint.class)
public class NacosConfigEndpointAutoConfiguration { public class NacosConfigEndpointAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint @ConditionalOnEnabledEndpoint(NacosConfigConstants.ENDPOINT_PREFIX)
public NacosConfigEndpoint nacosEndpoint() { public NacosConfigEndpoint nacosEndpoint() {
return new NacosConfigEndpoint(); return new NacosConfigEndpoint();
} }

View File

@ -20,8 +20,8 @@ import com.alibaba.boot.nacos.actuate.health.NacosConfigHealthIndicator;
import com.alibaba.boot.nacos.config.NacosConfigConstants; import com.alibaba.boot.nacos.config.NacosConfigConstants;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration; import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;

View File

@ -23,20 +23,18 @@ import java.util.Properties;
import com.alibaba.boot.nacos.common.PropertiesUtils; import com.alibaba.boot.nacos.common.PropertiesUtils;
import com.alibaba.boot.nacos.config.NacosConfigConstants; import com.alibaba.boot.nacos.config.NacosConfigConstants;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener; import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties; import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.spring.context.event.config.NacosConfigMetadataEvent; import com.alibaba.nacos.spring.context.event.config.NacosConfigMetadataEvent;
import com.alibaba.nacos.spring.util.NacosUtils; import com.alibaba.nacos.spring.util.NacosUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
@ -50,16 +48,19 @@ import static com.alibaba.nacos.spring.util.NacosBeanUtils.CONFIG_GLOBAL_NACOS_P
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @see Endpoint * @see Endpoint
*/ */
@Endpoint(id = NacosConfigConstants.ENDPOINT_PREFIX) public class NacosConfigEndpoint extends AbstractEndpoint<Map<String, Object>>
public class NacosConfigEndpoint
implements ApplicationListener<NacosConfigMetadataEvent> { implements ApplicationListener<NacosConfigMetadataEvent> {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
private Map<String, JsonNode> nacosConfigMetadataMap = new HashMap<>(8); private Map<String, JSONObject> nacosConfigMetadataMap = new HashMap<>();
@ReadOperation public NacosConfigEndpoint() {
super(NacosConfigConstants.ENDPOINT_PREFIX);
}
@Override
public Map<String, Object> invoke() { public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<>(8); Map<String, Object> result = new HashMap<>(8);
@ -84,37 +85,37 @@ public class NacosConfigEndpoint
public void onApplicationEvent(NacosConfigMetadataEvent event) { public void onApplicationEvent(NacosConfigMetadataEvent event) {
String key = buildMetadataKey(event); String key = buildMetadataKey(event);
if (StringUtils.isNotEmpty(key) && !nacosConfigMetadataMap.containsKey(key)) { if (StringUtils.isNotEmpty(key) && !nacosConfigMetadataMap.containsKey(key)) {
ObjectNode jsonNode = JacksonUtils.createEmptyJsonNode(); JSONObject jsonObject = new JSONObject();
jsonNode.put("groupId", event.getGroupId()); jsonObject.put("groupId", event.getGroupId());
jsonNode.put("dataId", event.getDataId()); jsonObject.put("dataId", event.getDataId());
if (ClassUtils.isAssignable(event.getSource().getClass(), if (ClassUtils.isAssignable(event.getSource().getClass(),
AnnotationMetadata.class)) { AnnotationMetadata.class)) {
jsonNode.put("origin", "NacosPropertySource"); jsonObject.put("origin", "NacosPropertySource");
jsonNode.put("target", jsonObject.put("target",
((AnnotationMetadata) event.getSource()).getClassName()); ((AnnotationMetadata) event.getSource()).getClassName());
} }
else if (ClassUtils.isAssignable(event.getSource().getClass(), else if (ClassUtils.isAssignable(event.getSource().getClass(),
NacosConfigListener.class)) { NacosConfigListener.class)) {
jsonNode.put("origin", "NacosConfigListener"); jsonObject.put("origin", "NacosConfigListener");
Method configListenerMethod = (Method) event.getAnnotatedElement(); Method configListenerMethod = (Method) event.getAnnotatedElement();
jsonNode.put("target", jsonObject.put("target",
configListenerMethod.getDeclaringClass().getName() + ":" configListenerMethod.getDeclaringClass().getName() + ":"
+ configListenerMethod.toString()); + configListenerMethod.toString());
} }
else if (ClassUtils.isAssignable(event.getSource().getClass(), else if (ClassUtils.isAssignable(event.getSource().getClass(),
NacosConfigurationProperties.class)) { NacosConfigurationProperties.class)) {
jsonNode.put("origin", "NacosConfigurationProperties"); jsonObject.put("origin", "NacosConfigurationProperties");
jsonNode.put("target", event.getBeanType().getName()); jsonObject.put("target", event.getBeanType().getName());
} }
else if (ClassUtils.isAssignable(event.getSource().getClass(), else if (ClassUtils.isAssignable(event.getSource().getClass(),
Element.class)) { Element.class)) {
jsonNode.put("origin", "NacosPropertySource"); jsonObject.put("origin", "NacosPropertySource");
jsonNode.put("target", event.getXmlResource().toString()); jsonObject.put("target", event.getXmlResource().toString());
} }
else { else {
throw new RuntimeException("unknown NacosConfigMetadataEvent"); throw new RuntimeException("unknown NacosConfigMetadataEvent");
} }
nacosConfigMetadataMap.put(key, jsonNode); nacosConfigMetadataMap.put(key, jsonObject);
} }
} }

View File

@ -19,8 +19,8 @@ package com.alibaba.boot.nacos.actuate.health;
import java.util.Properties; import java.util.Properties;
import com.alibaba.boot.nacos.common.PropertiesUtils; import com.alibaba.boot.nacos.common.PropertiesUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory; import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.factory.NacosServiceFactory; import com.alibaba.nacos.spring.factory.NacosServiceFactory;
import com.alibaba.nacos.spring.metadata.NacosServiceMetaData; import com.alibaba.nacos.spring.metadata.NacosServiceMetaData;
@ -38,7 +38,6 @@ import org.springframework.context.ApplicationContext;
* @see HealthIndicator * @see HealthIndicator
*/ */
public class NacosConfigHealthIndicator extends AbstractHealthIndicator { public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@ -54,7 +53,7 @@ public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) configService; NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) configService;
Properties properties = nacosServiceMetaData.getProperties(); Properties properties = nacosServiceMetaData.getProperties();
builder.withDetail( builder.withDetail(
JacksonUtils.toJson( JSON.toJSONString(
PropertiesUtils.extractSafeProperties(properties)), PropertiesUtils.extractSafeProperties(properties)),
configService.getServerStatus()); configService.getServerStatus());
} }

View File

@ -1,56 +0,0 @@
[
{
"name": "org.springframework.core.env.ConfigurableEnvironment",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true
},
{
"name": "java.util.Properties",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true
},
{
"name": "org.springframework.core.type.AnnotationMetadata",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true
},
{
"name": "com.alibaba.nacos.api.config.annotation.NacosConfigListener",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true
},
{
"name": "com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true
},
{
"name": "org.w3c.dom.Element",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true
}
]

View File

@ -1,2 +0,0 @@
com.alibaba.boot.nacos.actuate.autoconfigure.NacosConfigEndpointAutoConfiguration
com.alibaba.boot.nacos.actuate.autoconfigure.NacosConfigHealthIndicatorAutoConfiguration

View File

@ -1,51 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.actuate.autoconfigure;
import com.alibaba.boot.nacos.actuate.endpoint.NacosConfigEndpoint;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosConfigEndpointAutoConfiguration} Test
* @ClassName: NacosConfigEndpointAutoConfigurationTest
* @Author: SuperZ1999
* @Date: 2023/9/28
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {NacosConfigEndpointAutoConfiguration.class})
@TestPropertySource(properties = {
"management.endpoints.web.exposure.include=nacos-config"
})
public class NacosConfigEndpointAutoConfigurationTest {
@Autowired
private ApplicationContext context;
@Test
public void testNacosConfigEndpointBean() {
Assert.assertNotNull(context.getBean(NacosConfigEndpoint.class));
}
}

View File

@ -1,47 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.actuate.autoconfigure;
import com.alibaba.boot.nacos.actuate.health.NacosConfigHealthIndicator;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosConfigHealthIndicatorAutoConfiguration} Test
* @ClassName: NacosConfigHealthIndicatorAutoConfigurationTest
* @Author: SuperZ1999
* @Date: 2023/9/28
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {NacosConfigHealthIndicatorAutoConfiguration.class})
public class NacosConfigHealthIndicatorAutoConfigurationTest {
@Autowired
private ApplicationContext context;
@Test
public void testNacosConfigHealthIndicatorBean() {
Assert.assertNotNull(context.getBean(NacosConfigHealthIndicator.class));
}
}

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent</relativePath> <relativePath>../nacos-spring-boot-parent</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -27,18 +27,6 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Nacos Config Spring Boot AutoConfigure</name> <name>Nacos Config Spring Boot AutoConfigure</name>
<description>Nacos Config Spring Boot AutoConfigure</description> <description>Nacos Config Spring Boot AutoConfigure</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies> <dependencies>
@ -66,33 +54,14 @@
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<optional>true</optional> <optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-base</artifactId> <artifactId>nacos-spring-boot-base</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-aot</artifactId>
<optional>true</optional>
</dependency>
<!-- Test Dependencies --> <!-- Test Dependencies -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -25,7 +25,7 @@ import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
*/ */
public interface NacosConfigConstants { public interface NacosConfigConstants {
String ENDPOINT_PREFIX = "nacos-config"; String ENDPOINT_PREFIX = "nacos_config";
String ENABLED = EnableNacosConfig.CONFIG_PREFIX + "enabled"; String ENABLED = EnableNacosConfig.CONFIG_PREFIX + "enabled";

View File

@ -18,7 +18,7 @@ package com.alibaba.boot.nacos.config.autoconfigure;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class NacosBootConfigException extends RuntimeException { public class NacosBootConfigException extends RuntimeException {

View File

@ -17,88 +17,83 @@
package com.alibaba.boot.nacos.config.autoconfigure; package com.alibaba.boot.nacos.config.autoconfigure;
import java.util.Properties; import java.util.Properties;
import java.util.function.Function;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigLoader; import com.alibaba.boot.nacos.config.util.Function;
import com.alibaba.boot.nacos.config.util.NacosConfigLoaderFactory;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils; import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import com.alibaba.boot.nacos.config.util.NacosConfigUtils;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.utils.SnapShotSwitch;
import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory; import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.util.NacosBeanUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.2 * @since 0.1.2
*/ */
public class NacosConfigApplicationContextInitializer public class NacosConfigApplicationInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> { implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private final Logger logger = LoggerFactory private final Logger logger = LoggerFactory
.getLogger(NacosConfigApplicationContextInitializer.class); .getLogger(NacosConfigApplicationInitializer.class);
private final NacosConfigEnvironmentProcessor processor;
private final CacheableEventPublishingNacosServiceFactory singleton = CacheableEventPublishingNacosServiceFactory
.getSingleton();
private final Function<Properties, ConfigService> builder = properties -> {
try {
return singleton.createConfigService(properties);
}
catch (NacosException e) {
throw new NacosBootConfigException(
"ConfigService can't be created with properties : " + properties, e);
}
};
private ConfigurableEnvironment environment; private ConfigurableEnvironment environment;
private final NacosConfigEnvironmentProcessor processor;
private NacosConfigProperties nacosConfigProperties; private NacosConfigProperties nacosConfigProperties;
public NacosConfigApplicationContextInitializer( public NacosConfigApplicationInitializer(
NacosConfigEnvironmentProcessor configEnvironmentProcessor) { NacosConfigEnvironmentProcessor configEnvironmentProcessor) {
this.processor = configEnvironmentProcessor; this.processor = configEnvironmentProcessor;
} }
@Override @Override
public void initialize(ConfigurableApplicationContext context) { public void initialize(ConfigurableApplicationContext context) {
final CacheableEventPublishingNacosServiceFactory singleton = CacheableEventPublishingNacosServiceFactory
.getSingleton();
singleton.setApplicationContext(context); singleton.setApplicationContext(context);
environment = context.getEnvironment(); environment = context.getEnvironment();
nacosConfigProperties = NacosConfigPropertiesUtils nacosConfigProperties = NacosConfigPropertiesUtils
.buildNacosConfigProperties(environment); .buildNacosConfigProperties(environment);
final NacosConfigLoader configLoader = NacosConfigLoaderFactory.getSingleton(builder); processor.publishDeferService(context);
if (!processor.snapshotEnable()){
SnapShotSwitch.setIsSnapShot(false);
}
if (!enable()) { if (!enable()) {
logger.info("[Nacos Config Boot] : The preload configuration is not enabled"); logger.info("[Nacos Config Boot] : The preload configuration is not enabled");
} }
else { else {
final Function<Properties, ConfigService> builder = new Function<Properties, ConfigService>() {
// If it opens the log level loading directly will cache @Override
// DeferNacosPropertySource release public ConfigService apply(Properties input) {
try {
return singleton.createConfigService(input);
}
catch (NacosException e) {
throw new NacosBootConfigException(
"ConfigService can't be created with properties : "
+ input,
e);
}
}
};
final NacosConfigUtils configUtils = new NacosConfigUtils(
nacosConfigProperties, environment, builder);
if (processor.enable()) { if (processor.enable()) {
processor.publishDeferService(context); configUtils
configLoader
.addListenerIfAutoRefreshed(processor.getDeferPropertySources()); .addListenerIfAutoRefreshed(processor.getDeferPropertySources());
} }
else { else {
configLoader.loadConfig(environment, nacosConfigProperties); configUtils.loadConfig();
configLoader.addListenerIfAutoRefreshed(); configUtils.addListenerIfAutoRefreshed();
} }
} }
} }
private boolean enable() { private boolean enable() {
return processor.enable() || nacosConfigProperties.getBootstrap().isEnable(); return nacosConfigProperties.getBootstrap().isEnable();
} }
} }

View File

@ -16,7 +16,6 @@
*/ */
package com.alibaba.boot.nacos.config.autoconfigure; package com.alibaba.boot.nacos.config.autoconfigure;
import com.alibaba.boot.nacos.aot.context.EnableNacosConfigAotProcessor;
import com.alibaba.boot.nacos.config.NacosConfigConstants; import com.alibaba.boot.nacos.config.NacosConfigConstants;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig; import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
@ -37,10 +36,10 @@ import static com.alibaba.nacos.spring.util.NacosBeanUtils.CONFIG_GLOBAL_NACOS_P
*/ */
@ConditionalOnProperty(name = NacosConfigConstants.ENABLED, matchIfMissing = true) @ConditionalOnProperty(name = NacosConfigConstants.ENABLED, matchIfMissing = true)
@ConditionalOnMissingBean(name = CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME) @ConditionalOnMissingBean(name = CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME)
@EnableConfigurationProperties(value = NacosConfigProperties.class)
@ConditionalOnClass(name = "org.springframework.boot.context.properties.bind.Binder")
@Import(value = { NacosConfigBootBeanDefinitionRegistrar.class, EnableNacosConfigAotProcessor.class })
@EnableNacosConfig @EnableNacosConfig
@EnableConfigurationProperties(value = NacosConfigProperties.class)
@ConditionalOnClass(name = "org.springframework.boot.bind.RelaxedDataBinder")
@Import(value = { NacosConfigBootBeanDefinitionRegistrar.class })
public class NacosConfigAutoConfiguration { public class NacosConfigAutoConfiguration {
} }

View File

@ -30,7 +30,7 @@ import org.springframework.core.type.AnnotationMetadata;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.2 * @since 0.1.2
*/ */
@Configuration @Configuration
public class NacosConfigBootBeanDefinitionRegistrar public class NacosConfigBootBeanDefinitionRegistrar

View File

@ -20,21 +20,19 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.function.Function;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigLoader; import com.alibaba.boot.nacos.config.util.Function;
import com.alibaba.boot.nacos.config.util.NacosConfigLoaderFactory;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils; import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import com.alibaba.boot.nacos.config.util.log.LogAutoFreshProcess; import com.alibaba.boot.nacos.config.util.NacosConfigUtils;
import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory; import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.util.NacosUtils; import com.alibaba.nacos.spring.util.NacosUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -42,61 +40,62 @@ import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
/** /**
* In the Context to create premise before loading the log configuration information
*
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class NacosConfigEnvironmentProcessor public class NacosConfigEnvironmentProcessor
implements EnvironmentPostProcessor, Ordered { implements EnvironmentPostProcessor, Ordered {
private final Logger logger = LoggerFactory private final Logger logger = LoggerFactory
.getLogger(NacosConfigEnvironmentProcessor.class); .getLogger(NacosConfigEnvironmentProcessor.class);
private final CacheableEventPublishingNacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory private final CacheableEventPublishingNacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory
.getSingleton(); .getSingleton();
private final LinkedList<NacosConfigUtils.DeferNacosPropertySource> deferPropertySources = new LinkedList<>();
private final Map<String, ConfigService> serviceCache = new HashMap<>(8); private final Map<String, ConfigService> serviceCache = new HashMap<>(8);
private final LinkedList<NacosConfigLoader.DeferNacosPropertySource> deferPropertySources = new LinkedList<>();
private NacosConfigProperties nacosConfigProperties; private NacosConfigProperties nacosConfigProperties;
// Because ApplicationContext has not been injected at preload time, need to manually private final Function<Properties, ConfigService> builder = new Function<Properties, ConfigService>() {
// cache the created Service to prevent duplicate creation
private final Function<Properties, ConfigService> builder = properties -> { @Override
public ConfigService apply(Properties input) {
try { try {
final String key = NacosUtils.identify(properties); final String key = NacosUtils.identify(input);
if (serviceCache.containsKey(key)) { ConfigService service = serviceCache.get(key);
if (service != null) {
return serviceCache.get(key); return serviceCache.get(key);
} }
final ConfigService configService = NacosFactory service = NacosFactory.createConfigService(input);
.createConfigService(properties); serviceCache.put(key, service);
serviceCache.put(key, configService); return nacosServiceFactory.deferCreateService(service, input);
return nacosServiceFactory.deferCreateService(configService, properties);
} }
catch (NacosException e) { catch (NacosException e) {
throw new NacosBootConfigException( throw new NacosBootConfigException(
"ConfigService can't be created with properties : " + properties, e); "ConfigService can't be created with properties : " + input, e);
}
} }
}; };
@Override @Override
public void postProcessEnvironment(ConfigurableEnvironment environment, public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) { SpringApplication application) {
application.addInitializers(new NacosConfigApplicationContextInitializer(this)); application.addInitializers(new NacosConfigApplicationInitializer(this));
nacosConfigProperties = NacosConfigPropertiesUtils nacosConfigProperties = NacosConfigPropertiesUtils
.buildNacosConfigProperties(environment); .buildNacosConfigProperties(environment);
if (enable()) { if (enable()) {
System.out.println( System.out.println(
"[Nacos Config Boot] : The preload log configuration is enabled"); "[Nacos Config Boot] : The preload log configuration is enabled");
NacosConfigLoader nacosConfigLoader = NacosConfigLoaderFactory.getSingleton(builder); loadConfig(environment);
loadConfig(nacosConfigLoader, environment, nacosConfigProperties);
LogAutoFreshProcess.build(environment, nacosConfigProperties, nacosConfigLoader, builder).process();
} }
} }
private void loadConfig(NacosConfigLoader configLoader, ConfigurableEnvironment environment, NacosConfigProperties nacosConfigProperties) { private void loadConfig(ConfigurableEnvironment environment) {
configLoader.loadConfig(environment, nacosConfigProperties); final NacosConfigUtils configUtils = new NacosConfigUtils(nacosConfigProperties,
// set defer NacosPropertySource environment, builder);
deferPropertySources.addAll(configLoader.getNacosPropertySources()); configUtils.loadConfig();
// set defer nacosPropertySource
deferPropertySources.addAll(configUtils.getNacosPropertySources());
} }
boolean enable() { boolean enable() {
@ -104,29 +103,22 @@ public class NacosConfigEnvironmentProcessor
&& nacosConfigProperties.getBootstrap().isLogEnable(); && nacosConfigProperties.getBootstrap().isLogEnable();
} }
boolean snapshotEnable() { LinkedList<NacosConfigUtils.DeferNacosPropertySource> getDeferPropertySources() {
return nacosConfigProperties != null
&& nacosConfigProperties.getBootstrap().isSnapshotEnable();
}
LinkedList<NacosConfigLoader.DeferNacosPropertySource> getDeferPropertySources() {
return deferPropertySources; return deferPropertySources;
} }
// Do not set the minimum priority for future expansion needs
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 5;
}
void publishDeferService(ApplicationContext context) { void publishDeferService(ApplicationContext context) {
try { try {
nacosServiceFactory.publishDeferService(context); nacosServiceFactory.publishDeferService(context);
serviceCache.clear(); serviceCache.clear();
} }
catch (Exception e) { catch (Exception e) {
logger.error("publish defer ConfigService has some error", e); logger.error("publish defer ConfigService has some error : {}", e);
} }
} }
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
} }

View File

@ -16,74 +16,46 @@
*/ */
package com.alibaba.boot.nacos.config.binder; package com.alibaba.boot.nacos.config.binder;
import java.lang.reflect.Method; import java.util.Properties;
import com.alibaba.boot.nacos.config.util.BinderUtils;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties; import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.spring.context.properties.config.NacosConfigurationPropertiesBinder; import com.alibaba.nacos.spring.context.properties.config.NacosConfigurationPropertiesBinder;
import com.alibaba.nacos.spring.core.env.NacosPropertySource; import org.slf4j.Logger;
import com.alibaba.nacos.spring.util.ObjectUtils; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.StandardEnvironment; import static com.alibaba.nacos.spring.util.NacosUtils.toProperties;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.2 * @since 0.1.2
*/ */
public class NacosBootConfigurationPropertiesBinder public class NacosBootConfigurationPropertiesBinder
extends NacosConfigurationPropertiesBinder { extends NacosConfigurationPropertiesBinder {
private final ConfigurableApplicationContext applicationContext; private final Logger logger = LoggerFactory
.getLogger(NacosBootConfigurationPropertiesBinder.class);
private final StandardEnvironment environment = new StandardEnvironment(); private ConfigurableApplicationContext context;
public NacosBootConfigurationPropertiesBinder( public NacosBootConfigurationPropertiesBinder(
ConfigurableApplicationContext applicationContext) { ConfigurableApplicationContext applicationContext) {
super(applicationContext); super(applicationContext);
this.applicationContext = applicationContext; this.context = applicationContext;
} }
@Override @Override
protected void doBind(Object bean, String beanName, String dataId, String groupId, protected void doBind(Object bean, String beanName, String dataId, String groupId,
String configType, NacosConfigurationProperties properties, String content, String configType, NacosConfigurationProperties properties, String content,
ConfigService configService) { ConfigService configService) {
synchronized (this) { Properties prop = toProperties(dataId, groupId, content, configType);
String name = "nacos-bootstrap-" + beanName; BinderUtils.bind(bean, properties.prefix(), prop);
NacosPropertySource propertySource = new NacosPropertySource(dataId, groupId, name, content, configType); publishBoundEvent(bean, beanName, dataId, groupId, properties, content,
environment.getPropertySources().addLast(propertySource); configService);
ObjectUtils.cleanMapOrCollectionField(bean);
Binder binder = Binder.get(environment);
ResolvableType type = getBeanType(bean, beanName);
Bindable<?> target = Bindable.of(type).withExistingValue(bean);
binder.bind(properties.prefix(), target);
publishBoundEvent(bean, beanName, dataId, groupId, properties, content, configService);
publishMetadataEvent(bean, beanName, dataId, groupId, properties); publishMetadataEvent(bean, beanName, dataId, groupId, properties);
environment.getPropertySources().remove(name);
}
} }
private ResolvableType getBeanType(Object bean, String beanName) {
Method factoryMethod = findFactoryMethod(beanName);
if (factoryMethod != null) {
return ResolvableType.forMethodReturnType(factoryMethod);
}
return ResolvableType.forClass(bean.getClass());
}
public Method findFactoryMethod(String beanName) {
ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory();
if (beanFactory.containsBeanDefinition(beanName)) {
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
if (beanDefinition instanceof RootBeanDefinition) {
return ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod();
}
}
return null;
}
} }

View File

@ -1,67 +0,0 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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
*
* https://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.boot.nacos.config.logging;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import com.alibaba.nacos.client.logging.NacosLogging;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
/**
* Reload nacos log configuration file, after
* {@link org.springframework.boot.context.logging.LoggingApplicationListener}.
*
* @author mai.jh
*/
public class NacosLoggingListener implements GenericApplicationListener {
@Override
public boolean supportsEventType(ResolvableType resolvableType) {
Class<?> type = resolvableType.getRawClass();
if (type != null) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(type);
}
return false;
}
@Override
public boolean supportsSourceType(Class<?> aClass) {
return true;
}
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
//If the managed log is enabled, load your own log configuration after loading the user log configuration
ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent = (ApplicationEnvironmentPreparedEvent) applicationEvent;
NacosConfigProperties nacosConfigProperties = NacosConfigPropertiesUtils.buildNacosConfigProperties(
applicationEnvironmentPreparedEvent.getEnvironment());
if(nacosConfigProperties.getBootstrap().isLogEnable()){
NacosLogging.getInstance().loadConfiguration();
}
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 21;
}
}

View File

@ -18,13 +18,12 @@ package com.alibaba.boot.nacos.config.properties;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import com.alibaba.boot.nacos.config.NacosConfigConstants; import com.alibaba.boot.nacos.config.NacosConfigConstants;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.api.config.ConfigType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -61,7 +60,7 @@ public class NacosConfigProperties {
private String group = Constants.DEFAULT_GROUP; private String group = Constants.DEFAULT_GROUP;
private ConfigType type = ConfigType.PROPERTIES; private ConfigType type;
private String maxRetry; private String maxRetry;
@ -71,34 +70,12 @@ public class NacosConfigProperties {
private boolean enableRemoteSyncConfig = false; private boolean enableRemoteSyncConfig = false;
private String username; @JSONField(serialize = false)
private String password;
private boolean remoteFirst = false;
@JsonIgnore
private List<Config> extConfig = new ArrayList<>(); private List<Config> extConfig = new ArrayList<>();
@NestedConfigurationProperty @NestedConfigurationProperty
private Bootstrap bootstrap = new Bootstrap(); private Bootstrap bootstrap = new Bootstrap();
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getServerAddr() { public String getServerAddr() {
return serverAddr; return serverAddr;
} }
@ -236,14 +213,6 @@ public class NacosConfigProperties {
this.enableRemoteSyncConfig = enableRemoteSyncConfig; this.enableRemoteSyncConfig = enableRemoteSyncConfig;
} }
public boolean isRemoteFirst() {
return remoteFirst;
}
public void setRemoteFirst(boolean remoteFirst) {
this.remoteFirst = remoteFirst;
}
public List<Config> getExtConfig() { public List<Config> getExtConfig() {
return extConfig; return extConfig;
} }
@ -260,39 +229,11 @@ public class NacosConfigProperties {
this.bootstrap = bootstrap; this.bootstrap = bootstrap;
} }
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("NacosConfigProperties{");
sb.append("serverAddr='").append(serverAddr).append('\'');
sb.append(", contextPath='").append(contextPath).append('\'');
sb.append(", encode='").append(encode).append('\'');
sb.append(", endpoint='").append(endpoint).append('\'');
sb.append(", namespace='").append(namespace).append('\'');
sb.append(", accessKey='").append(Objects.isNull(accessKey) ? null : "******").append('\'');
sb.append(", secretKey='").append(Objects.isNull(secretKey) ? null : "******").append('\'');
sb.append(", ramRoleName='").append(ramRoleName).append('\'');
sb.append(", autoRefresh=").append(autoRefresh);
sb.append(", dataId='").append(dataId).append('\'');
sb.append(", dataIds='").append(dataIds).append('\'');
sb.append(", group='").append(group).append('\'');
sb.append(", type=").append(type);
sb.append(", maxRetry='").append(maxRetry).append('\'');
sb.append(", configLongPollTimeout='").append(configLongPollTimeout).append('\'');
sb.append(", configRetryTime='").append(configRetryTime).append('\'');
sb.append(", enableRemoteSyncConfig=").append(enableRemoteSyncConfig);
sb.append(", extConfig=").append(extConfig);
sb.append(", bootstrap=").append(bootstrap);
sb.append('}');
return sb.toString();
}
public static class Bootstrap { public static class Bootstrap {
private boolean enable; private boolean enable = false;
private boolean logEnable; private boolean logEnable = false;
private boolean snapshotEnable;
public boolean isEnable() { public boolean isEnable() {
return enable; return enable;
@ -309,29 +250,11 @@ public class NacosConfigProperties {
public void setLogEnable(boolean logEnable) { public void setLogEnable(boolean logEnable) {
this.logEnable = logEnable; this.logEnable = logEnable;
} }
public boolean isSnapshotEnable() {
return snapshotEnable;
}
public void setSnapshotEnable(boolean snapshotEnable) {
this.snapshotEnable = snapshotEnable;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Bootstrap{");
sb.append("enable=").append(enable);
sb.append(", snapshotEnable=").append(snapshotEnable);
sb.append(", logEnable=").append(logEnable);
sb.append('}');
return sb.toString();
}
} }
public static class Config { public static class Config {
private String serverAddr; private String serverAddr = "127.0.0.1:8848";
private String endpoint; private String endpoint;
@ -361,26 +284,6 @@ public class NacosConfigProperties {
private boolean enableRemoteSyncConfig = false; private boolean enableRemoteSyncConfig = false;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getServerAddr() { public String getServerAddr() {
return serverAddr; return serverAddr;
} }
@ -500,28 +403,5 @@ public class NacosConfigProperties {
public void setEnableRemoteSyncConfig(boolean enableRemoteSyncConfig) { public void setEnableRemoteSyncConfig(boolean enableRemoteSyncConfig) {
this.enableRemoteSyncConfig = enableRemoteSyncConfig; this.enableRemoteSyncConfig = enableRemoteSyncConfig;
} }
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Config{");
sb.append("serverAddr='").append(serverAddr).append('\'');
sb.append(", endpoint='").append(endpoint).append('\'');
sb.append(", namespace='").append(namespace).append('\'');
sb.append(", accessKey='").append(Objects.isNull(accessKey) ? null : "******").append('\'');
sb.append(", secretKey='").append(Objects.isNull(secretKey) ? null : "******").append('\'');
sb.append(", ramRoleName='").append(ramRoleName).append('\'');
sb.append(", dataId='").append(dataId).append('\'');
sb.append(", dataIds='").append(dataIds).append('\'');
sb.append(", group='").append(group).append('\'');
sb.append(", type=").append(type);
sb.append(", maxRetry='").append(maxRetry).append('\'');
sb.append(", configLongPollTimeout='").append(configLongPollTimeout)
.append('\'');
sb.append(", configRetryTime='").append(configRetryTime).append('\'');
sb.append(", autoRefresh=").append(autoRefresh);
sb.append(", enableRemoteSyncConfig=").append(enableRemoteSyncConfig);
sb.append('}');
return sb.toString();
}
} }
} }

View File

@ -1,95 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.config.support;
import com.alibaba.nacos.spring.core.env.AbstractNacosPropertySourceBuilder;
import com.alibaba.nacos.spring.core.env.NacosPropertySource;
import com.alibaba.nacos.spring.util.parse.DefaultYamlConfigParse;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
/**
* yaml multi profiles.
* <p>
* read nacos propertysource:
* <p>
* 1. {@link AbstractNacosPropertySourceBuilder#doBuild(String, BeanDefinition, Map)}<br/>
* 2. {@link NacosPropertySource#NacosPropertySource(String, String, String, String, String)}<br/>
* 3. {@link com.alibaba.nacos.spring.util.NacosUtils#toProperties(String, String, String, String)}<br/>
* 4. {@link com.alibaba.nacos.spring.util.ConfigParseUtils#toProperties(String, String, String, String)}<br/>
*
* @author <a href="mailto:yanglu_u@126.com">dbses</a>
*/
public class MultiProfilesYamlConfigParseSupport extends DefaultYamlConfigParse implements EnvironmentPostProcessor {
private static final String SPRING_PROFILES = "spring.profiles";
private static String[] profileArray = {};
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String[] profiles = environment.getActiveProfiles();
// fall back to default profiles
if (profiles.length == 0) {
profiles = environment.getDefaultProfiles();
}
// set once
if (profileArray.length == 0) {
profileArray = profiles;
}
}
@Override
public Map<String, Object> parse(String configText) {
final AtomicReference<Map<String, Object>> result = new AtomicReference<>();
process(map -> {
// first block
if (result.get() == null) {
result.set(map);
} else {
setFromOtherBlock(result, map);
}
}, createYaml(), configText);
return result.get();
}
private void setFromOtherBlock(AtomicReference<Map<String, Object>> result, Map<String, Object> map) {
if (map.get(SPRING_PROFILES) == null) {
result.get().putAll(map);
return;
}
for (String profile : profileArray) {
if (profile.equals(map.get(SPRING_PROFILES))) {
result.get().putAll(map);
}
}
}
/**
* for unit test
*/
static void setProfileArray(String[] profiles) {
profileArray = profiles;
}
}

View File

@ -31,9 +31,9 @@ import org.springframework.core.env.PropertySource;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class AttributeExtractTask implements Callable<Map<String, String>> { public class AttributeExtractTask implements Callable<Map<String, Object>> {
private final String prefix; private final String prefix;
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
@ -44,33 +44,33 @@ public class AttributeExtractTask implements Callable<Map<String, String>> {
} }
@Override @Override
public Map<String, String> call() throws Exception { public Map<String, Object> call() throws Exception {
List<Map<String, String>> defer = new LinkedList<>(); List<Map<String, Object>> defer = new LinkedList<>();
MutablePropertySources mutablePropertySources = environment.getPropertySources();
MutablePropertySources mutablePropertySources = environment.getPropertySources();
for (PropertySource propertySource : mutablePropertySources) { for (PropertySource propertySource : mutablePropertySources) {
calculate(propertySource.getSource(), defer); calculate(propertySource.getSource(), defer);
} }
Map<String, String> result = new HashMap<>(32); Map<String, Object> result = new HashMap<>(32);
Collections.reverse(defer); Collections.reverse(defer);
for (Map<String, String> item : defer) { for (Map<String, Object> item : defer) {
result.putAll(item); result.putAll(item);
} }
return result; return result;
} }
private void calculate(Object source, List<Map<String, String>> defer) { private void calculate(Object source, List<Map<String, Object>> defer) {
if (source instanceof PropertySource) { if (source instanceof PropertySource) {
calculate(((PropertySource) source).getSource(), defer); calculate(((PropertySource) source).getSource(), defer);
} }
if (source instanceof Map) { if (source instanceof Map) {
Map<String, String> map = new HashMap<>(8); Map<String, Object> map = new HashMap<>(8);
for (Object entry : ((Map) source).entrySet()) { for (Object entry : ((Map) source).entrySet()) {
Map.Entry<Object, Object> element = (Map.Entry<Object, Object>) entry; Map.Entry<Object, Object> element = (Map.Entry<Object, Object>) entry;
String key = String.valueOf(element.getKey()); String key = String.valueOf(element.getKey());
if (key.startsWith(prefix)) { if (key.startsWith(prefix)) {
map.put(key, String.valueOf(element.getValue())); map.put(key, element.getValue());
} }
} }
if (!map.isEmpty()) { if (!map.isEmpty()) {

View File

@ -14,33 +14,23 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.boot.nacos.config.util.editor; package com.alibaba.boot.nacos.config.util;
import java.beans.PropertyEditorSupport; import java.util.Properties;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.4
*/ */
public class NacosCharSequenceEditor extends PropertyEditorSupport { public final class BinderUtils {
@Override public static <T> T bind(T obj, String prefix, Properties properties) {
public void setValue(Object value) { RelaxedDataBinder binder = new RelaxedDataBinder(obj, prefix);
if (value == null) { binder.bind(new MutablePropertyValues(properties));
super.setValue(""); return obj;
}
if (value instanceof CharSequence) {
CharSequence sequence = (CharSequence) value;
super.setValue(sequence.toString());
}
else {
super.setValue(value);
}
} }
@Override
public String getAsText() {
Object value = getValue();
return String.valueOf(value);
}
} }

View File

@ -14,33 +14,20 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.boot.nacos.sample; package com.alibaba.boot.nacos.config.util;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.context.annotation.Configuration;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since * @since 0.1.4
*/ */
@Configuration public interface Function<F, T> {
public class TestConfiguration {
@NacosValue(value = "${people.count:0}", autoRefreshed = true) /**
private String count; * apply F to T
*
* @param input input
* @return T
*/
T apply(F input);
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
@NacosConfigListener(dataId = "listener.test", timeout = 500)
public void onChange(String newContent) throws Exception {
System.out.println("onChange : " + newContent);
}
} }

View File

@ -1,44 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.config.util;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Properties;
import java.util.function.Function;
/**
* NacosConfigLoaderFactory.
* @author hujun
*/
public class NacosConfigLoaderFactory {
private static volatile NacosConfigLoader nacosConfigLoader;
public static NacosConfigLoader getSingleton(Function<Properties, ConfigService> builder) {
if (nacosConfigLoader == null) {
synchronized (NacosConfigLoaderFactory.class) {
if (nacosConfigLoader == null) {
nacosConfigLoader = new NacosConfigLoader(builder);
}
}
}
return nacosConfigLoader;
}
}

View File

@ -16,40 +16,55 @@
*/ */
package com.alibaba.boot.nacos.config.util; package com.alibaba.boot.nacos.config.util;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import com.alibaba.boot.nacos.config.NacosConfigConstants; import com.alibaba.boot.nacos.config.NacosConfigConstants;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Properties;
import java.util.function.Function;
/** /**
* Springboot used to own property binding configured binding
*
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class NacosConfigPropertiesUtils { public class NacosConfigPropertiesUtils {
private static final String PROPERTIES_PREFIX = "nacos";
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(NacosConfigPropertiesUtils.class); .getLogger(NacosConfigPropertiesUtils.class);
private static Set<String> OBJ_FIELD_NAME = new HashSet<>();
static {
Field[] fields = NacosConfigProperties.class.getDeclaredFields();
for (Field field : fields) {
OBJ_FIELD_NAME.add(field.getName());
}
}
public static NacosConfigProperties buildNacosConfigProperties( public static NacosConfigProperties buildNacosConfigProperties(
ConfigurableEnvironment environment) { ConfigurableEnvironment environment) {
NacosConfigProperties nacosConfigProperties = new NacosConfigProperties(); NacosConfigProperties bean = new NacosConfigProperties();
Binder binder = Binder.get(environment);
ResolvableType type = ResolvableType.forClass(NacosConfigProperties.class); AttributeExtractTask task = new AttributeExtractTask(PROPERTIES_PREFIX,
Bindable<?> target = Bindable.of(type).withExistingValue(nacosConfigProperties); environment);
binder.bind(NacosConfigConstants.PREFIX, target);
logger.info("nacosConfigProperties : {}", nacosConfigProperties); try {
return nacosConfigProperties; Properties properties = new Properties();
properties.putAll(task.call());
BinderUtils.bind(bean, NacosConfigConstants.PREFIX, properties);
}
catch (Exception e) {
throw new RuntimeException(e);
}
logger.info("nacosConfigProperties : {}", bean);
return bean;
} }
} }

View File

@ -20,10 +20,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.ListIterator;
import java.util.Properties; import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
@ -31,57 +28,53 @@ import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.spring.core.env.NacosPropertySource; import com.alibaba.nacos.spring.core.env.NacosPropertySource;
import com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor; import com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor;
import com.alibaba.nacos.spring.util.NacosUtils; import com.alibaba.nacos.spring.util.NacosUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.StringUtils;
import static com.alibaba.nacos.spring.util.NacosUtils.buildDefaultPropertySourceName; import static com.alibaba.nacos.spring.util.NacosUtils.buildDefaultPropertySourceName;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @author <a href="mailto:guofuyinan@gmail.com">guofuyinan</a> * @since 0.1.3
* @since 0.2.3
*/ */
public class NacosConfigLoader { public class NacosConfigUtils {
private final Logger logger = LoggerFactory.getLogger(NacosConfigLoader.class); private final Logger logger = LoggerFactory.getLogger(NacosConfigUtils.class);
private Properties globalProperties = new Properties(); private final NacosConfigProperties nacosConfigProperties;
private final ConfigurableEnvironment environment;
private Function<Properties, ConfigService> builder;
private List<DeferNacosPropertySource> nacosPropertySources = new LinkedList<>();
private final Function<Properties, ConfigService> builder; public NacosConfigUtils(NacosConfigProperties nacosConfigProperties,
private final List<DeferNacosPropertySource> nacosPropertySources = new LinkedList<>(); ConfigurableEnvironment environment,
Function<Properties, ConfigService> builder) {
public NacosConfigLoader(Function<Properties, ConfigService> builder) { this.nacosConfigProperties = nacosConfigProperties;
this.environment = environment;
this.builder = builder; this.builder = builder;
} }
public void loadConfig(ConfigurableEnvironment environment, NacosConfigProperties nacosConfigProperties) { public void loadConfig() {
globalProperties = buildGlobalNacosProperties(environment, nacosConfigProperties); Properties globalProperties = buildGlobalNacosProperties();
MutablePropertySources mutablePropertySources = environment.getPropertySources(); MutablePropertySources mutablePropertySources = environment.getPropertySources();
List<NacosPropertySource> sources = reqGlobalNacosConfig(environment, globalProperties, nacosConfigProperties); List<NacosPropertySource> sources = reqGlobalNacosConfig(globalProperties,
nacosConfigProperties.getType());
for (NacosConfigProperties.Config config : nacosConfigProperties.getExtConfig()) { for (NacosConfigProperties.Config config : nacosConfigProperties.getExtConfig()) {
List<NacosPropertySource> elements = reqSubNacosConfig(environment, config, List<NacosPropertySource> elements = reqSubNacosConfig(config,
globalProperties, config.getType()); globalProperties, config.getType());
sources.addAll(elements); sources.addAll(sources.size(), elements);
} }
if (nacosConfigProperties.isRemoteFirst()) {
for (ListIterator<NacosPropertySource> itr = sources.listIterator(sources.size()); itr.hasPrevious();) {
mutablePropertySources.addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, itr.previous());
}
} else {
for (NacosPropertySource propertySource : sources) { for (NacosPropertySource propertySource : sources) {
mutablePropertySources.addLast(propertySource); mutablePropertySources.addLast(propertySource);
} }
} }
}
public Properties buildGlobalNacosProperties(ConfigurableEnvironment environment, NacosConfigProperties nacosConfigProperties) { private Properties buildGlobalNacosProperties() {
return NacosPropertiesBuilder.buildNacosProperties(environment, return NacosPropertiesBuilder.buildNacosProperties(
nacosConfigProperties.getServerAddr(), nacosConfigProperties.getServerAddr(),
nacosConfigProperties.getNamespace(), nacosConfigProperties.getEndpoint(), nacosConfigProperties.getNamespace(), nacosConfigProperties.getEndpoint(),
nacosConfigProperties.getSecretKey(), nacosConfigProperties.getSecretKey(),
@ -90,35 +83,36 @@ public class NacosConfigLoader {
nacosConfigProperties.getConfigLongPollTimeout(), nacosConfigProperties.getConfigLongPollTimeout(),
nacosConfigProperties.getConfigRetryTime(), nacosConfigProperties.getConfigRetryTime(),
nacosConfigProperties.getMaxRetry(), nacosConfigProperties.getMaxRetry(),
nacosConfigProperties.getContextPath(), nacosConfigProperties.isEnableRemoteSyncConfig());
nacosConfigProperties.isEnableRemoteSyncConfig(),
nacosConfigProperties.getUsername(), nacosConfigProperties.getPassword());
} }
private Properties buildSubNacosProperties(ConfigurableEnvironment environment, Properties globalProperties, private Properties buildSubNacosProperties(Properties globalProperties,
NacosConfigProperties.Config config) { NacosConfigProperties.Config config) {
Properties sub = NacosPropertiesBuilder.buildNacosProperties(environment, return getProperties(globalProperties, config);
}
private static Properties getProperties(Properties globalProperties,
NacosConfigProperties.Config config) {
if (StringUtils.isEmpty(config.getServerAddr())) {
return globalProperties;
}
Properties sub = NacosPropertiesBuilder.buildNacosProperties(
config.getServerAddr(), config.getNamespace(), config.getEndpoint(), config.getServerAddr(), config.getNamespace(), config.getEndpoint(),
config.getSecretKey(), config.getAccessKey(), config.getRamRoleName(), config.getSecretKey(), config.getAccessKey(), config.getRamRoleName(),
config.getConfigLongPollTimeout(), config.getConfigRetryTime(), config.getConfigLongPollTimeout(), config.getConfigRetryTime(),
config.getMaxRetry(),null, config.isEnableRemoteSyncConfig(), config.getMaxRetry(), config.isEnableRemoteSyncConfig());
config.getUsername(), config.getPassword());
NacosPropertiesBuilder.merge(sub, globalProperties); NacosPropertiesBuilder.merge(sub, globalProperties);
return sub; return sub;
} }
private List<NacosPropertySource> reqGlobalNacosConfig(ConfigurableEnvironment environment, Properties globalProperties, private List<NacosPropertySource> reqGlobalNacosConfig(Properties globalProperties,
NacosConfigProperties nacosConfigProperties) { ConfigType type) {
List<String> dataIds = new ArrayList<>(); List<String> dataIds = new ArrayList<>();
// Loads all data-id information into the list in the list // Loads all data-id information into the list in the list
if (!StringUtils.hasLength(nacosConfigProperties.getDataId())) { if (StringUtils.isEmpty(nacosConfigProperties.getDataId())) {
final String ids = environment final String ids = environment
.resolvePlaceholders(nacosConfigProperties.getDataIds()); .resolvePlaceholders(nacosConfigProperties.getDataIds());
if(StringUtils.hasText(ids)){ dataIds.addAll(Arrays.asList(ids.split(",")));
dataIds.addAll(Arrays.stream(ids.split(","))
.filter(StringUtils::hasText)
.collect(Collectors.toList()));
}
} }
else { else {
dataIds.add(nacosConfigProperties.getDataId()); dataIds.add(nacosConfigProperties.getDataId());
@ -126,18 +120,18 @@ public class NacosConfigLoader {
final String groupName = environment final String groupName = environment
.resolvePlaceholders(nacosConfigProperties.getGroup()); .resolvePlaceholders(nacosConfigProperties.getGroup());
final boolean isAutoRefresh = nacosConfigProperties.isAutoRefresh(); final boolean isAutoRefresh = nacosConfigProperties.isAutoRefresh();
return new ArrayList<>(Arrays.asList(reqNacosConfig(environment, globalProperties, return new ArrayList<>(Arrays.asList(reqNacosConfig(globalProperties,
dataIds.toArray(new String[0]), groupName, nacosConfigProperties.getType(), isAutoRefresh))); dataIds.toArray(new String[0]), groupName, type, isAutoRefresh)));
} }
private List<NacosPropertySource> reqSubNacosConfig(ConfigurableEnvironment environment, private List<NacosPropertySource> reqSubNacosConfig(
NacosConfigProperties.Config config, Properties globalProperties, NacosConfigProperties.Config config, Properties globalProperties,
ConfigType type) { ConfigType type) {
Properties subConfigProperties = buildSubNacosProperties(environment, globalProperties, Properties subConfigProperties = buildSubNacosProperties(globalProperties,
config); config);
ArrayList<String> dataIds = new ArrayList<>(); ArrayList<String> dataIds = new ArrayList<>();
if (!StringUtils.hasLength(config.getDataId())) { if (StringUtils.isEmpty(config.getDataId())) {
final String ids = environment.resolvePlaceholders(config.getDataIds()); final String ids = environment.resolvePlaceholders(config.getDataId());
dataIds.addAll(Arrays.asList(ids.split(","))); dataIds.addAll(Arrays.asList(ids.split(",")));
} }
else { else {
@ -145,18 +139,17 @@ public class NacosConfigLoader {
} }
final String groupName = environment.resolvePlaceholders(config.getGroup()); final String groupName = environment.resolvePlaceholders(config.getGroup());
final boolean isAutoRefresh = config.isAutoRefresh(); final boolean isAutoRefresh = config.isAutoRefresh();
return new ArrayList<>(Arrays.asList(reqNacosConfig(environment, subConfigProperties, return new ArrayList<>(Arrays.asList(reqNacosConfig(subConfigProperties,
dataIds.toArray(new String[0]), groupName, type, isAutoRefresh))); dataIds.toArray(new String[0]), groupName, type, isAutoRefresh)));
} }
private NacosPropertySource[] reqNacosConfig(ConfigurableEnvironment environment, Properties configProperties, private NacosPropertySource[] reqNacosConfig(Properties configProperties,
String[] dataIds, String groupId, ConfigType type, boolean isAutoRefresh) { String[] dataIds, String groupId, ConfigType type, boolean isAutoRefresh) {
final NacosPropertySource[] propertySources = new NacosPropertySource[dataIds.length]; final NacosPropertySource[] propertySources = new NacosPropertySource[dataIds.length];
for (int i = 0; i < dataIds.length; i++) { for (int i = 0; i < dataIds.length; i++) {
if (!StringUtils.hasLength(dataIds[i])) { if (StringUtils.isEmpty(dataIds[i])) {
continue; continue;
} }
// Remove excess Spaces
final String dataId = environment.resolvePlaceholders(dataIds[i].trim()); final String dataId = environment.resolvePlaceholders(dataIds[i].trim());
final String config = NacosUtils.getContent(builder.apply(configProperties), final String config = NacosUtils.getContent(builder.apply(configProperties),
dataId, groupId); dataId, groupId);
@ -168,8 +161,9 @@ public class NacosConfigLoader {
nacosPropertySource.setType(type.getType()); nacosPropertySource.setType(type.getType());
nacosPropertySource.setGroupId(groupId); nacosPropertySource.setGroupId(groupId);
nacosPropertySource.setAutoRefreshed(isAutoRefresh); nacosPropertySource.setAutoRefreshed(isAutoRefresh);
logger.info("load config from nacos, data-id is : {}, group is : {}", logger.info(
nacosPropertySource.getDataId(), nacosPropertySource.getGroupId()); "load config from nacos, data-id is : [{}], group is : [{}], config-text is : [{}]",
dataId, groupId, config);
propertySources[i] = nacosPropertySource; propertySources[i] = nacosPropertySource;
DeferNacosPropertySource defer = new DeferNacosPropertySource( DeferNacosPropertySource defer = new DeferNacosPropertySource(
nacosPropertySource, configProperties, environment); nacosPropertySource, configProperties, environment);
@ -196,11 +190,6 @@ public class NacosConfigLoader {
return nacosPropertySources; return nacosPropertySources;
} }
public Properties getGlobalProperties() {
return globalProperties;
}
// Delay Nacos configuration data source object, used for log level of loading time, // Delay Nacos configuration data source object, used for log level of loading time,
// the cache configuration, wait for after the completion of the Spring Context // the cache configuration, wait for after the completion of the Spring Context
// created in the release // created in the release

View File

@ -22,45 +22,53 @@ import java.util.Properties;
import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.PropertyKeyConst;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class NacosPropertiesBuilder { public class NacosPropertiesBuilder {
public static Properties buildNacosProperties(Environment environment, public static Properties buildNacosProperties(String serverAddr, String namespaceId,
String serverAddr, String namespaceId, String endpoint, String secretKey, String endpoint, String secretKey, String accessKey, String ramRoleName,
String accessKey, String ramRoleName, String configLongPollTimeout, String configLongPollTimeout, String configRetryTimeout, String maxRetry,
String configRetryTimeout, String maxRetry,String contextPath, boolean enableRemoteSyncConfig, boolean enableRemoteSyncConfig) {
String username, String password) {
Properties properties = new Properties();
processPropertiesData(properties,environment,serverAddr,PropertyKeyConst.SERVER_ADDR);
processPropertiesData(properties,environment,namespaceId,PropertyKeyConst.NAMESPACE);
processPropertiesData(properties,environment,endpoint,PropertyKeyConst.ENDPOINT);
processPropertiesData(properties,environment,secretKey,PropertyKeyConst.SECRET_KEY);
processPropertiesData(properties,environment,accessKey,PropertyKeyConst.ACCESS_KEY);
processPropertiesData(properties,environment,ramRoleName,PropertyKeyConst.RAM_ROLE_NAME);
processPropertiesData(properties,environment,configLongPollTimeout,PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT);
processPropertiesData(properties,environment,configRetryTimeout,PropertyKeyConst.CONFIG_RETRY_TIME);
processPropertiesData(properties,environment,contextPath,PropertyKeyConst.CONTEXT_PATH);
processPropertiesData(properties,environment,maxRetry,PropertyKeyConst.MAX_RETRY);
processPropertiesData(properties,environment,username,PropertyKeyConst.USERNAME);
processPropertiesData(properties,environment,password,PropertyKeyConst.PASSWORD);
Properties properties = new Properties();
if (StringUtils.isNotEmpty(serverAddr)) {
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
}
if (StringUtils.isNotEmpty(namespaceId)) {
properties.put(PropertyKeyConst.NAMESPACE, namespaceId);
}
if (StringUtils.isNotEmpty(endpoint)) {
properties.put(PropertyKeyConst.ENDPOINT, endpoint);
}
if (StringUtils.isNotEmpty(secretKey)) {
properties.put(PropertyKeyConst.SECRET_KEY, secretKey);
}
if (StringUtils.isNotEmpty(accessKey)) {
properties.put(PropertyKeyConst.ACCESS_KEY, accessKey);
}
if (StringUtils.isNoneEmpty(ramRoleName)) {
properties.put(PropertyKeyConst.RAM_ROLE_NAME, ramRoleName);
}
if (StringUtils.isNotEmpty(configLongPollTimeout)) {
properties.put(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT,
configLongPollTimeout);
}
if (StringUtils.isNotEmpty(configRetryTimeout)) {
properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, configRetryTimeout);
}
if (StringUtils.isNotEmpty(maxRetry)) {
properties.put(PropertyKeyConst.MAX_RETRY, maxRetry);
}
properties.put(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG, properties.put(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG,
String.valueOf(enableRemoteSyncConfig)); String.valueOf(enableRemoteSyncConfig));
return properties; return properties;
} }
private static void processPropertiesData(Properties properties,Environment environment,String keyword,String key) {
if (StringUtils.isNotBlank(keyword)) {
properties.put(key ,environment.resolvePlaceholders(keyword));
}
}
public static void merge(Properties targetProperties, Properties sourceProperties) { public static void merge(Properties targetProperties, Properties sourceProperties) {
if (CollectionUtils.isEmpty(sourceProperties)) { if (CollectionUtils.isEmpty(sourceProperties)) {

View File

@ -0,0 +1,72 @@
/*
* 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 com.alibaba.boot.nacos.config.util.editor;
import java.beans.PropertyEditorSupport;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.1.3
*/
public class NacosBooleanEditor extends PropertyEditorSupport {
public NacosBooleanEditor() {
}
@Override
public String getJavaInitializationString() {
Object var1 = this.getValue();
return var1 != null ? var1.toString() : "null";
}
@Override
public String getAsText() {
Object var1 = this.getValue();
return var1 instanceof Boolean ? this.getValidName((Boolean) var1) : null;
}
@Override
public void setAsText(String var1) throws IllegalArgumentException {
if (var1 == null) {
this.setValue((Object) null);
}
else if (this.isValidName(true, var1)) {
this.setValue(Boolean.TRUE);
}
else {
if (!this.isValidName(false, var1)) {
throw new IllegalArgumentException(var1);
}
this.setValue(Boolean.FALSE);
}
}
@Override
public String[] getTags() {
return new String[] { this.getValidName(true), this.getValidName(false) };
}
private String getValidName(boolean var1) {
return var1 ? "True" : "False";
}
private boolean isValidName(boolean var1, String var2) {
return this.getValidName(var1).equalsIgnoreCase(var2);
}
}

View File

@ -1,107 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.config.util.editor;
import java.beans.PropertyEditorSupport;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3
*/
public class NacosCustomBooleanEditor extends PropertyEditorSupport {
public static final String VALUE_TRUE = "true";
public static final String VALUE_FALSE = "false";
public static final String VALUE_ON = "on";
public static final String VALUE_OFF = "off";
public static final String VALUE_YES = "yes";
public static final String VALUE_NO = "no";
public static final String VALUE_1 = "1";
public static final String VALUE_0 = "0";
@Nullable
private final String trueString;
@Nullable
private final String falseString;
private final boolean allowEmpty;
public NacosCustomBooleanEditor(boolean allowEmpty) {
this(null, null, allowEmpty);
}
public NacosCustomBooleanEditor(@Nullable String trueString,
@Nullable String falseString, boolean allowEmpty) {
this.trueString = trueString;
this.falseString = falseString;
this.allowEmpty = allowEmpty;
}
@Override
public void setValue(Object value) {
super.setValue(convert(String.valueOf(value)));
}
public Object convert(String text) throws IllegalArgumentException {
String input = (text != null ? text.trim() : null);
if (this.allowEmpty && !StringUtils.hasLength(input)) {
// Treat empty String as null value.
return null;
}
else if (this.trueString != null && this.trueString.equalsIgnoreCase(input)) {
return Boolean.TRUE;
}
else if (this.falseString != null && this.falseString.equalsIgnoreCase(input)) {
return Boolean.FALSE;
}
else if (this.trueString == null
&& (VALUE_TRUE.equalsIgnoreCase(input) || VALUE_ON.equalsIgnoreCase(input)
|| VALUE_YES.equalsIgnoreCase(input) || VALUE_1.equals(input))) {
return Boolean.TRUE;
}
else if (this.falseString == null && (VALUE_FALSE.equalsIgnoreCase(input)
|| VALUE_OFF.equalsIgnoreCase(input) || VALUE_NO.equalsIgnoreCase(input)
|| VALUE_0.equals(input))) {
return Boolean.FALSE;
}
else {
throw new IllegalArgumentException("Invalid boolean value [" + text + "]");
}
}
@Override
public String getAsText() {
String t = String.valueOf(getValue());
if (Boolean.TRUE.equals(Boolean.valueOf(t))) {
return (this.trueString != null ? this.trueString : VALUE_TRUE);
}
else if (Boolean.FALSE.equals(Boolean.valueOf(t))) {
return (this.falseString != null ? this.falseString : VALUE_FALSE);
}
else {
return "";
}
}
}

View File

@ -25,11 +25,11 @@ import java.util.List;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class NacosEnumEditor implements PropertyEditor { public class NacosEnumEditor implements PropertyEditor {
private final List<PropertyChangeListener> listeners = new ArrayList<>(8); private final List<PropertyChangeListener> listeners = new ArrayList();
private final Class type; private final Class type;
private final String[] tags; private final String[] tags;
private Object value; private Object value;
@ -153,4 +153,5 @@ public class NacosEnumEditor implements PropertyEditor {
this.listeners.remove(var1); this.listeners.remove(var1);
} }
} }
} }

View File

@ -0,0 +1,96 @@
/*
* 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 com.alibaba.boot.nacos.config.util.editor;
import java.beans.PropertyEditorSupport;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.1.3
*/
public class NacosStringEditor extends PropertyEditorSupport {
public NacosStringEditor() {
}
@Override
public String getJavaInitializationString() {
Object var1 = this.getValue();
if (var1 == null) {
return "null";
}
else {
String var2 = var1.toString();
int var3 = var2.length();
StringBuilder var4 = new StringBuilder(var3 + 2);
var4.append('"');
for (int var5 = 0; var5 < var3; ++var5) {
char var6 = var2.charAt(var5);
String var7;
int var8;
switch (var6) {
case '\b':
var4.append("\\b");
continue;
case '\t':
var4.append("\\t");
continue;
case '\n':
var4.append("\\n");
continue;
case '\f':
var4.append("\\f");
continue;
case '\r':
var4.append("\\r");
continue;
case '"':
var4.append("\\\"");
continue;
case '\\':
var4.append("\\\\");
continue;
default:
if (var6 >= ' ' && var6 <= '~') {
var4.append(var6);
continue;
}
var4.append("\\u");
var7 = Integer.toHexString(var6);
var8 = var7.length();
}
while (var8 < 4) {
var4.append('0');
++var8;
}
var4.append(var7);
}
var4.append('"');
return var4.toString();
}
}
@Override
public void setAsText(String var1) {
this.setValue(var1);
}
}

View File

@ -1,149 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.config.util.log;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigLoader;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.utils.ConcurrentDiskUtil;
import com.alibaba.nacos.client.config.utils.JvmUtil;
import com.alibaba.nacos.client.logging.NacosLogging;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.spring.util.NacosUtils;
import org.slf4j.Logger;
import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemFactory;
import org.springframework.core.env.ConfigurableEnvironment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
/**
* Start:
* Step1: get the log XML configuration from the configuration center
* Step2: modify the springboot log configuration path
* Modifying log configuration during operation:
* Clean up the configuration through LoggingSystem and reload the configuration.
*
* @author <a href="mailto:hujun3@xiaomi.com">hujun</a>
*/
public class LogAutoFreshProcess {
private static final Logger LOGGER = LogUtils.logger(LogAutoFreshProcess.class);
private final NacosConfigProperties nacosConfigProperties;
private final ConfigurableEnvironment environment;
private final NacosConfigLoader nacosConfigLoader;
private final Function<Properties, ConfigService> builder;
private static final List<String> LOG_DATA_ID = new ArrayList<>();
private static final String LOG_CACHE_BASE = System.getProperty("JM.SNAPSHOT.PATH", System.getProperty("user.home")) + File.separator + "nacos"
+ File.separator + "logConfig";
static {
LOG_DATA_ID.add("logback.xml");
LOG_DATA_ID.add("log4j2.xml");
}
public static LogAutoFreshProcess build(ConfigurableEnvironment environment, NacosConfigProperties nacosConfigProperties, NacosConfigLoader nacosConfigLoader, Function<Properties, ConfigService> builder) {
return new LogAutoFreshProcess(environment, nacosConfigProperties, nacosConfigLoader, builder);
}
private LogAutoFreshProcess(ConfigurableEnvironment environment, NacosConfigProperties nacosConfigProperties, NacosConfigLoader nacosConfigLoader, Function<Properties, ConfigService> builder) {
this.nacosConfigProperties = nacosConfigProperties;
this.environment = environment;
this.nacosConfigLoader = nacosConfigLoader;
this.builder = builder;
}
public void process() {
final String groupName = environment
.resolvePlaceholders(nacosConfigProperties.getGroup());
ConfigService configService = builder.apply(nacosConfigLoader.getGlobalProperties());
for (String dataId : LOG_DATA_ID) {
String content = NacosUtils.getContent(configService, dataId, groupName);
if (StringUtils.isNotBlank(content)) {
writeLogFile(content, dataId);
System.setProperty(LoggingApplicationListener.CONFIG_PROPERTY, LOG_CACHE_BASE + File.separator + dataId);
registerListener(configService, dataId, groupName);
return;
}
}
}
private void registerListener(ConfigService configService, String dataId, String groupName) {
try {
configService.addListener(dataId, groupName, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
if (StringUtils.isNotBlank(configInfo)) {
writeLogFile(configInfo, dataId);
reloadConfig(LOG_CACHE_BASE + File.separator + dataId);
}
}
});
} catch (NacosException e) {
throw new RuntimeException("ConfigService can't add Listener with dataId : " + dataId, e);
}
}
private void writeLogFile(String content, String dataId) {
File file = new File(LOG_CACHE_BASE, dataId);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
boolean isMdOk = parentFile.mkdirs();
if (!isMdOk) {
LOGGER.error("save log cache error");
}
}
try {
if (JvmUtil.isMultiInstance()) {
ConcurrentDiskUtil.writeFileContent(file, content, Constants.ENCODE);
} else {
IoUtils.writeStringToFile(file, content, Constants.ENCODE);
}
} catch (IOException e) {
throw new RuntimeException("write log file fail");
}
}
private void reloadConfig(String logPath) {
LoggingSystem loggingSystem = LoggingSystemFactory.fromSpringFactories()
.getLoggingSystem(this.getClass().getClassLoader());
loggingSystem.cleanUp();
loggingSystem.initialize(new LoggingInitializationContext(environment),
logPath, null);
NacosLogging.getInstance().loadConfiguration();
}
}

View File

@ -1,9 +0,0 @@
{
"resources": {
"includes": [
{
"pattern": "\\QMETA-INF/services/com.alibaba.nacos.spring.util.ConfigParse\\E"
}
]
}
}

View File

@ -1 +0,0 @@
com.alibaba.boot.nacos.config.support.MultiProfilesYamlConfigParseSupport

View File

@ -0,0 +1,127 @@
{
"hints": [ ],
"groups": [ ],
"properties": [
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.enabled",
"description": "enable or disable nacos config feature",
"type": "java.lang.Boolean"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.server-addr",
"description": "the server address of nacos",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.context-path",
"description": "the context path of nacos server",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.encode",
"description": "encode of reading nacos properties",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.endpoint",
"description": "the entry domain name of a service in each region",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.namespace",
"description": "for configuration isolation by tenants. Different namespaces may have configurations with the same Group or Data ID. ",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.access-key",
"description": "the accesskey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.secret-key",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.ram-role-name",
"description": "the ramRoleName of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.data-id",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.data-ids",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.group",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.auto-refresh",
"type": "java.lang.Boolean"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.max-retry",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.config-long-poll-timeout",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.config-retry-time",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.type",
"description": "the secretkey of Alibaba Cloud Account",
"type": "com.alibaba.nacos.api.config.ConfigType"
},{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.enable-remote-sync-config",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.Boolean"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.bootstrap.enable",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.bootstrap.log.enable",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.config.properties.NacosConfigProperties",
"name": "nacos.config.ext-config",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.util.List<com.alibaba.boot.nacos.config.properties.NacosConfigProperties.Config>"
}
]
}

View File

@ -1,9 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration
#org.springframework.context.ApplicationContextInitializer=\
# com.alibaba.boot.nacos.config.autoconfigure.NacosConfigApplicationContextInitializer
org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.boot.env.EnvironmentPostProcessor=\
com.alibaba.boot.nacos.config.autoconfigure.NacosConfigEnvironmentProcessor,\ com.alibaba.boot.nacos.config.autoconfigure.NacosConfigEnvironmentProcessor
com.alibaba.boot.nacos.config.support.MultiProfilesYamlConfigParseSupport
org.springframework.context.ApplicationListener=\
com.alibaba.boot.nacos.config.logging.NacosLoggingListener

View File

@ -1,4 +0,0 @@
com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration
com.alibaba.boot.nacos.config.autoconfigure.NacosConfigEnvironmentProcessor
com.alibaba.boot.nacos.config.support.MultiProfilesYamlConfigParseSupport
com.alibaba.boot.nacos.config.logging.NacosLoggingListener

View File

@ -1,46 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.autoconfigure;
import com.alibaba.boot.nacos.config.autoconfigure.NacosBootConfigException;
import org.junit.Assert;
import org.junit.Test;
/**
* {@link NacosBootConfigException} Test
* @ClassName: NacosBootConfigExceptionTest
* @Author: ChenHao26
* @Date: 2022/8/12 15:56
*/
public class NacosBootConfigExceptionTest {
@Test
public void getCons() {
try {
testMethod("spring boot");
}catch (Exception e) {
Assert.assertTrue(e instanceof NacosBootConfigException);
}
}
private void testMethod(String str) {
if (str.startsWith("spring")) {
throw new NacosBootConfigException(new Throwable());
}
}
}

View File

@ -1,79 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.autoconfigure;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigApplicationContextInitializer;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigBootBeanDefinitionRegistrar;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigEnvironmentProcessor;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigLoader;
import com.alibaba.boot.nacos.config.util.NacosConfigLoaderFactory;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosConfigApplicationContextInitializer} Test
* @ClassName: NacosConfigApplicationContextInitializerTest
* @Author: ChenHao26
* @Date: 2022/8/12 15:25
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosConfigApplicationContextInitializerTest {
private NacosConfigApplicationContextInitializer nacosConfigApplicationContextInitializer;
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private NacosConfigProperties nacosConfigProperties;
@Autowired
private ConfigurableApplicationContext context;
@Before
public void testNacosConfigApplicationContextInitializer() {
nacosConfigApplicationContextInitializer = new NacosConfigApplicationContextInitializer(new NacosConfigEnvironmentProcessor());
}
@Test
public void initialize(){
try {
nacosConfigProperties = NacosConfigPropertiesUtils
.buildNacosConfigProperties(environment);
Assert.assertNotNull(nacosConfigProperties);
NacosConfigLoader singleton = NacosConfigLoaderFactory.getSingleton(null);
Assert.assertNotNull(singleton);
nacosConfigApplicationContextInitializer.initialize(context);
} catch (Exception e) {
Assert.assertNotNull(e);
}
}
}

View File

@ -19,7 +19,6 @@ package com.alibaba.boot.nacos.autoconfigure;
import java.util.Properties; import java.util.Properties;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration; import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.annotation.NacosInjected;
@ -64,9 +63,9 @@ public class NacosConfigAutoConfigurationTest {
Assert.assertEquals("localhost", nacosConfigProperties.getServerAddr()); Assert.assertEquals("localhost", nacosConfigProperties.getServerAddr());
} }
@Test @Test(expected = NoSuchBeanDefinitionException.class)
public void testNacosConfigGlobalBean() { public void testNacosConfigGlobalBean() {
Assert.assertNotNull(applicationContext Assert.assertNull(applicationContext
.getBean(NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME)); .getBean(NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME));
} }
@ -85,8 +84,4 @@ public class NacosConfigAutoConfigurationTest {
properties.getProperty(PropertyKeyConst.SERVER_ADDR)); properties.getProperty(PropertyKeyConst.SERVER_ADDR));
} }
@Test
public void testNacosConfigBootBeanDefinitionRegistrar() {
Assert.assertNotNull(applicationContext.getBean(NacosBootConfigurationPropertiesBinder.BEAN_NAME));
}
} }

View File

@ -1,69 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.autoconfigure;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigBootBeanDefinitionRegistrar;
import com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder;
import com.alibaba.nacos.client.utils.LogUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosConfigBootBeanDefinitionRegistrar} Test
* @ClassName: NacosConfigBootBeanDefinitionRegistrarTest
* @Author: ChenHao26
* @Date: 2022/8/12 15:38
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosConfigBootBeanDefinitionRegistrarTest {
private static final Logger LOGGER = LogUtils.logger(NacosConfigBootBeanDefinitionRegistrarTest.class);
private NacosConfigBootBeanDefinitionRegistrar nacosConfigBootBeanDefinitionRegistrar;
@Before
public void setup() {
nacosConfigBootBeanDefinitionRegistrar = new NacosConfigBootBeanDefinitionRegistrar();
}
@Test
public void setBeanFactory(){
try {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition("beanName");
Assert.assertNotNull(builder);
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
nacosConfigBootBeanDefinitionRegistrar.setBeanFactory(beanFactory);
Assert.assertNotNull(beanFactory.getBeanDefinition(NacosBootConfigurationPropertiesBinder.BEAN_NAME));
nacosConfigBootBeanDefinitionRegistrar.setBeanFactory(beanFactory);
}catch (Exception e) {
LOGGER.error("error info: {}",e.toString());
Assert.assertNull(e);
}
}
}

View File

@ -1,85 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.autoconfigure;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigEnvironmentProcessor;
import com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import com.alibaba.nacos.client.config.NacosConfigService;
import com.alibaba.nacos.client.utils.LogUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosBootConfigurationPropertiesBinder} Test
* @ClassName: NacosConfigEnvironmentProcessorTest
* @Author: ChenHao26
* @Date: 2022/8/12 15:43
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosConfigEnvironmentProcessorTest {
private NacosConfigEnvironmentProcessor nacosConfigEnvironmentProcessor;
@Autowired
private ConfigurableEnvironment environment;
private static final Logger LOGGER = LogUtils.logger(NacosConfigEnvironmentProcessorTest.class);
@Mock
private NacosConfigProperties nacosConfigProperties;
@Before
public void setup() {
nacosConfigEnvironmentProcessor = new NacosConfigEnvironmentProcessor();
}
@Test
public void postProcessEnvironment() {
try {
NacosConfigProperties nacosConfigProperties1 = NacosConfigPropertiesUtils.buildNacosConfigProperties(
environment);
Assert.assertFalse(nacosConfigProperties1.isEnableRemoteSyncConfig());
nacosConfigEnvironmentProcessor.postProcessEnvironment(environment, new SpringApplication());
}catch (Exception e) {
LOGGER.error("error info :{} ",e);
Assert.assertNull(e);
}
}
@Test
public void getOrder() {
int order = nacosConfigEnvironmentProcessor.getOrder();
Assert.assertNotNull(order);
}
}

View File

@ -1,108 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.binder;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.binder.NacosBootConfigurationPropertiesBinder;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.spring.context.event.config.EventPublishingConfigService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.lang.reflect.Method;
/**
* {@link NacosBootConfigurationPropertiesBinder} Test
* @ClassName: NacosBootConfigurationPropertiesBinderTest
* @Author: ChenHao26
* @Date: 2022/8/12 16:00
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosBootConfigurationPropertiesBinderTest {
private Binder binder;
@Autowired
private ConfigurableApplicationContext context;
@Before
public void setup() {
binder = new Binder(context);
}
@Test
public void findFactoryMethod(){
Method beanName = binder.findFactoryMethod(NacosBootConfigurationPropertiesBinder.BEAN_NAME);
Assert.assertNull(beanName);
}
@Test
public void testDoBind() {
People people = new People();
binder.doBind(people, "people", "people", "people", "properties",
People.class.getAnnotation(NacosConfigurationProperties.class), "people.name=SuperZ1999\npeople.age=24",
new EventPublishingConfigService(null, null, context, null));
Assert.assertEquals(people.getName(), "SuperZ1999");
Assert.assertEquals(people.getAge(), 24);
}
static class Binder extends NacosBootConfigurationPropertiesBinder {
public Binder(ConfigurableApplicationContext applicationContext) {
super(applicationContext);
}
@Override
protected void doBind(Object bean, String beanName, String dataId, String groupId, String configType, NacosConfigurationProperties properties, String content, ConfigService configService) {
super.doBind(bean, beanName, dataId, groupId, configType, properties, content, configService);
}
}
@NacosConfigurationProperties(prefix = "people", dataId = "people", groupId = "people")
static class People {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}

View File

@ -1,162 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.config.support;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.nacos.spring.util.ConfigParseUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link MultiProfilesYamlConfigParseSupport} Test
*
* @author <a href="mailto:yanglu_u@126.com">dbses</a>
*/
public class MultiProfilesYamlConfigParseSupportTest {
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"alpha"})
@SpringBootTest(classes = {NacosConfigAutoConfiguration.class})
public static class OneProfiles {
@Autowired
private Environment environment;
@Before
public void setUp() {
// because MultiProfilesYamlConfigParseSupport # postProcessEnvironment() run once,
// so it should set profilesArray before test
MultiProfilesYamlConfigParseSupport.setProfileArray(environment.getActiveProfiles());
}
@Test
public void oneProfiles_normal() {
String content = "test1:\n" +
" config: 2\n" +
"\n" +
"---\n" +
"spring:\n" +
" profiles: alpha\n" +
"test1:\n" +
" config: alpha\n" +
"\n" +
"---\n" +
"spring:\n" +
" profiles: beta\n" +
"test1:\n" +
" config: beta";
Assert.assertEquals(environment.getActiveProfiles()[0], "alpha");
Object result = ConfigParseUtils.toProperties("test.yaml", "test", content, "yaml")
.get("test1.config");
Assert.assertEquals("alpha", result);
}
@Test
public void oneProfiles_when_content_profiles_isnull() {
String content = "test1:\n" +
" config: 2\n" +
"\n" +
"---\n" +
"test1:\n" +
" config: alpha\n" +
"\n" +
"---\n" +
"test1:\n" +
" config: beta";
Object result = ConfigParseUtils.toProperties("test.yaml", "test", content, "yaml")
.get("test1.config");
Assert.assertEquals("beta", result);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"alpha", "beta"})
@SpringBootTest(classes = {NacosConfigAutoConfiguration.class})
public static class TwoProfiles {
@Autowired
private Environment environment;
@Before
public void setUp() {
MultiProfilesYamlConfigParseSupport.setProfileArray(environment.getActiveProfiles());
}
@Test
public void twoProfiles_normal() {
String content = "test1:\n" +
" config: 2\n" +
"\n" +
"---\n" +
"spring:\n" +
" profiles: alpha\n" +
"test1:\n" +
" config: alpha\n" +
"\n" +
"---\n" +
"spring:\n" +
" profiles: beta\n" +
"test1:\n" +
" config: beta";
Assert.assertEquals(environment.getActiveProfiles()[0], "alpha");
Assert.assertEquals(environment.getActiveProfiles()[1], "beta");
Object result = ConfigParseUtils.toProperties("test.yaml", "test", content, "yaml")
.get("test1.config");
Assert.assertEquals("beta", result);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {NacosConfigAutoConfiguration.class})
public static class NoProfiles {
@Autowired
private Environment environment;
@Test
public void noProfiles_normal() {
String content = "test1:\n" +
" config: 2\n" +
"\n" +
"---\n" +
"spring:\n" +
" profiles: default\n" +
"test1:\n" +
" config: default\n" +
"\n" +
"---\n" +
"spring:\n" +
" profiles: beta\n" +
"test1:\n" +
" config: beta";
Assert.assertEquals(environment.getActiveProfiles().length, 0);
Object result = ConfigParseUtils.toProperties("test.yaml", "test", content, "yaml")
.get("test1.config");
Assert.assertEquals("default", result);
}
}
}

View File

@ -1,59 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.logging;
import com.alibaba.boot.nacos.config.logging.NacosLoggingListener;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.ResolvableType;
import java.lang.reflect.Type;
/**
* {@link NacosLoggingListener} Test
* @ClassName: AttributeExtractTaskTest
* @Author: ChenHao26
* @Date: 2022/8/12 15:08
*/
public class NacosLoggingListenerTest {
private NacosLoggingListener nacosLoggingListener;
@Before
public void setup() {
nacosLoggingListener = new NacosLoggingListener();
}
@Test
public void supportsEventType() {
boolean result = nacosLoggingListener.supportsEventType(ResolvableType.forType(new Type() {
@Override
public String getTypeName() {
return Type.super.getTypeName();
}
}));
Assert.assertEquals(result, false);
}
@Test
public void getOrder() {
int order = nacosLoggingListener.getOrder();
Assert.assertNotNull(order);
}
}

View File

@ -1,65 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.util;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.util.AttributeExtractTask;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySources;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Map;
/**
* {@link AttributeExtractTask} Test
* @ClassName: AttributeExtractTaskTest
* @Author: ChenHao26
* @Date: 2022/8/12 15:08
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class AttributeExtractTaskTest {
private AttributeExtractTask attributeExtractTask;
@Autowired
private ConfigurableEnvironment environment ;
@Before
public void setUp() {
environment.setDefaultProfiles("prefix01");
attributeExtractTask = new AttributeExtractTask("nacos.config",environment);
}
@Test
public void call() throws Exception{
Map<String, String> map = attributeExtractTask.call();
Assert.assertEquals(map.get("nacos.config.server-addr"), "localhost");
}
}

View File

@ -1,87 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.util;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigLoader;
import com.alibaba.boot.nacos.config.util.NacosConfigLoaderFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.NacosConfigService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Properties;
import java.util.function.Function;
/**
* @ClassName: NacosConfigLoaderFactoryTest
* @Author: ChenHao26
* @Date: 2022/8/12 16:42
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosConfigLoaderFactoryTest {
private NacosConfigProperties nacosConfigProperties;
private Properties globalProperties;
private Function<Properties, ConfigService> builder;
@Before
public void setup() {
nacosConfigProperties = new NacosConfigProperties();
nacosConfigProperties.setServerAddr("localhost");
nacosConfigProperties.setUsername("nacos");
nacosConfigProperties.setPassword("nacos");
nacosConfigProperties.setMaxRetry("4");
nacosConfigProperties.setType(ConfigType.TEXT);
nacosConfigProperties.setDataId("xiaomi");
nacosConfigProperties.setGroup("group01");
nacosConfigProperties.setAutoRefresh(true);
nacosConfigProperties.setEndpoint("localhost");
globalProperties = new Properties();
globalProperties.setProperty("maxRetry","3");
globalProperties.setProperty("content","key=01");
globalProperties.setProperty("endpoint","localhost");
builder = properties -> {
try {
return new NacosConfigService(globalProperties);
} catch (NacosException e) {
throw new RuntimeException(e);
}
};
}
@Test
public void getSingleton() {
NacosConfigLoader singleton = NacosConfigLoaderFactory.getSingleton(builder);
NacosConfigLoader singleton2 = NacosConfigLoaderFactory.getSingleton(builder);
// Verify that it is the same object
Assert.assertEquals(singleton2, singleton);
}
}

View File

@ -1,116 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.util;
import com.alibaba.boot.nacos.autoconfigure.NacosConfigEnvironmentProcessorTest;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigLoader;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.NacosConfigService;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
/**
* @ClassName: NacosConfigLoaderTest
* @Author: ChenHao26
* @Date: 2022/8/12 16:12
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosConfigLoaderTest {
private NacosConfigLoader nacosConfigLoader;
private NacosConfigProperties nacosConfigProperties;
private Properties globalProperties;
@Autowired
private ConfigurableEnvironment environment;
private Function<Properties, ConfigService> builder;
private List<NacosConfigLoader.DeferNacosPropertySource> nacosPropertySources;
private static final Logger LOGGER = LogUtils.logger(NacosConfigEnvironmentProcessorTest.class);
private NacosPropertySourcePostProcessor nacosPropertySourcePostProcessor;
@Before
public void setup() {
nacosConfigProperties = new NacosConfigProperties();
nacosConfigProperties.setServerAddr("localhost");
nacosConfigProperties.setUsername("nacos");
nacosConfigProperties.setPassword("nacos");
nacosConfigProperties.setMaxRetry("4");
nacosConfigProperties.setType(ConfigType.TEXT);
nacosConfigProperties.setDataId("xiaomi");
nacosConfigProperties.setGroup("group01");
nacosConfigProperties.setAutoRefresh(true);
nacosConfigProperties.setEndpoint("localhost");
globalProperties = new Properties();
globalProperties.setProperty("maxRetry","3");
globalProperties.setProperty("content","key=01");
globalProperties.setProperty("endpoint","localhost");
builder = properties -> {
try {
return new NacosConfigService(globalProperties);
} catch (NacosException e) {
throw new RuntimeException(e);
}
};
nacosPropertySources = new LinkedList<>();
nacosConfigLoader = new NacosConfigLoader(builder);
nacosPropertySourcePostProcessor = new NacosPropertySourcePostProcessor();
}
@Test
public void buildGlobalNacosProperties() {
Properties properties = nacosConfigLoader.buildGlobalNacosProperties(environment, nacosConfigProperties);
LOGGER.info("buildGlobalNacosProperties properties : {}", properties);
Assert.assertNotNull(properties);
Assert.assertEquals(properties.size(), 6);
}
@Test
public void addListenerIfAutoRefreshed() {
nacosConfigLoader.addListenerIfAutoRefreshed();
List<NacosConfigLoader.DeferNacosPropertySource> propertySources = nacosConfigLoader.getNacosPropertySources();
Assert.assertEquals(propertySources.size(), 0);
}
}

View File

@ -1,56 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.util;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosConfigPropertiesUtils} Test
* @ClassName: NacosConfigPropertiesUtilsTest
* @Author: SuperZ1999
* @Date: 2023/9/28
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
@TestPropertySource(properties = {
"nacos.config.server-addr=8.8.8.8",
"nacos.config.password=123456"
})
public class NacosConfigPropertiesUtilsTest {
@Autowired
private ConfigurableEnvironment environment;
@Test
public void testBuildNacosConfigProperties() {
NacosConfigProperties properties = NacosConfigPropertiesUtils.buildNacosConfigProperties(environment);
Assert.assertEquals(properties.getServerAddr(), "8.8.8.8");
Assert.assertEquals(properties.getPassword(), "123456");
}
}

View File

@ -1,78 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.util;
import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration;
import com.alibaba.boot.nacos.config.util.NacosPropertiesBuilder;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Properties;
/**
* @ClassName: NacosPropertiesBuilderTest
* @Author: ChenHao26
* @Date: 2022/8/12 16:49
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = { "nacos.config.server-addr=localhost" })
@SpringBootTest(classes = { NacosConfigAutoConfiguration.class })
public class NacosPropertiesBuilderTest {
@Autowired
private Environment environment;
@Test
public void testBuildNacosProperties() {
String serverAddr = "${nacos.config.server-addr}";
String namespaceId = "namespaceId";
String secretKey = "secretKey";
String ramRoleName = "ramRoleName";
String configLongPollTimeout = "configLongPollTimeout";
String configRetryTimeout = "configRetryTimeout";
String maxRetry = "maxRetry";
String enableRemoteSyncConfig = "enableRemoteSyncConfig";
String username = "nacos";
String password = "password";
Properties properties = NacosPropertiesBuilder.buildNacosProperties(environment, serverAddr, namespaceId, secretKey,
"ak", ramRoleName, configLongPollTimeout, configRetryTimeout, maxRetry, null,enableRemoteSyncConfig, true,
username, password);
Assert.assertEquals(properties.size(), 12);
Assert.assertEquals(properties.get("serverAddr"), "localhost");
}
@Test
public void testMerge() {
Properties sourceProperties = new Properties();
sourceProperties.put("name", "SuperZ1999");
sourceProperties.put("age", 24);
Properties targetProperties = new Properties();
targetProperties.put("age", 99);
NacosPropertiesBuilder.merge(targetProperties, sourceProperties);
Assert.assertEquals(targetProperties.get("name"), "SuperZ1999");
Assert.assertEquals(targetProperties.get("age"), 99);
}
}

View File

@ -1,51 +0,0 @@
/*
* 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 com.alibaba.boot.nacos.util.editor;
import com.alibaba.boot.nacos.config.util.editor.NacosCharSequenceEditor;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* @ClassName: NacosCharSequenceEditorTest
* @Author: ChenHao26
* @Date: 2022/8/12 17:00
*/
public class NacosCharSequenceEditorTest {
public NacosCharSequenceEditor nacosCharSequenceEditor;
@Before
public void setup() {
nacosCharSequenceEditor = new NacosCharSequenceEditor();
}
@Test
public void setValue() {
nacosCharSequenceEditor.setValue("nacosTest");
String asText = nacosCharSequenceEditor.getAsText();
Assert.assertEquals(asText,"nacosTest");
}
@Test
public void getAsText() {
String str = nacosCharSequenceEditor.getAsText();
Assert.assertEquals(str, "null");
}
}

View File

@ -1,64 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.util.editor;
import com.alibaba.boot.nacos.config.util.editor.NacosCustomBooleanEditor;
import org.junit.Assert;
import org.junit.Test;
/**
* {@link NacosCustomBooleanEditor} Test
* @ClassName: NacosCustomBooleanEditorTest
* @Author: SuperZ1999
* @Date: 2023/9/28
*/
public class NacosCustomBooleanEditorTest {
@Test
public void testAllowEmpty() {
NacosCustomBooleanEditor booleanEditor = new NacosCustomBooleanEditor(true);
booleanEditor.setValue("");
Assert.assertEquals(booleanEditor.getAsText(), "false");
booleanEditor.setValue("true");
Assert.assertEquals(booleanEditor.getAsText(), "true");
}
@Test(expected = IllegalArgumentException.class)
public void testNotAllowEmpty() {
NacosCustomBooleanEditor booleanEditor = new NacosCustomBooleanEditor(false);
booleanEditor.setValue("true");
Assert.assertEquals(booleanEditor.getAsText(), "true");
booleanEditor.setValue("");
}
@Test
public void testCustomBooleanString() {
NacosCustomBooleanEditor booleanEditor = new NacosCustomBooleanEditor("TRUE", "FALSE", true);
booleanEditor.setValue("");
Assert.assertEquals(booleanEditor.getAsText(), "FALSE");
booleanEditor.setValue("TRUE");
Assert.assertEquals(booleanEditor.getAsText(), "TRUE");
booleanEditor.setValue("false");
Assert.assertEquals(booleanEditor.getAsText(), "FALSE");
}
}

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent</relativePath> <relativePath>../nacos-spring-boot-parent</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -34,24 +34,13 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<scope>true</scope>
</dependency> </dependency>
<!-- Nacos --> <!-- Nacos -->
<dependency> <dependency>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -64,10 +53,5 @@
<artifactId>nacos-spring-boot-base</artifactId> <artifactId>nacos-spring-boot-base</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-aot</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent/pom.xml</relativePath> <relativePath>../nacos-spring-boot-parent/pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -81,21 +81,8 @@
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<optional>true</optional> <optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<!-- Test Dependencies --> <!-- Test Dependencies -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -16,9 +16,12 @@
*/ */
package com.alibaba.boot.nacos.discovery.actuate.autoconfigure; package com.alibaba.boot.nacos.discovery.actuate.autoconfigure;
import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants;
import com.alibaba.boot.nacos.discovery.actuate.endpoint.NacosDiscoveryEndpoint; import com.alibaba.boot.nacos.discovery.actuate.endpoint.NacosDiscoveryEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -30,11 +33,12 @@ import org.springframework.context.annotation.Configuration;
* @see Endpoint * @see Endpoint
*/ */
@Configuration @Configuration
@ConditionalOnClass(Endpoint.class)
public class NacosDiscoveryEndpointsAutoConfiguration { public class NacosDiscoveryEndpointsAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint @ConditionalOnEnabledEndpoint(NacosDiscoveryConstants.ENDPOINT_PREFIX)
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() { public NacosDiscoveryEndpoint nacosDiscoveryEndpoint() {
return new NacosDiscoveryEndpoint(); return new NacosDiscoveryEndpoint();
} }

View File

@ -20,8 +20,8 @@ import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants;
import com.alibaba.boot.nacos.discovery.actuate.health.NacosDiscoveryHealthIndicator; import com.alibaba.boot.nacos.discovery.actuate.health.NacosDiscoveryHealthIndicator;
import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration; import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;

View File

@ -22,17 +22,16 @@ import java.util.Properties;
import com.alibaba.boot.nacos.common.PropertiesUtils; import com.alibaba.boot.nacos.common.PropertiesUtils;
import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants; import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory; import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.factory.NacosServiceFactory; import com.alibaba.nacos.spring.factory.NacosServiceFactory;
import com.alibaba.nacos.spring.util.NacosUtils; import com.alibaba.nacos.spring.util.NacosUtils;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME; import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME;
@ -43,15 +42,18 @@ import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACO
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @see Endpoint * @see Endpoint
*/ */
@Endpoint(id = NacosDiscoveryConstants.ENDPOINT_PREFIX) public class NacosDiscoveryEndpoint extends AbstractEndpoint<Map<String, Object>> {
public class NacosDiscoveryEndpoint {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
private static final Integer PAGE_SIZE = 100; private static final Integer PAGE_SIZE = 100;
@ReadOperation public NacosDiscoveryEndpoint() {
super(NacosDiscoveryConstants.ENDPOINT_PREFIX);
}
@Override
public Map<String, Object> invoke() { public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<>(8); Map<String, Object> result = new HashMap<>(8);
@ -61,18 +63,19 @@ public class NacosDiscoveryEndpoint {
NacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory NacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory
.getSingleton(); .getSingleton();
;
ArrayNode array = JacksonUtils.createEmptyArrayNode(); JSONArray array = new JSONArray();
for (NamingService namingService : nacosServiceFactory.getNamingServices()) { for (NamingService namingService : nacosServiceFactory.getNamingServices()) {
ObjectNode jsonObject = JacksonUtils.createEmptyJsonNode(); JSONObject jsonObject = new JSONObject();
try { try {
jsonObject.putPOJO("servicesOfServer", jsonObject.put("servicesOfServer",
namingService.getServicesOfServer(0, PAGE_SIZE)); namingService.getServicesOfServer(0, PAGE_SIZE));
jsonObject.putPOJO("subscribeServices", namingService.getSubscribeServices()); jsonObject.put("subscribeServices", namingService.getSubscribeServices());
array.add(jsonObject); array.add(jsonObject);
} }
catch (Exception e) { catch (Exception e) {
jsonObject.put("serverStatus", namingService.getServerStatus() + ": " jsonObject.put("serverStatus", namingService.getServerStatus()
+ NacosUtils.SEPARATOR + e.getMessage()); + NacosUtils.SEPARATOR + e.getMessage());
} }
} }

View File

@ -19,8 +19,8 @@ package com.alibaba.boot.nacos.discovery.actuate.health;
import java.util.Properties; import java.util.Properties;
import com.alibaba.boot.nacos.common.PropertiesUtils; import com.alibaba.boot.nacos.common.PropertiesUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory; import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.factory.NacosServiceFactory; import com.alibaba.nacos.spring.factory.NacosServiceFactory;
import com.alibaba.nacos.spring.metadata.NacosServiceMetaData; import com.alibaba.nacos.spring.metadata.NacosServiceMetaData;
@ -54,11 +54,11 @@ public class NacosDiscoveryHealthIndicator extends AbstractHealthIndicator {
NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) namingService; NacosServiceMetaData nacosServiceMetaData = (NacosServiceMetaData) namingService;
Properties properties = nacosServiceMetaData.getProperties(); Properties properties = nacosServiceMetaData.getProperties();
builder.withDetail( builder.withDetail(
JacksonUtils.toJson( JSON.toJSONString(
PropertiesUtils.extractSafeProperties(properties)), PropertiesUtils.extractSafeProperties(properties)),
namingService.getServerStatus()); namingService.getServerStatus());
} }
if (!namingService.getServerStatus().equalsIgnoreCase(UP_STATUS)) { if (!namingService.getServerStatus().toLowerCase().equals(UP_STATUS)) {
builder.down(); builder.down();
} }
} }

View File

@ -1,2 +0,0 @@
com.alibaba.boot.nacos.discovery.actuate.autoconfigure.NacosDiscoveryEndpointsAutoConfiguration
com.alibaba.boot.nacos.discovery.actuate.autoconfigure.NacosDiscoveryHealthIndicatorAutoConfiguration

View File

@ -1,52 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.actuate.autoconfigure;
import com.alibaba.boot.nacos.discovery.actuate.autoconfigure.NacosDiscoveryEndpointsAutoConfiguration;
import com.alibaba.boot.nacos.discovery.actuate.endpoint.NacosDiscoveryEndpoint;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosDiscoveryEndpointsAutoConfiguration} Test
* @ClassName: NacosDiscoveryEndpointsAutoConfigurationTest
* @Author: SuperZ1999
* @Date: 2023/9/28
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {NacosDiscoveryEndpointsAutoConfiguration.class})
@TestPropertySource(properties = {
"management.endpoints.web.exposure.include=nacos-discovery"
})
public class NacosDiscoveryEndpointsAutoConfigurationTest {
@Autowired
private ApplicationContext context;
@Test
public void testNacosDiscoveryEndpointBean() {
Assert.assertNotNull(context.getBean(NacosDiscoveryEndpoint.class));
}
}

View File

@ -1,48 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.actuate.autoconfigure;
import com.alibaba.boot.nacos.discovery.actuate.autoconfigure.NacosDiscoveryHealthIndicatorAutoConfiguration;
import com.alibaba.boot.nacos.discovery.actuate.health.NacosDiscoveryHealthIndicator;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* {@link NacosDiscoveryHealthIndicatorAutoConfiguration} Test
* @ClassName: NacosDiscoveryHealthIndicatorAutoConfigurationTest
* @Author: SuperZ1999
* @Date: 2023/9/28
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {NacosDiscoveryHealthIndicatorAutoConfiguration.class})
public class NacosDiscoveryHealthIndicatorAutoConfigurationTest {
@Autowired
private ApplicationContext context;
@Test
public void testNacosConfigHealthIndicatorBean() {
Assert.assertNotNull(context.getBean(NacosDiscoveryHealthIndicator.class));
}
}

View File

@ -21,12 +21,14 @@ import java.util.Map;
import com.alibaba.boot.nacos.discovery.actuate.endpoint.NacosDiscoveryEndpoint; import com.alibaba.boot.nacos.discovery.actuate.endpoint.NacosDiscoveryEndpoint;
import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration; import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration;
import com.alibaba.nacos.api.PropertyKeyConst;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
/** /**
@ -36,6 +38,7 @@ import org.springframework.test.context.junit4.SpringRunner;
* @see NacosDiscoveryEndpoint * @see NacosDiscoveryEndpoint
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@TestPropertySource(properties = { "nacos.discovery.server-addr=localhost" })
@SpringBootTest(classes = { NacosDiscoveryEndpoint.class, @SpringBootTest(classes = { NacosDiscoveryEndpoint.class,
NacosDiscoveryAutoConfiguration.class }) NacosDiscoveryAutoConfiguration.class })
public class NacosDiscoveryEndpointTest { public class NacosDiscoveryEndpointTest {
@ -50,7 +53,8 @@ public class NacosDiscoveryEndpointTest {
HashMap nacosDiscoveryGlobalProperties = (HashMap) metadata HashMap nacosDiscoveryGlobalProperties = (HashMap) metadata
.get("nacosDiscoveryGlobalProperties"); .get("nacosDiscoveryGlobalProperties");
Assert.assertNotNull(nacosDiscoveryGlobalProperties); Assert.assertEquals("localhost",
nacosDiscoveryGlobalProperties.get(PropertyKeyConst.SERVER_ADDR));
} }
} }

View File

@ -1 +0,0 @@
nacos.discovery.serverAddr = "127.0.0.1:8848"

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent</relativePath> <relativePath>../nacos-spring-boot-parent</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -54,33 +54,14 @@
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<optional>true</optional> <optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-base</artifactId> <artifactId>nacos-spring-boot-base</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-aot</artifactId>
<optional>true</optional>
</dependency>
<!-- Test Dependencies --> <!-- Test Dependencies -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -25,10 +25,9 @@ import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscover
*/ */
public interface NacosDiscoveryConstants { public interface NacosDiscoveryConstants {
String PREFIX = "nacos.discovery"; String ENDPOINT_PREFIX = "nacos_discovery";
String ENDPOINT_PREFIX = "nacos-discovery";
String ENABLED = EnableNacosDiscovery.DISCOVERY_PREFIX + "enabled"; String ENABLED = EnableNacosDiscovery.DISCOVERY_PREFIX + "enabled";
String PREFIX = "nacos.discovery";
} }

View File

@ -18,14 +18,10 @@ package com.alibaba.boot.nacos.discovery.autoconfigure;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.4 * @since 0.1.4
*/ */
public class AutoDeregisterException extends RuntimeException { public class AutoDeregisterException extends RuntimeException {
public AutoDeregisterException(String message) {
super(message);
}
public AutoDeregisterException(Throwable cause) { public AutoDeregisterException(Throwable cause) {
super(cause); super(cause);
} }

View File

@ -18,14 +18,10 @@ package com.alibaba.boot.nacos.discovery.autoconfigure;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class AutoRegisterException extends RuntimeException { public class AutoRegisterException extends RuntimeException {
public AutoRegisterException(String message) {
super(message);
}
public AutoRegisterException(Throwable cause) { public AutoRegisterException(Throwable cause) {
super(cause); super(cause);
} }

View File

@ -16,7 +16,6 @@
*/ */
package com.alibaba.boot.nacos.discovery.autoconfigure; package com.alibaba.boot.nacos.discovery.autoconfigure;
import com.alibaba.boot.nacos.aot.context.EnableNacosDiscoveryAotProcessor;
import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants; import com.alibaba.boot.nacos.discovery.NacosDiscoveryConstants;
import com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties; import com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery; import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
@ -27,7 +26,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME; import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME;
@ -40,8 +38,7 @@ import static com.alibaba.nacos.spring.util.NacosBeanUtils.DISCOVERY_GLOBAL_NACO
@ConditionalOnMissingBean(name = DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME) @ConditionalOnMissingBean(name = DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME)
@EnableNacosDiscovery @EnableNacosDiscovery
@EnableConfigurationProperties(value = NacosDiscoveryProperties.class) @EnableConfigurationProperties(value = NacosDiscoveryProperties.class)
@ConditionalOnClass(name = "org.springframework.boot.context.properties.bind.Binder") @ConditionalOnClass(name = "org.springframework.boot.bind.RelaxedDataBinder")
@Import(EnableNacosDiscoveryAotProcessor.class)
public class NacosDiscoveryAutoConfiguration { public class NacosDiscoveryAutoConfiguration {
@Bean @Bean

View File

@ -21,20 +21,20 @@ import com.alibaba.boot.nacos.discovery.properties.Register;
import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.utils.NetUtils; import com.alibaba.nacos.client.naming.utils.NetUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.4 * @since 0.1.4
*/ */
@Component @Component
public class NacosDiscoveryAutoDeregister public class NacosDiscoveryAutoDeregister
@ -47,13 +47,13 @@ public class NacosDiscoveryAutoDeregister
private NamingService namingService; private NamingService namingService;
private final NacosDiscoveryProperties discoveryProperties; private final NacosDiscoveryProperties discoveryProperties;
private final WebServer webServer; private final EmbeddedServletContainer webServer;
@Value("${spring.application.name:}") @Value("${spring.application.name:spring.application.name}")
private String applicationName; private String applicationName;
public NacosDiscoveryAutoDeregister(NacosDiscoveryProperties discoveryProperties, public NacosDiscoveryAutoDeregister(NacosDiscoveryProperties discoveryProperties,
WebServer webServer) { EmbeddedServletContainer webServer) {
this.discoveryProperties = discoveryProperties; this.discoveryProperties = discoveryProperties;
this.webServer = webServer; this.webServer = webServer;
} }
@ -74,20 +74,15 @@ public class NacosDiscoveryAutoDeregister
register.setPort(webServer.getPort()); register.setPort(webServer.getPort());
} }
String serviceName = register.getServiceName(); String serviceName = StringUtils.isEmpty(register.getServiceName())
? applicationName
if (StringUtils.isEmpty(serviceName)){ : register.getServiceName();
if (StringUtils.isEmpty(applicationName)){
throw new AutoDeregisterException("serviceName notNull");
}
serviceName = applicationName;
}
try { try {
namingService.deregisterInstance(serviceName, register.getGroupName(), namingService.deregisterInstance(serviceName, register.getGroupName(),
register); register);
logger.info("Finished auto deregister service : {}, ip : {}, port : {}", logger.info("Finished auto deregister service : {}, ip : {}, port : {}",
serviceName, register.getIp(), register.getPort()); register.getServiceName(), register.getIp(), register.getPort());
} }
catch (NacosException e) { catch (NacosException e) {
throw new AutoDeregisterException(e); throw new AutoDeregisterException(e);

View File

@ -21,25 +21,24 @@ import com.alibaba.boot.nacos.discovery.properties.Register;
import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.utils.NetUtils; import com.alibaba.nacos.client.naming.utils.NetUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
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.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
@Component @Component
public class NacosDiscoveryAutoRegister public class NacosDiscoveryAutoRegister
implements ApplicationListener<WebServerInitializedEvent> { implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(NacosDiscoveryAutoRegister.class); .getLogger(NacosDiscoveryAutoRegister.class);
@ -50,11 +49,11 @@ public class NacosDiscoveryAutoRegister
@Autowired @Autowired
private NacosDiscoveryProperties discoveryProperties; private NacosDiscoveryProperties discoveryProperties;
@Value("${spring.application.name:}") @Value("${spring.application.name:spring.application.name}")
private String applicationName; private String application;
@Override @Override
public void onApplicationEvent(WebServerInitializedEvent event) { public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
if (!discoveryProperties.isAutoRegister()) { if (!discoveryProperties.isAutoRegister()) {
return; return;
@ -67,29 +66,23 @@ public class NacosDiscoveryAutoRegister
} }
if (register.getPort() == 0) { if (register.getPort() == 0) {
register.setPort(event.getWebServer().getPort()); register.setPort(event.getSource().getPort());
} }
register.getMetadata().put("preserved.register.source", "SPRING_BOOT"); register.getMetadata().put("preserved.register.source", "SPRING_BOOT");
register.setInstanceId(""); register.setInstanceId("");
String serviceName = StringUtils.isEmpty(register.getServiceName()) ? application
String serviceName = register.getServiceName(); : register.getServiceName();
if (StringUtils.isEmpty(serviceName)){
if (StringUtils.isEmpty(applicationName)){
throw new AutoRegisterException("serviceName notNull");
}
serviceName = applicationName;
}
try { try {
namingService.registerInstance(serviceName, register.getGroupName(), namingService.registerInstance(serviceName, register.getGroupName(),
register); register);
logger.info("Finished auto register service : {}, ip : {}, port : {}", logger.info("Finished auto register service : {}, ip : {}, port : {}",
serviceName, register.getIp(), register.getPort()); register.getServiceName(), register.getIp(), register.getPort());
} catch (NacosException e) { }
throw new AutoRegisterException(e); catch (NacosException e) {
throw new RuntimeException(e);
} }
} }

View File

@ -49,26 +49,6 @@ public class NacosDiscoveryProperties {
@NestedConfigurationProperty @NestedConfigurationProperty
private Register register = new Register(); private Register register = new Register();
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getServerAddr() { public String getServerAddr() {
return serverAddr; return serverAddr;
} }

View File

@ -21,7 +21,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
/** /**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a> * @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3 * @since 0.1.3
*/ */
public class Register extends Instance { public class Register extends Instance {

View File

@ -0,0 +1,66 @@
{
"hints": [ ],
"groups": [ ],
"properties": [
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.enabled",
"description": "enable or disable nacos discovery feature",
"type": "java.lang.Boolean"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.server-addr",
"description": "the server address of nacos",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.context-path",
"description": "the context path of nacos server",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.endpoint",
"description": "the entry domain name of a service in each region",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.cluster-name",
"description": "cluster info under service",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.namespace",
"description": "for configuration isolation by tenants. Different namespaces may have configurations with the same Group or Data ID. ",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.access-key",
"description": "the accesskey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.secret-key",
"description": "the secretkey of Alibaba Cloud Account",
"type": "java.lang.String"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.register",
"description": "the service",
"type": "com.alibaba.boot.nacos.discovery.properties.Register"
},
{
"sourceType": "com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties",
"name": "nacos.discovery.auto-register",
"description": "the service",
"type": "java.lang.Boolean"
}
]
}

View File

@ -1 +0,0 @@
com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration

View File

@ -19,7 +19,6 @@ package com.alibaba.boot.nacos.autoconfigure;
import java.util.Properties; import java.util.Properties;
import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration; import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration;
import com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoRegister;
import com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties; import com.alibaba.boot.nacos.discovery.properties.NacosDiscoveryProperties;
import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.annotation.NacosInjected;
@ -65,13 +64,13 @@ public class NacosDiscoveryAutoConfigurationTest {
} }
@Test(expected = NoSuchBeanDefinitionException.class) @Test(expected = NoSuchBeanDefinitionException.class)
public void testNacosDiscoveryGlobalBean() { public void testNacosConfigGlobalBean() {
Assert.assertNull(applicationContext Assert.assertNull(applicationContext
.getBean(NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME)); .getBean(NacosBeanUtils.GLOBAL_NACOS_PROPERTIES_BEAN_NAME));
} }
@Test(expected = NoSuchBeanDefinitionException.class) @Test(expected = NoSuchBeanDefinitionException.class)
public void testNacosConfigGlobalBean() { public void testNacosDiscoveryGlobalBean() {
Assert.assertNull(applicationContext Assert.assertNull(applicationContext
.getBean(NacosBeanUtils.CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME)); .getBean(NacosBeanUtils.CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME));
} }
@ -85,8 +84,4 @@ public class NacosDiscoveryAutoConfigurationTest {
properties.getProperty(PropertyKeyConst.SERVER_ADDR)); properties.getProperty(PropertyKeyConst.SERVER_ADDR));
} }
@Test
public void testAddNacosDiscoveryAutoRegister() {
Assert.assertNotNull(applicationContext.getBean(NacosDiscoveryAutoRegister.class));
}
} }

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent</relativePath> <relativePath>../nacos-spring-boot-parent</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -34,24 +34,13 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<scope>true</scope>
</dependency> </dependency>
<!-- Nacos --> <!-- Nacos -->
<dependency> <dependency>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -64,10 +53,5 @@
<artifactId>nacos-spring-boot-base</artifactId> <artifactId>nacos-spring-boot-base</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-aot</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,69 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ /*
~ * 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.
~ */
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-parent</artifactId>
<version>${revision}</version>
<relativePath>../nacos-spring-boot-parent</relativePath>
</parent>
<artifactId>nacos-spring-boot-aot</artifactId>
<packaging>jar</packaging>
<name>Nacos Spring Boot Aot</name>
<description>Nacos Spring Boot Aot</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring6.framework.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring6.framework.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring6.framework.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>${nacos-spring-context.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,105 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.aot.context;
import com.alibaba.boot.nacos.aot.util.AotDetector;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosConfigBeanDefinitionRegistrar;
import com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor;
import com.alibaba.nacos.spring.util.NacosBeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Map;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.*;
/**
* {@link EnableNacosConfig} AotProcessor
* Except for the operation of registering BeanDefinition, all other operations in {@link NacosConfigBeanDefinitionRegistrar} must be done here
* because spring will not call {@link NacosConfigBeanDefinitionRegistrar#registerBeanDefinitions} in AOT.
* @author SuperZ1999
*/
public class EnableNacosConfigAotProcessor implements BeanDefinitionRegistryPostProcessor, EnvironmentAware, BeanFactoryAware {
private Environment environment;
private BeanFactory beanFactory;
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (!AotDetector.useGeneratedArtifacts()) {
return;
}
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) this.beanFactory;
Map<String, Object> beansWithAnnotation = beanFactory.getBeansWithAnnotation(EnableNacosConfig.class);
Object[] beans = beansWithAnnotation.values().toArray();
if (beans.length != 0) {
// only handle the first one
Class<?> aClass = beans[0].getClass();
if (aClass.getAnnotation(EnableNacosConfig.class) == null) {
// cglib proxy object
aClass = aClass.getSuperclass();
}
AnnotationMetadata annotationMetadata = AnnotationMetadata.introspect(aClass);
AnnotationAttributes attributes = AnnotationAttributes
.fromMap(annotationMetadata
.getAnnotationAttributes(EnableNacosConfig.class.getName()));
// Register Global Nacos Properties Bean
registerGlobalNacosProperties(attributes, registry, environment,
CONFIG_GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
}
registerNacosConfigListenerExecutor(registry, environment);
// replace NacosPropertySourcePostProcessor with NacosPropertySourcePostProcessorForAot
if (registry.containsBeanDefinition(NacosPropertySourcePostProcessor.BEAN_NAME)) {
registry.removeBeanDefinition(NacosPropertySourcePostProcessor.BEAN_NAME);
}
NacosBeanUtils.registerInfrastructureBeanIfAbsent(registry, NacosPropertySourcePostProcessorForAot.BEAN_NAME,
NacosPropertySourcePostProcessorForAot.class);
// Invoke NacosPropertySourcePostProcessor immediately
// in order to enhance the precedence of @NacosPropertySource process
invokeNacosPropertySourcePostProcessor(this.beanFactory);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}

View File

@ -1,94 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.aot.context;
import com.alibaba.boot.nacos.aot.util.AotDetector;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import com.alibaba.nacos.spring.context.annotation.discovery.NacosDiscoveryBeanDefinitionRegistrar;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Map;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.*;
/**
* {@link EnableNacosDiscovery} AotProcessor
* Except for the operation of registering BeanDefinition, all other operations in {@link NacosDiscoveryBeanDefinitionRegistrar} must be done here
* because spring will not call {@link NacosDiscoveryBeanDefinitionRegistrar#registerBeanDefinitions} in AOT.
* @author SuperZ1999
*/
public class EnableNacosDiscoveryAotProcessor implements BeanDefinitionRegistryPostProcessor, EnvironmentAware, BeanFactoryAware {
private Environment environment;
private BeanFactory beanFactory;
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (!AotDetector.useGeneratedArtifacts()) {
return;
}
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) this.beanFactory;
Map<String, Object> beansWithAnnotation = beanFactory.getBeansWithAnnotation(EnableNacosDiscovery.class);
Object[] beans = beansWithAnnotation.values().toArray();
if (beans.length != 0) {
// only handle the first one
Class<?> aClass = beans[0].getClass();
if (aClass.getAnnotation(EnableNacosDiscovery.class) == null) {
// cglib proxy object
aClass = aClass.getSuperclass();
}
AnnotationMetadata annotationMetadata = AnnotationMetadata.introspect(aClass);
AnnotationAttributes attributes = AnnotationAttributes
.fromMap(annotationMetadata
.getAnnotationAttributes(EnableNacosDiscovery.class.getName()));
// Register Global Nacos Properties Bean
registerGlobalNacosProperties(attributes, registry, environment,
DISCOVERY_GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
registerGlobalNacosProperties(attributes, registry, environment,
MAINTAIN_GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}

View File

@ -1,90 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.aot.context;
import com.alibaba.boot.nacos.aot.util.AotDetector;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.core.env.AbstractNacosPropertySourceBuilder;
import com.alibaba.nacos.spring.core.env.NacosPropertySourcePostProcessor;
import com.alibaba.spring.util.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import java.util.ArrayList;
import java.util.Map;
import static com.alibaba.nacos.spring.util.NacosBeanUtils.getConfigServiceBeanBuilder;
public class NacosPropertySourcePostProcessorForAot extends NacosPropertySourcePostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
String[] abstractNacosPropertySourceBuilderBeanNames = BeanUtils
.getBeanNames(beanFactory, AbstractNacosPropertySourceBuilder.class);
this.nacosPropertySourceBuilders = new ArrayList<AbstractNacosPropertySourceBuilder>(
abstractNacosPropertySourceBuilderBeanNames.length);
for (String beanName : abstractNacosPropertySourceBuilderBeanNames) {
this.nacosPropertySourceBuilders.add(beanFactory.getBean(beanName,
AbstractNacosPropertySourceBuilder.class));
}
NacosPropertySourcePostProcessor.beanFactory = beanFactory;
this.configServiceBeanBuilder = getConfigServiceBeanBuilder(beanFactory);
if (AotDetector.useGeneratedArtifacts()) {
// the type of all BeanDefinitions is RootBeanDefinition in AOT, but what we need is AnnotatedGenericBeanDefinition.
Map<String, Object> beansWithAnnotation = beanFactory.getBeansWithAnnotation(NacosPropertySource.class);
for (Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()) {
processPropertySourceForAot(entry.getKey(), entry.getValue());
}
}
String[] beanNames = beanFactory.getBeanDefinitionNames();
for (String beanName : beanNames) {
processPropertySource(beanName, beanFactory);
}
}
private void processPropertySourceForAot(String beanName, Object bean) {
if (processedBeanNames.contains(beanName)) {
return;
}
BeanDefinition beanDefinition = null;
Class<?> aClass = bean.getClass();
NacosPropertySource[] annotations = aClass.getSuperclass().getAnnotationsByType(NacosPropertySource.class);
if (annotations.length != 0) {
beanDefinition = new AnnotatedGenericBeanDefinition(aClass.getSuperclass());
}
annotations = aClass.getAnnotationsByType(NacosPropertySource.class);
if (annotations.length != 0) {
beanDefinition = new AnnotatedGenericBeanDefinition(aClass);
}
doProcessPropertySource(beanName, beanDefinition);
processedBeanNames.add(beanName);
}
}

View File

@ -1,76 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.aot.hint;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* {@link NacosInjected} and {@link NacosValue} AotProcessor
* The fields annotated with {@link NacosInjected} or {@link NacosValue} must be added to the reflect-config.json
* @author SuperZ1999
*/
public class NacosAnnotationBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@Override
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanClass = registeredBean.getBeanClass();
List<Field> fields = new ArrayList<>();
ReflectionUtils.doWithFields(beanClass, field -> {
NacosInjected injectedAnnotation = field.getDeclaredAnnotation(NacosInjected.class);
NacosValue nacosValueAnnotation = field.getDeclaredAnnotation(NacosValue.class);
if (injectedAnnotation != null || nacosValueAnnotation != null) {
fields.add(field);
}
});
if (fields.isEmpty()) {
return null;
}
return new AotContribution(fields);
}
private static class AotContribution implements BeanRegistrationAotContribution {
private final List<Field> fields;
public AotContribution() {
this.fields = new ArrayList<>();
}
public AotContribution(List<Field> fields) {
this.fields = fields;
}
@Override
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
for (Field field : fields) {
generationContext.getRuntimeHints().reflection().registerField(field);
}
}
}
}

View File

@ -1,45 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.aot.util;
import org.springframework.core.NativeDetector;
import org.springframework.core.SpringProperties;
public abstract class AotDetector {
/**
* System property that indicates the application should run with AOT
* generated artifacts. If such optimizations are not available, it is
* recommended to throw an exception rather than fall back to the regular
* runtime behavior.
*/
public static final String AOT_ENABLED = "spring.aot.enabled";
/**
* Determine whether AOT optimizations must be considered at runtime. This
* is mandatory in a native image but can be triggered on the JVM using
* the {@value #AOT_ENABLED} Spring property.
* @return whether AOT optimizations must be considered
*/
public static boolean useGeneratedArtifacts() {
return (NativeDetector.inNativeImage() || SpringProperties.getFlag(AOT_ENABLED));
}
}

View File

@ -1,33 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.aot.util;
public abstract class NativeDetector {
// See https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java
private static final boolean imageCode = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
/**
* Returns {@code true} if invoked in the context of image building or during image runtime, else {@code false}.
*/
public static boolean inNativeImage() {
return imageCode;
}
}

View File

@ -1,9 +0,0 @@
[
{
"name": "com.alibaba.boot.nacos.aot.context.NacosPropertySourcePostProcessorForAot",
"allDeclaredClasses": true,
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true
}
]

View File

@ -1,15 +0,0 @@
{
"resources": {
"includes": [
{
"pattern": "\\QMETA-INF/spring.handlers\\E"
},
{
"pattern": "\\QMETA-INF/spring.schemas\\E"
},
{
"pattern": "\\QMETA-INF/schemas/nacos.xsd\\E"
}
]
}
}

View File

@ -1,2 +0,0 @@
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
com.alibaba.boot.nacos.aot.hint.NacosAnnotationBeanRegistrationAotProcessor

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-parent</artifactId> <artifactId>nacos-spring-boot-parent</artifactId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../nacos-spring-boot-parent</relativePath> <relativePath>../nacos-spring-boot-parent</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -54,21 +54,8 @@
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<optional>true</optional> <optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<!-- Test Dependencies --> <!-- Test Dependencies -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -16,6 +16,7 @@
package com.alibaba.boot.nacos.common; package com.alibaba.boot.nacos.common;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -29,11 +30,13 @@ public class PropertiesUtils {
public static Map<Object, Object> extractSafeProperties(Properties properties) { public static Map<Object, Object> extractSafeProperties(Properties properties) {
Map<Object, Object> result = new HashMap<>(); Map<Object, Object> result = new HashMap<>();
properties.forEach((key, val) -> { Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement().toString();
if (!PropertyKeyConst.SECRET_KEY.equals(key)) { if (!PropertyKeyConst.SECRET_KEY.equals(key)) {
result.put(key, val); result.put(key, properties.get(key));
}
} }
});
return result; return result;
} }

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-project</artifactId> <artifactId>nacos-spring-boot-project</artifactId>
<version>${revision}</version> <version>0.1.4</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
@ -30,14 +30,14 @@
<description>Nacos Spring Boot Parent</description> <description>Nacos Spring Boot Parent</description>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.7</java.version>
<java.source.version>1.8</java.source.version> <java.source.version>1.7</java.source.version>
<java.target.version>1.8</java.target.version> <java.target.version>1.7</java.target.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.3</spring-boot.version> <spring-boot.version>1.4.1.RELEASE</spring-boot.version>
<spring6.framework.version>6.0.8</spring6.framework.version> <nacos.version>0.1.0</nacos.version>
<nacos-spring-context.version>2.1.0-RC</nacos-spring-context.version> <nacos-spring-context.version>0.3.4</nacos-spring-context.version>
<!-- Build args --> <!-- Build args -->
<argline>-server -Xms256m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=128m -Dfile.encoding=UTF-8 <argline>-server -Xms256m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=128m -Dfile.encoding=UTF-8
-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack=true
@ -53,12 +53,23 @@
<maven-release-plugin.version>2.5.3</maven-release-plugin.version> <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version> <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<nacos.version>2.2.1</nacos.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<!-- Spring Boot --> <!-- Spring Boot -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -73,18 +84,6 @@
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
<version>${nacos-spring-context.version}</version> <version>${nacos-spring-context.version}</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -93,12 +92,6 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-aot</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.boot</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-autoconfigure</artifactId> <artifactId>nacos-config-spring-boot-autoconfigure</artifactId>
@ -176,16 +169,6 @@
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build> <build>
<!-- Used for packaging NOTICE & LICENSE to each sub-module jar--> <!-- Used for packaging NOTICE & LICENSE to each sub-module jar-->
<resources> <resources>
@ -282,7 +265,7 @@
<configuration> <configuration>
<rules> <rules>
<requireJavaVersion> <requireJavaVersion>
<version>[1.8,)</version> <version>[1.7,)</version>
</requireJavaVersion> </requireJavaVersion>
<requireProperty> <requireProperty>
<property>project.name</property> <property>project.name</property>

View File

@ -1,105 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ /*
~ * 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.
~ */
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-spring-boot-samples</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>nacos-aot-sample</artifactId>
<packaging>jar</packaging>
<name>Nacos Spring Boot Aot Sample</name>
<description>Nacos Spring Boot Aot Sample</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.25</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<mainClass>com.alibaba.boot.nacos.sample.AotApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -1,32 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.sample;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class AotApplication {
public static void main(String[] args) {
SpringApplication.run(AotApplication.class, args);
}
}

View File

@ -1,65 +0,0 @@
/*
*
* * 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 com.alibaba.boot.nacos.sample.controller;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller
public class AotController {
@NacosInjected
private ConfigService configService;
@NacosInjected
private NamingService namingService;
@NacosValue(value = "${flag:false}", autoRefreshed = true)
private boolean flag;
@ResponseBody
@RequestMapping(value = "/config/get", method = GET)
public String getConfig() throws NacosException {
return configService.getConfig("example", "DEFAULT_GROUP", 5000);
}
@ResponseBody
@RequestMapping(value = "/naming/get", method = GET)
public List<Instance> getNaming(@RequestParam("serviceName") String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
@ResponseBody
@RequestMapping(value = "/flag/get", method = GET)
public boolean getFlag() {
return flag;
}
}

View File

@ -1,36 +0,0 @@
[
{
"name":"[Lcom.sun.management.internal.DiagnosticCommandArgumentInfo;"
},
{
"name":"[Lcom.sun.management.internal.DiagnosticCommandInfo;"
},
{
"name":"com.sun.management.internal.DiagnosticCommandArgumentInfo",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean","boolean","boolean","int"] }]
},
{
"name":"com.sun.management.internal.DiagnosticCommandInfo",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String","java.lang.String","java.lang.String","java.lang.String","java.lang.String","boolean","java.util.List"] }]
},
{
"name":"java.lang.Boolean",
"methods":[{"name":"getBoolean","parameterTypes":["java.lang.String"] }]
},
{
"name":"java.lang.InternalError",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
},
{
"name":"java.util.Arrays",
"methods":[{"name":"asList","parameterTypes":["java.lang.Object[]"] }]
},
{
"name":"sun.instrument.InstrumentationImpl",
"methods":[{"name":"<init>","parameterTypes":["long","boolean","boolean"] }, {"name":"loadClassAndCallAgentmain","parameterTypes":["java.lang.String","java.lang.String"] }, {"name":"loadClassAndCallPremain","parameterTypes":["java.lang.String","java.lang.String"] }, {"name":"transform","parameterTypes":["java.lang.Module","java.lang.ClassLoader","java.lang.String","java.lang.Class","java.security.ProtectionDomain","byte[]","boolean"] }]
},
{
"name":"sun.management.VMManagementImpl",
"fields":[{"name":"compTimeMonitoringSupport"}, {"name":"currentThreadCpuTimeSupport"}, {"name":"objectMonitorUsageSupport"}, {"name":"otherThreadCpuTimeSupport"}, {"name":"remoteDiagnosticCommandsSupport"}, {"name":"synchronizerUsageSupport"}, {"name":"threadAllocatedMemorySupport"}, {"name":"threadContentionMonitoringSupport"}]
}
]

Some files were not shown because too many files have changed in this diff Show More