Spring config server cannot be disabled for local development - java

I was updating microservice to the newest Springboot - for now it is 2.5.5 with cloud version 2020.0.4. However there were some changes related to connection with config server. Previously config server configuration looked like this:
spring:
cloud:
config:
uri: ${CONFIG_SERVER_URI:http://localhost:8888}
fail-fast: ${CONFIG_FAIL_FAST:true}
However now it's required to provide it this way:
spring:
config:
import: configserver:${CONFIG_SERVER_URI:http://localhost:8888}
And that would be perfectly fine, except I am not able to run microservice locally without connecting to config server.
Previously I did it like this:
spring:
cloud:
config:
enabled: false
And it was perfectly fine, I had separate application-local.yaml file and had what I wanted.
Now I tried this (according to what is said in documentation):
spring:
cloud:
config:
import-check:
enabled: false
enabled: false
config:
import: "optional:configserver:http://localhost:8888"
But once I run microservice locally, I get only single log like this:
Connected to the target VM, address: '127.0.0.1:59759', transport: 'socket'
And basically nothing more, seems like it keeps trying to connect to config server, because if I run config server and after this try to run my microservice, it's working fine. Additional weird thing is that IntelliJ is showing me that it doesn't recognize this import-check property.
As for dependencies I have only implementation 'org.springframework.cloud:spring-cloud-starter-config:3.0.5' related to config server.
Is there some way to run microservice locally so that it's not connecting to config server using Spring Boot 2.4+? I don't want to use bootstrapping and providing additional dependency, since there is no point to use legacy stuff.

Here is the configuration I have for application.yaml in /src/main/resources folder.
---
spring:
config:
activate:
on-profile: "!config-local"
import: configserver:http://config-server.example.com:8888/
cloud:
config.fail-fast: true
---
spring:
config:
activate:
on-profile: "config-local"
import: optional:configserver:http://localhost:8888/
cloud:
config:
enabled: false
And in Intellij IDEA Run config, I set the active Profile as config-local this works with Springboot 2.5.5 and
ext {
set('springCloudVersion', "2020.0.4")
}

Related

Profile Groups in Spring Boot 3 not working

After the migration to Spring Boot 3 it's not possible to activate multiple profiles in profile specific files. Instead Profile Groups should be used.
Unfortunately I can't get them to work, here is the snippet from my yml config:
spring:
profiles:
group:
local: debuglogin, profile_a, profile_b, profile_c
I have tried this in application-local.yml. Did anyone had the same experience?
I've just found the issue. I was adding this code block in the profile specific file like application-local.yml instead it should be added in the application.yml:
spring:
profiles:
group:
local: debuglogin, profile_a
integration-test: debuglogin, profile_b
this should work if added in the application.yml but not in profile specific files.

Springboot ServerConfig Client

I have the following in my app properties
spring:
main:
banner-mode: off
allow-bean-definition-overriding: true
config:
uri: http://localhost:8890
I have this class:
#RefreshScope
#Configuration
#Getter
public class Service {
#Value("${Some Value}")
Boolean val;
}
The problem, is that my app does not grab the configuration from the localhost running configuration server. I can tell you that my local config server is working fine and that the config is visible in a browser
Have you added 'Spring cloud config client' dependency ?
And also add some properties in client service to make connection with config server ,
spring.cloud.config.uri=http://localhost:8888(Port of your
server)
spring.cloud.config.name=user-service-ws(properties file
name in your remote)
I have found the solution.
Since spring boot 2.4 everything has changed. We must specify to bootstrap the cloud config. This is done using the dependency such as:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>3.1.4</version>
</dependency>
spring:
main:
banner-mode: off
allow-bean-definition-overriding: true
application:
name: myApp
config:
import: optional:configserver:http://localhost:8890/
cloud:
config:
name: some name
Very important to do this: optional:configserver:
Thanks everyone and hope it is useful to everyone.

Quarkus Consul Config: how to inject application.yaml file from Consul Agent?

I am using Quarkus inside a microservice Java application.
I recently started to migrate from Spring Boot to Quarkus itself.
I am having some trouble while migrating "Spring Cloud Consul" to "Quarkus Consul Config". In order to be more specific, I am getting the following error:
java.lang.RuntimeException: Key 'my/consul/path/application.yaml' not found in Consul
at io.quarkus.consul.config.runtime.ConsulConfigSourceProvider$1.accept(ConsulConfigSourceProvider.java:66)
at io.quarkus.consul.config.runtime.ConsulConfigSourceProvider$1.accept(ConsulConfigSourceProvider.java:56)
at io.smallrye.context.impl.wrappers.SlowContextualConsumer.accept(SlowContextualConsumer.java:21)
at io.smallrye.mutiny.operators.uni.UniOnItemConsume$UniOnItemComsumeProcessor.invokeEventHandler(UniOnItemConsume.java:77)
at io.smallrye.mutiny.operators.uni.UniOnItemConsume$UniOnItemComsumeProcessor.onItem(UniOnItemConsume.java:42)
at io.smallrye.mutiny.operators.uni.UniOnItemTransform$UniOnItemTransformProcessor.onItem(UniOnItemTransform.java:43)
at io.smallrye.mutiny.vertx.AsyncResultUni.lambda$subscribe$1(AsyncResultUni.java:35)
(...)
Inside my Consul instance, the key my/consul/path/application.yaml corresponds to an application.yaml external file that I would like to import from there during the startup phase.
Below you can find my consul config (application.yaml):
quarkus:
application:
name: myapplication
consul-config:
enabled: true
properties-value-keys: my/consul/path/application.yaml
agent:
host-port: http://localhost:9500
prefix: myappprefix
If I try to switch from properties-value-keys to properties-raw-value-keys, I see that my property is not being injected inside my application context:
#ConfigProperty(name = "consultest")
String test;
java.util.NoSuchElementException: SRCFG00014: The config property consultest is required but it could not be found in any config source
Below you can find the application.yaml content (located on Consul):
consultest: testtest
The intent, here, is to delegate application.yaml properties to Consul, divided by environment (dev, test, prod).
I would like to threat my local application.yaml file (located in src/main/resources) as a bootstrap.yaml file, similarly to Spring Boot approach.
How could this be done with Quarkus? Thank you a lot for your support.

Disable GraphiQL playground on production in spring boot application

I have added pom dependency in the spring boot application for graphiql playground with the "graphiql-spring-boot-starter-5.0.2.jar" dependency.
Now I would like to disable the playground for the production environment.
And have tried with spring boot applications as below, but none of these options working to disable the GraphiQL endpoint.
graphiql.enabled= false
dgs.graphql.graphiql.enabled=false
Could you please suggest how we could disable GraphiQL?
You can actually add the below configuration in your production profile inside application.yml or application.properties to disable the playground in the production.
I have tested it with com.graphql-java:playground-spring-boot-started:11.0.0
In application.yml
graphql:
playground:
enabled: false
In application.properties
graphql.playground.enabled = false

convert application.yml file to application.properties

I have my application.yml file as below. How to convert it to application.properties
I am trying it but how can i write multiple properties in same file. Its giving me duplicate kery error.
---
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2/eureka/
---
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1/eureka/
IntelliJ and other IDEs provide plugins for the same.
For eg- https://plugins.jetbrains.com/plugin/13804-convert-yaml-and-properties-file
Install the plugin, right click on your yaml or properties file and choose - "Convert yaml and properties file".
With Spring Boot 2.4, there's the possibility to use the switch spring.config.activate.on-profile for this purpose, everything defined after spring.config.activate.on-profile=myprofile will only be set when the active profile is set to myprofile. In the given example, you would do as follows:
#-- Peer1 Config
spring.config.activate.on-profile=peer1
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
#-- Peer2 Config
spring.config.activate.on-profile=peer2
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1/eureka/
See https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4 for more information.
You'll need to create differents files, example:
application-dev.properties
application-prod.properties
application-test.properties
And then you deffine your active profile in the application.properties with:
spring.profiles.active=dev
When using properties file, you cannot have multiple "sections" per profile in the same file, this is a feature available only with Yaml.
You will have to create several properties file, one per profile, as described here : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment
To do the same thing with properties files, you can use application-${profile}.properties to specify profile-specific values
You will have one main application.properties file containing common values, and then one application-${profile}.properties file per profile containing values that are environment/profile dependent.
Finally, you will have to set the active profile either as a System property when running the application, or directly in your main application.properties file, as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles
to do it manually:
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2/eureka/
will be like this:
spring.profiles=peer1
spring.eureka.instance.hostname=peer1
spring.eureka.client.serviceUrl.defaultZone=http://peer2/eureka/

Categories

Resources