Springboot ServerConfig Client - java

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.

Related

Spring config server cannot be disabled for local development

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")
}

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

Spring not picking up changes to ConfigMap

I have an application that is using Springboot, I am trying to allow on the fly configuration updates using the following.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1-RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
I have tried to follow this article https://medium.com/swlh/kubernetes-configmap-confuguration-and-reload-strategy-9f8a286f3a44, and have managed to get Spring pulling the config from the ConfigMap, however, if I update the ConfigMap when the application is running, spring does not pick it up. This is my bootstrap.yml
spring:
cloud:
kubernetes:
config:
enabled: true
sources:
- namespace: default
name: hello-world
reload:
enabled: true
mode: event
strategy: refresh
I have also tried using mode: polling, but still no change. And I have added the view role.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ include "hello-world.fullname" . }}-view
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io
subjects:
# Authorize specific service accounts:
- kind: ServiceAccount
name: {{ include "hello-world.serviceAccountName" . }}
namespace: default
I am thinking maybe it's the way I am loading my configuration in Java?
#ConfigurationProperties(prefix = "spring.app")
#Bean
public Properties appProperties() {
return new Properties();
}
#Autowired
#Qualifier("appProperties")
private Properties props;
My ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: hello-world
data:
application.yml: |-
spring:
app:
message: Hello
I am then accessing values like props.getProperty("message")
UPDATE:
So I managed to get the changes picked up, by enabling the actuator refresh endpoint:
management:
endpoint:
restart:
enabled: true
But now I have a new question, is this necessary? Is there any way to get this to work without including the actuator?
Please make sure you have the following dependency in your POM with a version.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
I had similar issue, which is resolved after adding this.
make sure to add #RefreshScope to the bean that you want to hot reload with inject with values from config maps.

Unable to view Eureka Dashboard

I have a simple spring boot application on which I am trying to run a Eureka server.
This is my main application file.
#SpringBootApplication
#EnableEurekaServer
public class NetflixEurekaNamingServerApplication {
public static void main(String[] args) {
SpringApplication.run(NetflixEurekaNamingServerApplication.class, args);
}
}
This is my application property file
spring.application.name=netflix-eureka-naming-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Dependencies used.
spring boot version : 2.2.4.RELEASE
spring cloud version : Hoxton.SR1
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
But when I run the application and visits the localhost:8761 then I get an Whitelabel page error showing 404 NOT FOUND.
and when I changed the configuration according to this post unable to render eureka dashboard
then it started showing me an XML page instead of the dashbord.
Can someone help me out here ?
Eureka Dashboard should be available unter http://localhost:8761/eureka/
I got the issue.
Just add these two properties in your application.properties file and it will work.
spring.freemarker.template-loader-path= classpath:/templates/
spring.freemarker.prefer-file-system-access= false
I am using spring boot version 2.2.4.RELEASE and springCloudVersion Hoxton.SR1

Categories

Resources