In this coding exercise for learning microservices, I've created a Netflix Zuul project for service routing my microservices.
Sadly, the /routes endpoint does not seem to be mounted. Everything else seems to be working fine: Defining prefixes and setting up specific routes for my services.
There are no errors on the zuul server log files.
When I try to hit the /routes url on postman, I get an 404 error:
My Zuul application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
#SpringBootApplication
#EnableZuulProxy
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
}
pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <!--should be set to 4.0.0 -->
<groupId>com.booking.system.hotel</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>hotel-gateway-service-server</artifactId>
<name>Hotel Gateway service - zuul</name>
<description>Hotel Gateway service - it uses Netflix Zuul Proxy Server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.booking.system.hotel.zuulsvr.ZuulServerApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
<build>
<finalName>hotel-gateway-service-server</finalName> <!--name of the jar -->
<plugins>
<!-- packages the project as an executable jar, as an Spring Boot application -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- used for running tests at various stages -->
</plugins>
</build>
</project>
bootstrap.yml file:
spring:
application:
name: hotel-gateway-service-server
profiles:
active:
default
cloud:
config:
enabled: true
Zuul configuration file from configuration server:
zuul.ignored-services: "*"
zuul.prefix: /api
zuul.routes.hotel-reservations-service: /reservations/**
zuul.routes.hotel-rooms-service: /rooms/**
docker-compose.yml entry for initializing the zuul gateway server:
hotel-gateway-service-server: #zuul server
image: imageprefix/hotel-gateway-service-server
ports:
- 5555:5555
environment:
PROFILE: "dev"
SERVER_PORT: "5555"
CONFIGSERVER_URI: "http://hotel-configuration-server:8888"
CONFIGSERVER_PORT: "8888"
EUREKASERVER_URI: "http://hotel-service-discovery-server:8761/eureka/"
EUREKASERVER_PORT: "8761"
I don't seem to spot what I am missing.
The actuator base path has changed to /actuator. So you need to use /actuator/routes. It is also not enabled by default.
management.endpoints.web.exposure.include=*
Related
I am developing an application using Spring Boot 2.4.1 and Eureka. I achieved to start up the Eureka Server, but with the Eureka Client I am getting the following error:
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.client.apache4.ApacheHttpClient4
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
... 55 common frames omitted
...
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.cloud.netflix.eureka.CloudEurekaClient.getApplications()" because the return value of "org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration.getEurekaClient()" is null
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.maybeInitializeClient(EurekaServiceRegistry.java:54) ~[spring-cloud-netflix-eureka-client-3.0.0.jar:3.0.0]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.register(EurekaServiceRegistry.java:38) ~[spring-cloud-netflix-eureka-client-3.0.0.jar:3.0.0]
at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.start(EurekaAutoServiceRegistration.java:83) ~[spring-cloud-netflix-eureka-client-3.0.0.jar:3.0.0]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.2.jar:5.3.2]
The pom.xml is the following:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>15</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<groupId>some.group</groupId>
<artifactId>some-artefact</artifactId>
<version>1.0</version>
<name>some-name</name>
<description>some-description</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The following is the application.yml:
spring:
application:
name: #project.artifactId#
logging:
file.name: ../logs/#project.artifactId#.log
file.max-size: 10MB
server:
port: 8080
And the main class:
#SpringBootApplication
#ComponentScan("com.something")
#EnableDiscoveryClient
public class SpigaConnectorServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpigaConnectorServiceApplication.class, args);
}
#RestController
class ServiceInstanceRestController {
#Autowired
private DiscoveryClient discoveryClient;
#RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
#PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}
}
The following is the server pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>15</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<groupId>com.ptesa.cloud</groupId>
<artifactId>pt-eureka-server</artifactId>
<version>1.0</version>
<name>pt-eureka-server</name>
<description>Registry and discovery of services</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The application.yml of the server:
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
And the class that start the server:
#EnableEurekaServer
#SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(com.ptesa.cloud.EurekaServerApplication.class);
}
}
When using Spring Boot version 2.3.7.RELEASE I don't have this problem. The application starts almost normally. Just get the following warning:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/Jucaalpa/.m2/repository/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Is there a way to run Eureka Client with Spring Boot 2.4.1. Do you think it's better to use Spring Boot 2.3.7 because of compatibilities issue like the one I am having?
Thanks you.
I found that adding the following dependency, the problem doesn't occur.
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client4</artifactId>
<version>1.19.4</version>
</dependency>
May help: I changed the project SDK version from 16 to 1.8 and the problem disappeared! (in Intellij go to Project Structure)
In client yaml file, add the below config
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
My goal is to get the config for world-service from a config-service.
The architecture:
config-service with dependency spring-cloud-config-server at localhost:8888
world-service with dependency the spring-web and spring-cloud-starter-config.
What I have done:
I have set up the Config Server and send a GET request to http://localhost:8888/hello-service/master and the config server get the hello-service.properties from the config-repo repository. (If you need the config-service's source code, I will push it to this repository.)
My expected result:
The world-service use port 8081.
My actual result:
The world-service use port 8080.
bootstrap.properties
spring.application.name=world-service
spring.cloud.config.uri=http://localhost:8888
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>world-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>world-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0-M5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
With Spring Cloud 2020, they made a change in how bootstrap works and you have to include a new starter: spring-cloud-starter-bootstrap.
I spent a day on it and finally found a solution. It may help others
You need to add new dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
According to the Spring Cloud 2020.0
Bootstrap, provided by spring-cloud-commons, is no longer enabled by
default. If your project requires it, it can be re-enabled by
properties or by a new starter.
To re-enable by properties set spring.cloud.bootstrap.enabled=true or
spring.config.use-legacy-processing=true. These need to be set as an
environment variable, java system property or a command line argument.
The other option is to include the new spring-cloud-starter-bootstrap
(in your POM file).
I used the first option and that worked for me.
Spring Boot 2.4 introduced a new way to import configuration data via the spring.config.import property. This is now the default way to bind to Config Server.
To connect to config server set the following in application.yml:
spring:
application:
name: APPLICATION_NAME
config:
import: optional:configserver:http://USER:PASSWORD#MY_HOST:PORT/
You can see more details in: https://docs.spring.io/spring-cloud-config/docs/3.0.0/reference/html/#config-data-import
I am implementing Spring Cloud config, but the clients are not fetching the config automatically...
When I start up my client gets the data from the server properly, but never retrieves it again.
This is my CloudConfig Server.
#EnableDiscoveryClient
#EnableConfigServer
#SpringBootApplication
public class CloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigApplication.class, args);
}
}
aplication.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9091/eureka
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
bootstrap: true
health:
enabled: true
native:
searchLocations: file:${user.dir}/src/main/resources/configs/
profiles:
active: native
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>CloudConfig</groupId>
<artifactId>CloudConfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CloudConfig</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- 3a) Dependency for spring-cloud-config-Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 3b) Dependency for testing boot projects -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/src/main/resources/config/CloudConfigClient.yaml
test: aaaaa
test2: 1111
SpringCloud Client
#EnableScheduling
#EnableDiscoveryClient
#SpringBootApplication
#EnableAutoConfiguration
public class CloudConfigClient {
#Value("${test}")
private String myProperty;
public static void main(String[] args) {
SpringApplication.run(CloudConfigClient.class, args);
}
#Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
#Scheduled(fixedDelay =1000)
public void printProperty() {
System.out.println("Value of property \"myProperty\": " + myProperty);
}
}
bootstrap.yaml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9091/eureka
server:
port: 7004
datasource:
driver-class-name: org.h2.Driver
application:
name: CloudConfigClient
cloud:
config:
uri: http://localhost:8888
fail-fast: true
max-attempts: 20
max-interval: 15000
initial-interval: 10000
Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
This is the ouput log:
2020-11-20 23:33:12.747 INFO 16764 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-11-20 23:33:13.510 INFO 16764 --- [ restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=CloudConfigClient, profiles=[default], label=null, version=null, state=null
2020-11-20 23:33:13.510 INFO 16764 --- [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='file:D:/CloudConfig/src/main/resources/configs/CloudConfigClient.yaml'}]}
...
...
Value of property "myProperty": aaaaa
Value of property "myProperty": aaaaa
if you want your config to be refresh you have to use #RefreshScope spring doc the config won't get update by magic himself because you call #Scheduled.
But base on how you fetch the value in Configuration component they might be limitation and #RefreshScope will not work refreshscope
I suggest create a bean a #ConfigurationProperties and annotate with #RefreshScope when you hit from actuator endpoint /refresh everything should update or maybe combination of #Schedule you might achieved what you want
I am using Spring Cloud, I have Eureka, Zuul configured. When deploying an Api Rest in Spring boot and consuming the rest POST service through Zuul, I receive an HTTP 405 error message.
I am checking the log and the following message appears:
Request method 'GET' not supported
2019-12-11 12: 19: 04.679 WARN 5724 --- [nio-8089-exec-6] .m.m.a.ExceptionHandlerExceptionResolver: Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
It is strange, when I consume the service directly it works correctly, but since Zuul it seems that I was sending it by GET but in fact I always send it by POST.
The Api Rest has security configured with JWT, I don't know if there is any kind of extra configuration that I have to do
Thank you very much, I would appreciate if someone can help me.
application.yml
spring:
application:
name: kentaurus-zuul-server
server:
port: 8082
servlet:
contextPath: /api/v1
#Endpoints
#endpoints:
# restart:
# enabled: true
#shutdown:
# enabled: true
#health:
# sensitive: false
#Zuul routes active
zuul:
routes:
kentaurus-page-core:
service-id: kentaurus-page-core
path: /public/kentaurus-page/**
sensitiveHeaders: Cookie,Set-Cookie
url: http://localhost:8089
# stripPrefix : true
management:
endpoints:
kentaurus-page-core:
exposure:
include: "*"
exclude:
#Eureka Instance ID
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8081/eureka/
instance:
hostname: localhost
instanceId: ${spring.application.name}:${server.port}
#Ribbon Activation
ribbon:
eureka:
enabled: true
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.kentaurus.zuul.server</groupId>
<artifactId>kentaurus-zuul-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kentaurus-zuul-server</name>
<description>Eureka Service</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- Dependency to use a spring cloud config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Dependency to register the service within Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- Dependency to use a spring cloud config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Dependency to register the service within Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<!-- Dependency Management -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am trying to configure a configuration server for all the properties in our application using #EnableConfigServer in spring boot. Please see the code below :
#EnableConfigServer
#SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cdk.config</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configserver</name>
<description>Contains all the configurations/properties required by all the services</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.application.name=config-server
server.port=9090
spring.cloud.config.server.native.searchLocations=file://Users/Sankest/StarterProjects/MicroServices/AllConfigurations/
spring.profiles.active=native
Copied all the property files to : /Users/Sankest/StarterProjects/MicroServices/AllConfigurations/
But when I try to access url at http://localhost:9090/config-server/default I am not seeing any property files and getting the following response:
{"name":"config-server","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[]}
Add dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Correct value should be spring.cloud.config.server.native.searchLocations=file:///Users/Sankest/StarterProjects/MicroServices/AllConfigurations/ with 3 front slashes after file:. One way to verify whether path is correct or not, even without running the application, is to paste the path in browser and check whether it shows all the files.
For default profile make sure either file name is application.yml or application.properties.
For other profiles e.g. dev, file name should be application-dev.yml or application-dev.properties (if all are in the same folder), then http://localhost:9090/config-server/dev would show both dev and default profile entries.
I don't think so you are accessing the correct endpoint.
For example :
If you are having three files in your AllConfigurations folder.
The file can be yml or properties.
application-dev.yml
application-test.yml
application-prod.yml
The name in the left side of the - is application name and name in right side is profile.
So the endpoint for application-dev.yml will be
http://localhost:9090/application/dev
From the client to access the specific profile file in the config-server you need to set the active profile.
If you client application name is test
spring.profile.active=dev
Then it will return test-dev file to your client from config server.