I am trying set Spring Cloud Config Server, but the service config server, it the running on port 8888 which is correct, and another service should run on port 18060, but for reason when I startup, it allocate port 8080 for me and the return a warning "Could not locate PropertySource: label not found", what should I do? Thank you !!!
Add #EnableConfigServer at configuration class level or main class of spring boot application and restart the service .it will work .
In my case it was because I had to set the default branch of my github URI (the branch of the repository set for spring.cloud.config.server.git.uri) in the config server by setting the following to the application.properties
spring.cloud.config.server.git.default-label=main
I ran into similar issue, when I run my server locally I see in the logs only on the stratup
o.s.c.c.c.ConfigServicePropertySourceLocator - Could not locate PropertySource: label not found
It turned out this is a problem with the health check for the config server, when health check is triggered to config server an empty environment is used so the application name is empty hence the spring cloud client will try to access {config-server}/application since application is the default application name, see issue for more info on this.
To solve the issue I just disabled the health check for config server
health.config.enabled=false
Try this one. I had the same issue and solved.
First enable debug mode in your app.(In your property file: logging.level.=DEBUG . This is only to make sure the issue is same as mine Or you may have any clue of where it can go wrong.)
Then start the application and see the logs. The log shows the configured server URI and what the URL to get all property resources.
Compare both URL s - the one in the log and your Configuration server URI.
Issue is that mistakenly the URL defined the property file can have empty space at the end. (This can happen when you copy past from somewhere ) Example:
the value for spring.cloud.config.uri=http:localhost:< port >< additional empty space >
If this is the case, the client's log shows localhost:/20%20%/ < app name > / < profile >
Simply remove the post empty space. Then it should work !
In your bootstrap.yml file
Add these lines
spring:
cloud:
config:
enabled: true
uri: http://localhost:8888 ##this is the link to the server config
label: main ##this is what you are missing (name of the git repo branch)
application:
name: currency-spring-cloud-config-client
profiles:
active: testing
server:
port: 8600
spring:
cloud:
config:
enabled: true
uri:
- http://localhost:9196 ### your port for cloud config server
label: main ### your git repo branch name
In my case my server was fetching cofnigs from github repository, and I forgot that I made it private :)
Related
I am currently trying to run a Java Spring application in a Docker container. This has worked without any problems so far. But now I want that in the application.properties there are no fixed values but only placeholders, which are passed as environment variables to the container. According to the Spring Docs this should be possible but when I try this I always get the error that no database connection can be established to the placeholder ${DB_CONFIG} (Caused by: java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, ${DB_CONFIG}).
I already tried to pass the application.properties externally, instead of an env file pass the variables directly as Docker environment variable and instead of a custom variable (DB_CONFIG) for the JDBC url pass the Spring variable (SPRING_DATASOURCE_URL). Neither variant worked for me.
I hope I got my point across correctly and you guys can help me out.
For your information: I don't maintain the source code, I just get it from a CI/CD, copy the changed application.properties to its place and compile the project.
KR,
BlackRose01
docker-compose file:
version: '3'
services:
arrowhead-serviceregistry:
container_name: arrowhead-serviceregistry
image: 'openjdk:11-jre-slim-buster'
restart: always
env_file: '.env'
ports:
- 8443:8443/tcp
volumes:
- ./arrowhead-serviceregistry-4.3.0.jar:/service.jar
depends_on:
- mysql
command:
-java -noverify -XX:TieredStopAtLevel=1 -jar /service.jar
application.properties file:
############################################
### APPLICATION PARAMETERS ###
############################################
# Database connection (mandatory)
# Change the server timezone if neccessary
spring.datasource.url=${DB_CONFIG}
spring.datasource.username=${SERVICEREGISTRY_DB_USERNAME}
spring.datasource.password=${SERVICEREGISTRY_DB_PASSWORD}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# use true only for debugging
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
# Service Registry web-server parameters
server.address=0.0.0.0
server.port=${SERVICEREGISTRY_PORT}
domain.name=${SERVICEREGISTRY_ADDRESS}
domain.port=${SERVICEREGISTRY_PORT}
############################################
### CUSTOM PARAMETERS ###
############################################
# Name of the core system
core_system_name=SERVICE_REGISTRY
# Show all request/response in debug log
log_all_request_and_response=${SERVICEREGISTRY_LOG_REQUESTS}
# Service Registry has an optional feature to ping service providers in a fixed time interval,
# and remove service offerings where the service provider was not available
# use this feature (true/false)
ping_scheduled=${SERVICEREGISTRY_PING_ENABLE}
# how much time the Service Registry should wait for the ping response (in milliseconds)
ping_timeout=${SERVICEREGISTRY_PING_TIMEOUT}
# how frequently should the ping happen, in minutes
ping_interval=${SERVICEREGISTRY_PING_INTERVAL}
# Service Registry has an optional feature to automatically remove service offerings, where the endOfValidity
# timestamp field is in the past, meaning the offering expired
# use this feature (true/false)
ttl_scheduled=${SERVICEREGISTRY_TTL_ENABLE}
# how frequently the database should be checked for expired services, in minutes
ttl_interval=${SERVICEREGISTRY_TTL_INTERVAL}
# Interface names has to follow this format <PROTOCOL>-<SECURITY>-<FORMAT>, where security can be SECURE or INSECURE and protocol and format must be a sequence of letters, numbers and underscore.
# A regexp checker will verify that. If this setting is set to true then the PROTOCOL and FORMAT must come from a predefined set.
use_strict_service_intf_name_verifier=${SERVICEREGISTRY_STRICT_INTERFACE_NAMES}
############################################
### SECURE MODE ###
############################################
# configure secure mode
# Set this to false to disable https mode
server.ssl.enabled=${SSL_ENABLED}
server.ssl.key-store-type=${SERVICEREGISTRY_SSL_KEYSTORE_TYPE}
server.ssl.key-store=${SERVICEREGISTRY_SSL_KEYSTORE}
server.ssl.key-store-password=${SERVICEREGISTRY_SSL_KEYSTORE_PASSWORD}
server.ssl.key-alias=${SERVICEREGISTRY_SSL_KEY_ALIAS}
server.ssl.key-password=${SERVICEREGISTRY_SSL_KEY_PASSWORD}
server.ssl.client-auth=${SERVICEREGISTRY_SSL_CLIENT_AUTH}
server.ssl.trust-store-type=${SERVICEREGISTRY_SSL_TRUSTSTORE_TYPE}
server.ssl.trust-store=${SERVICEREGISTRY_SSL_TRUSTSTORE_PATH}
server.ssl.trust-store-password=${SERVICEREGISTRY_SSL_TRUSTSTORE_PASSWORD}
#If true, http client does not check whether the hostname is match one of the server's SAN in its certificate
#Just for testing, DO NOT USE this feature in production environment
disable.hostname.verifier=${SERVICEREGISTRY_DISABLE_HOSTNAME_VERIFIER}
Environment file:
# Basic settings
DB_CONFIG=jdbc:mysql://127.0.0.1:3306/arrowhead?serverTimezone=Europe/Berlin
SSL_ENABLED=false
# Service Registry settings
SERVICEREGISTRY_ADDRESS=127.0.0.1
SERVICEREGISTRY_PORT=8443
SERVICEREGISTRY_DB_USERNAME=myUser
SERVICEREGISTRY_DB_PASSWORD=xxx
SERVICEREGISTRY_LOG_REQUESTS=false
SERVICEREGISTRY_PING_ENABLE=false
SERVICEREGISTRY_PING_TIMEOUT=5000
SERVICEREGISTRY_PING_INTERVAL=60
SERVICEREGISTRY_TTL_ENABLE=false
SERVICEREGISTRY_TTL_INTERVAL=10
SERVICEREGISTRY_STRICT_INTERFACE_NAMES=false
SERVICEREGISTRY_DISABLE_HOSTNAME_VERIFIER=false
SERVICEREGISTRY_SSL_KEYSTORE_TYPE=PKCS12
SERVICEREGISTRY_SSL_KEYSTORE=classpath:certificates/service_registry.p12
SERVICEREGISTRY_SSL_KEYSTORE_PASSWORD=123456
SERVICEREGISTRY_SSL_KEY_ALIAS=service_registry
SERVICEREGISTRY_SSL_KEY_PASSWORD=123456
SERVICEREGISTRY_SSL_CLIENT_AUTH=need
SERVICEREGISTRY_SSL_TRUSTSTORE_TYPE=PKCS12
SERVICEREGISTRY_SSL_TRUSTSTORE_PATH=classpath:certificates/truststore.p12
SERVICEREGISTRY_SSL_TRUSTSTORE_PASSWORD=123456
While searching for a solution to my problem, I came across the Spring Cloud Config Server (also just a Spring application that is self-hostable). This provides an interface between the configuration file storage location and the Spring application. The application is set up so that instead of a static application.(properties|yml) file, a bootstrap.yml is stored with the URL to the config server. When the application starts up, it then connects to the Config Server and receives it via a REST interface. The configuration files can be stored in Git/a DB or as a file.
Spring Docs - Cloud Config Server
Spring - Instruction for Implementation
Baeldung - Tutorial
Baeldung - Tutorial Bootstrapping
I have tried to create the environment specific configuration in micronaut
with main application.yml as
micronaut:
application:
name: xyz
server:
port: 9090
environments: local
and local configuration file as
with name as application-local.yml
xyz:
aws:
accessKey: <access_key>
secretKey: <secret_key>
In code I am trying to access as
#Value("${xyz.aws.accessKey}")
I while trying to access them in code getting following error
Message: Error resolving field value [${xyz.aws.accessKey}]. Property doesn't exist or cannot be converted
I found out that there is no such properties in micronaut similar to spring's
spring.profiles.active
here instead we have to pass the external file as VM options instead
-Dmicronaut.environments=local
then it started to work
I have a Spring Boot app that can not be run with a .yml config file.Below is the command that I run:
java -jar /opt/myAppFolder/myApp.war
I have a config folder withing the same location that I run my war file. And in that config,I have a application.yml file that application needs to retrieve some config:
security:
user:
password: password
logging:
level:
org.springframework.security: DEBUG
release:
sourceDir: /$ANY_DIR
targetDir: /$ANY_DIR
users:
- name: $ANY_NAME
pwd : $ANY_NAME
- name: $ANY_NAME
pwd : $ANY_NAME
mail:
host: $ANY_NAME
recipients: $ANY_NAME
subject: $ANY_NAME
body: $ANY_NAME
server:
port: 9000
spring:
profiles:
active: prod
Problem: Once I run the app on command line,the process just hangs.No output,no logging. I have tried to enable debug via command line. No avail.
If I put a file with properties extension,it works.However I want to use .yml since it is more convenient for me to have list of dynamic properties.And why nothing is displayed anway?
Any help appreciated.
Spring Boot Version:1.5.7
OS Version:Ubuntu 3.13.0-24-generic
Finally resolved it.
So here is the story:
I have contacted Spring Boot Team and they have recommended me to upgrade to version 1.5.8.RELEASE.
Once I did that,I had a different issue;application was not hanging anymore,but shutting down without logging anything.
And eventually it turned out to be an error in my logback-spring.xml file. It was malformatted.And in its "prod" profile,I did not have any console appender.Hence no output visible. So I have fixed my file and it started working.
More on my communication with Spring Boot Team:
https://github.com/spring-projects/spring-boot/issues/10711
I am working on a Spring Boot Registration Server(Eureka Server).
Currently it is working with below configuration.
Project Name: registration-service
Inside main method: System.setProperty("spring.config.name", "registration-service");
"yml file":
file name: registration-service
content:
eureka:
instance:
hostname: eureka-server
server:
enableSelfPreservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 2323 # HTTP (Tomcat) port
spring:
application:
name: eureka-server
With above configuration,application start running on 2323.
But if i change spring.config.name,it does not work,start giving connection refused exception.
why it is happening? even though this spring.config.name is no where used in yml file. Should it be necessarily same as project name? or it is specific to #EnableEurekaServer enabled spring boot application.
And in yml we have to write
spring:
application:
name: eureka-server
though in other spring boot application we give name of current project(here it should be registration-service).why we have to write here eureka-server? I know,I am missing something(or a lot of things).please help me in understanding the missing part.
Spring boot by default looks for file application.yml. If u have different profiles in your application it can also look for application-{profilename}.yml. This is the default convention followed.
spring.config.name property is used to override this default behaviour. When u override this property with register-service then spring boot looks for a file register-service.yml and loads config from that.
So your eureka server url which is given in the register-service.yml file may not be available in the default application.yml file. Hence when u change the value Spring boot may not be avle to find the Eureka server url.
Keep the names unchanged, as much as possible. If u have config file as register-service.yml then keep the spring.config.name=register-service. If you change this value then u need to create the new file with config.name value and then add eureka configuration to that again.
We have different config servers per environment. Each spring boot application should target its corresponding config server. I have tried to achieve this by setting profiles in the bootstrap.properties file, e.g.:
spring.application.name=app-name
spring.cloud.config.uri=http://default-config-server.com
---
spring.profiles=dev
spring.cloud.config.uri=http://dev-config-server.com
---
spring.profiles=stage
spring.cloud.config.uri=http://stage-config-server.com
---
spring.profiles=prod
spring.cloud.config.uri=http://prod-config-server.com
And then I set the cla -Dspring.profiles.active=dev but the loaded config server is always the last one set in the file (i.e. prod config server would be loaded in the above settings, and then if prod is removed, stage would be loaded).
Is it possible to set bootstrap profiles for the cloud config server? I followed this example but can't seem to get it working. For what it's worth, these profiles work great to load the correct config (i.e. app-name-dev.properties will load if the dev profile is active), but aren't being pulled from the proper config server.
Specifying different profiles in a single file is only support for YAML files and doesn't apply to property files. For property files specify an environment specific bootstrap-[profile].properties to override properties from the default bootstrap.properties.
So in your case you would get 4 files bootstrap.properties, bootstrap-prod.properties, bootstrap-stage.properties and bootstrap-dev.properties.
However instead of that you could also only provide the default bootstrap.properties and when starting the application override the property by passing a -Dspring.cloud.config.uri=<desired-uri> to your application.
java -jar <your-app>.jar -Dspring.cloud.config.uri=<desired-url>
This will take precedence over the default configured values.
I solved a similar problem with an environment variable in Docker.
bootstrap.yml
spring:
application:
name: dummy_service
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:8888/}
enabled: true
profiles:
active: ${SPR_PROFILE:dev}
Dockerfile
ENV CONFIG_SERVER_URL=""
ENV SPR_PROFILE=""
Docker-compose.yml
version: '3'
services:
dummy:
image: xxx/xxx:latest
restart: always
environment:
- SPR_PROFILE=docker
- CONFIG_SERVER_URL=http://configserver:8888/
ports:
- 8080:8080
depends_on:
- postgres
- configserver
- discovery
#LarryW (I cannot answer on the same comment):
I guess the advantage of explicitly adding the property is that it allows you to add a default value (in this case "dev") in case of not setting up the environment variable.