Я следовал этому руководству от Baeldung по настройке сервера и клиента Spring Cloud Config. Я полагаю, что я правильно настроил сервер, поскольку при переходе на http://localhost:8888/service-config/development/master, по-видимому, правильно будет отображаться json с определенным user.role:
{"name":"service-config","profiles":["development"],"label":"master","version":"d30a6015a6b8cb1925f6e61cee22721f331e5783","state":null,"propertySources":[{"name":"file:///C:.../git/service-config-data/service-config-development.properties","source":{"user.role":"Developer"}},{"name":"file:///C:.../git/service-config-data/service-config.properties","source":{"user.role":"Developer"}}]}
Однако я не могу правильно подключить клиент Spring Cloud Config к серверу - клиент не может работать с ним и не может устранить ошибки заполнителя в аннотации @Value("${user.role}")
. Я попытался добавить значение по умолчанию к аннотации, а затем вручную обновить значения (используя аннотацию @refreshscope
), но безрезультатно.
Я просмотрел журналы отладки и трассировки, запустив инструменты с флагами --debug --trace
, но не могу найти, когда / если клиент выполняет вызов к серверу и какой файл config-data .properties файл на самом деле является сервером находясь в поиске. Я немного растерялся, как поступить.
Вот мой сервер application.properties
:
spring.application.name=service-config
server.port=8888
spring.cloud.config.server.git.uri=file:///${user.home}/git/service-config-data
spring.cloud.config.server.git.clone-on-start=false
spring.security.user.name=root
spring.security.user.password=toor
А внутри папки service-config-data
есть файлы с этим внутри:
user.role=Developer
Я пробовал файлы с именами service-config.properties, service-config-client.properties, service-config-development.properties и service-config-client-development.properties, и ни один из них, похоже, не прошел через user.role
.
Вот мой клиент:
@SpringBootApplication
@RestController
@RefreshScope
public class ServiceConfigClient {
@Value("${user.role:unknown}")
private String role;
@RequestMapping(
value = "/whoami/{username}",
method = RequestMethod.GET,
produces = MediaType.TEXT_PLAIN_VALUE)
public String whoami(@PathVariable("username") String username) {
return String.format("Hello! You're %s and you'll become a(n) %s...\n", username, role);
}
}
И мой клиент bootstrap.properties
:
spring.application.name=service-config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=toor
management.endpoints.web.exposure.include=*
Как правильно подключить клиент к серверу, чтобы он мог успешно получить данные конфигурации? Спасибо.