1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00
This commit is contained in:
fangjian0423
2019-08-14 15:24:08 +08:00
parent 7ce2f7a27e
commit c747d1f5e6
120 changed files with 1318 additions and 1430 deletions

View File

@@ -1,12 +1,11 @@
/*
* 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
* Copyright (C) 2018 the original author or authors.
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -18,30 +17,30 @@ package com.alibaba.cloud.nacos.config.server;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import com.alibaba.cloud.nacos.config.server.environment.NacosEnvironmentRepository;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.alibaba.cloud.nacos.config.server.environment.NacosEnvironmentRepository;
/**
* Nacos Config Server Auto-Configuration
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @since 0.2.0
*/
@ConditionalOnClass(EnableConfigServer.class) // If class of @EnableConfigServer is present in class-path
@ComponentScan(basePackages = {
"com.alibaba.nacos.config.server",
})
@ConditionalOnClass(EnableConfigServer.class) // If class of @EnableConfigServer is
// present in class-path
@ComponentScan(basePackages = { "com.alibaba.nacos.config.server", })
@AutoConfigureBefore(ConfigServerAutoConfiguration.class)
@Configuration
public class NacosConfigServerAutoConfiguration {
@Bean
public NacosEnvironmentRepository nacosEnvironmentRepository() {
return new NacosEnvironmentRepository();
}
@Bean
public NacosEnvironmentRepository nacosEnvironmentRepository() {
return new NacosEnvironmentRepository();
}
}

View File

@@ -1,12 +1,11 @@
/*
* 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
* Copyright (C) 2018 the original author or authors.
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -16,19 +15,20 @@
*/
package com.alibaba.cloud.nacos.config.server.environment;
import com.alibaba.nacos.config.server.model.ConfigInfo;
import com.alibaba.nacos.config.server.service.PersistService;
import static com.alibaba.nacos.config.server.constant.Constants.DEFAULT_GROUP;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
import static com.alibaba.nacos.config.server.constant.Constants.DEFAULT_GROUP;
import com.alibaba.nacos.config.server.model.ConfigInfo;
import com.alibaba.nacos.config.server.service.PersistService;
/**
* Nacos {@link EnvironmentRepository}
@@ -38,48 +38,53 @@ import static com.alibaba.nacos.config.server.constant.Constants.DEFAULT_GROUP;
*/
public class NacosEnvironmentRepository implements EnvironmentRepository {
@Autowired
private PersistService persistService;
@Autowired
private PersistService persistService;
@Override
public Environment findOne(String application, String profile, String label) {
@Override
public Environment findOne(String application, String profile, String label) {
String dataId = application + "-" + profile + ".properties";
String dataId = application + "-" + profile + ".properties";
ConfigInfo configInfo = persistService.findConfigInfo(dataId, DEFAULT_GROUP, label);
ConfigInfo configInfo = persistService.findConfigInfo(dataId, DEFAULT_GROUP,
label);
return createEnvironment(configInfo, application, profile);
}
return createEnvironment(configInfo, application, profile);
}
private Environment createEnvironment(ConfigInfo configInfo, String application, String profile) {
private Environment createEnvironment(ConfigInfo configInfo, String application,
String profile) {
Environment environment = new Environment(application, profile);
Environment environment = new Environment(application, profile);
Properties properties = createProperties(configInfo);
Properties properties = createProperties(configInfo);
String propertySourceName = String.format("Nacos[application : %s , profile : %s]", application, profile);
String propertySourceName = String
.format("Nacos[application : %s , profile : %s]", application, profile);
PropertySource propertySource = new PropertySource(propertySourceName, properties);
PropertySource propertySource = new PropertySource(propertySourceName,
properties);
environment.add(propertySource);
environment.add(propertySource);
return environment;
}
return environment;
}
private Properties createProperties(ConfigInfo configInfo) {
Properties properties = new Properties();
String content = configInfo == null ? null : configInfo.getContent();
if (StringUtils.hasText(content)) {
try {
properties.load(new StringReader(content));
} catch (IOException e) {
throw new IllegalStateException("The format of content is a properties");
}
}
return properties;
}
private Properties createProperties(ConfigInfo configInfo) {
Properties properties = new Properties();
String content = configInfo == null ? null : configInfo.getContent();
if (StringUtils.hasText(content)) {
try {
properties.load(new StringReader(content));
}
catch (IOException e) {
throw new IllegalStateException("The format of content is a properties");
}
}
return properties;
}
private static String[] of(String... values) {
return values;
}
private static String[] of(String... values) {
return values;
}
}

View File

@@ -1,12 +1,11 @@
/*
* 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
* Copyright (C) 2018 the original author or authors.
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -32,16 +31,16 @@ import org.springframework.context.annotation.Bean;
@EnableConfigServer
public class NacosConfigServerBootstrap {
public static void main(String[] args) {
System.setProperty("nacos.standalone", "true");
SpringApplication.run(NacosConfigServerBootstrap.class);
}
public static void main(String[] args) {
System.setProperty("nacos.standalone", "true");
SpringApplication.run(NacosConfigServerBootstrap.class);
}
@Bean
public ApplicationRunner applicationRunner() {
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
System.out.println("Running...");
};
}
return args -> {
System.out.println("Running...");
};
}
}