속성의 최종 목록 나열 - Spring Cloud Config Server
애플리케이션의 /env 끝점에 액세스하면 Spring Cloud Config Server가 여러 프로파일을 수락하고 모든 프로파일에 대한 속성을 반환합니다.응답에는 각 프로파일에 고유한 속성이 나열됩니다.동일한 속성이 두 개의 서로 다른 속성 파일에 있는 경우 마지막으로 정의된 속성이 우선합니다.응용 프로그램에서 사용할 속성 키 및 값의 최종 목록을 가져올 수 있는 방법이 있습니까?
Cloud Config Client 애플리케이션의 경우
여러 가지 방법을 시도해 본 결과 (우연히) 다음과 같은 것을 발견했습니다.
GET /env/.*
구성 속성의 전체 목록을 반환합니다.
클라우드 구성 서버 애플리케이션의 경우
이것은 이미 구현되었지만 잘 문서화되지 않은 것으로 드러났습니다.요청만 하면 됩니다.json
,yml
또는properties
패턴에 따라:
/{application}-{profile}.{ext}
/{label}/{application}-{profile}.{ext}
import java.util.properties;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
public class MyClass {
@Autowired
private Environment env;
Properties getProperties() {
Properties props = new Properties();
CompositePropertySource bootstrapProperties = (CompositePropertySource) ((AbstractEnvironment) env).getPropertySources().get("bootstrapProperties");
for (String propertyName : bootstrapProperties.getPropertyNames()) {
props.put(propertyName, bootstrapProperties.getProperty(propertyName));
}
return props;
}
}
미안...여기서 질문에 대답하는 것은 이번이 처음입니다.저는 같은 문제를 조사하던 중 우연히 발견했기 때문에 이 질문에 답변하기 위해 특별히 계정을 만들었습니다.저는 저에게 맞는 해결책을 찾아 공유하기로 결정했습니다.
수행된 작업에 대한 제 설명은 다음과 같습니다.
새 "속성" 개체를 초기화합니다(HashMap 또는 원하는 다른 개체일 수 있음).
CompositePropertySource 개체인 "bootstrapProperties"에 대한 속성 소스를 검색합니다.이 속성 원본에는 로드된 모든 응용 프로그램 속성이 포함됩니다.
CompositePropertySource 객체의 "getPropertyNames" 메서드에서 반환된 모든 속성 이름을 루프하고 새 속성 항목을 만듭니다.
속성 개체를 반환합니다.
이는 스프링 프레임워크의 의도적인 한계로 보입니다.
여기 참조
당신은 그것을 해킹하고 PropertySources 인터페이스를 주입한 다음 모든 개별 PropertySource 객체를 루프할 수 있지만, 당신은 당신이 찾고 있는 속성을 알아야 합니다.
Spring Boot을 사용하면 구성을 외부화하여 다른 환경에서 동일한 응용 프로그램 코드로 작업할 수 있습니다.속성 파일, YAML 파일, 환경 변수 및 명령줄 인수를 사용하여 구성을 외부화할 수 있습니다.속성 값은 @Value 주석을 사용하여 콩에 직접 주입하거나, Spring's Environment 추상화를 통해 액세스하거나, @ConfigurationProperties를 통해 구조화된 객체에 바인딩할 수 있습니다.
Spring Boot은 값을 합리적으로 재정의할 수 있도록 설계된 매우 특정한 PropertySource 순서를 사용합니다.속성은 다음 순서로 고려됩니다.
- 홈 디렉토리의 Devtools 글로벌 설정 속성(devtools가 활성화된 경우 ~/.spring-boot-devtools.properties).
- @TestProperty테스트에 대한 원본 주석.
- @스프링 부츠테스트에서 test#properties 주석 속성을 테스트합니다.
- 명령줄 인수입니다.
- SPRING_APPLICATION_JSON의 속성(환경 변수 또는 시스템 속성에 내장된 인라인 JSON)
- ServletConfig init 매개 변수입니다.
- ServletContext in 매개 변수입니다.
- java:comp/env의 JNDI 특성입니다.
- Java 시스템 속성(System.getProperties()).
- OS 환경 변수.
- 임의의 속성만 있는 임의 값 속성 원본입니다.*.
- 패키지 병 외부의 프로파일별 애플리케이션 속성(application-{profile} 속성 및 YAML 변형)
- 병 내부에 패키지된 프로필별 응용 프로그램 속성(application-{profile}.properties 및 YAML 변형)
- 패키지 병 외부의 응용 프로그램 속성(application.properties 및 YAML 변형).
- 병 내부에 패키지화된 애플리케이션 속성(application.properties 및 YAML 변형).
- @Property@Configuration 클래스에 대한 주석 원본.
- 기본 속성(SpringApplication을 사용하여 지정됨).setDefaultProperties)를 선택합니다.
아래 프로그램은 스프링 부팅 환경에서 속성을 인쇄합니다.
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.StandardServletEnvironment;
@Component
public class EnvironmentLogger extends ApplicationObjectSupport {
@Override
protected void initApplicationContext(ApplicationContext context) throws BeansException {
Environment environment = context.getEnvironment();
String[] profiles = environment.getActiveProfiles();
if(profiles != null && profiles.length > 0) {
for (String profile : profiles) {
System.out.print(profile);
}
} else {
System.out.println("Setting default profile");
}
//Print the profile properties
if(environment != null && environment instanceof StandardServletEnvironment) {
StandardServletEnvironment env = (StandardServletEnvironment)environment;
MutablePropertySources mutablePropertySources = env.getPropertySources();
if(mutablePropertySources != null) {
for (PropertySource<?> propertySource : mutablePropertySources) {
if(propertySource instanceof MapPropertySource) {
MapPropertySource mapPropertySource = (MapPropertySource)propertySource;
if(mapPropertySource.getPropertyNames() != null) {
System.out.println(propertySource.getName());
String[] propertyNames = mapPropertySource.getPropertyNames();
for (String propertyName : propertyNames) {
Object val = mapPropertySource.getProperty(propertyName);
System.out.print(propertyName);
System.out.print(" = " + val);
}
}
}
}
}
}
}
}
언급URL : https://stackoverflow.com/questions/44122251/list-final-list-of-properties-spring-cloud-config-server
'programing' 카테고리의 다른 글
테마를 사용해야 합니다.이 활동의 AppCompat 테마(또는 하위) (0) | 2023.07.01 |
---|---|
Firebase에서의 다대다 관계 (0) | 2023.07.01 |
VBA를 사용하여 Excel 2007에서 셀을 투명하게 만드는 방법 (0) | 2023.07.01 |
Git에서 파일 하나만 풀 수 있습니까? (0) | 2023.07.01 |
원시 유형을 확장하는 TypeScript에서 공칭 유형을 만드는 방법이 있습니까? (0) | 2023.07.01 |