我正在使用spring-cloud-config-server,我不希望Git后端或基于文件系统的后端。我希望返回自定义键值对。
答案 0 :(得分:0)
I found solution of this .
1) on the application.properties set spring.profiles.active=native
2) Create CustomEnvironmentRepository -- Refer code on #A
3) Register CustomEnvironmentRepository Bean -- Refer code on #B
#A -
import java.util.HashMap;
import java.util.Map;
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.core.Ordered;
public class CustomEnvironmentRepository implements
EnvironmentRepository, Ordered
{
@Override
public Environment findOne(String application, String profile, String label)
{
Environment environment = new Environment(application, profile);
final Map<String, String> properties = new HashMap<>();
properties.put("mycustomPropertyKey", "myValue");
environment.add(new PropertySource("mapPropertySource", properties));
return environment;
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
#B
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Bean;
@EnableConfigServer
@SpringBootApplication
public class MyCloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MyCloudConfigApplication.class, args);
}
@Bean
public EnvironmentRepository environmentRepository() {
return new CustomEnvironmentRepository();
}
}
Test by invoking - http://localhost:9092/my-cloud-config.properties
You will see
mycustomPropertyKey: myValue
My Original Goal is to set some properties from Azure Key Vault , I guess integrating AzureKey Vault in CustomEnvironmentRepository , I should be able to achieve this
Application Properties File will look like
server.port=<YOUR_PORT>
spring.profiles.active=native
azure.keyvault.uri=<YOUR_AZURE_URI_CAN_BE_FOUND_IN_AZURE_PORTAL>
azure.keyvault.client-id=<YOUR_AZURE_CLIENT_ID_CAN_BE_FOUND_IN_AZURE_PORTAL>
azure.keyvault.client-key=<YOUR_AZURE_CLIENT_KEY_CAN_BE_FOUND_IN_AZURE_PORTAL>
IN POM Use these Dependencies
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>