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.
Related
I get this output will running me spring app
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driver-class-name
Value: com.mysql.cj.jdbc.Driver
Origin: "driverClassName" from property source "source"
Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader
Action:
Update your application's configuration
my pom.xml look like this:
<?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>
<groupId>com</groupId>
<artifactId>couponProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>couponProject-Spring </name>
<description>coupon project part 2 spring</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
and my application.properties looking like this:
spring.datasource.url=jdbc:mysql://localhost:3306/couponproject?serverTimezone=Israel&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# spring.jpa.properties.hibernate.format_sql=true
# spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
I'm new to spring so I don't really understand why I get this output
I used spring initializer spring JPA
I read similar questions but I couldn't quite understand the solution
Your project is not able to find the jar because maven dependency is missing for MYSQL Drivers . Please add below dependency for mySQL 8 in order to get the drivers available for your application . Add it inside the <dependencies> section of your pom.xml .
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
Or if you want to have another 8.X or 5.X version MySQL based dependency (based upon which version of MySQL it is that you are using) , please use the correct maven dependency from here : https://mvnrepository.com/artifact/mysql/mysql-connector-java .
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
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=*
I had deploy maven project built in war package to Jboss EAP 6.4 and successfully, WAR package deployed. I had created services and run with spring-boot in local eclipse and I can ran in browser but I have a problem when access that URL based on war package in Jboss eap. I success ran jboss service but cannot run the URL. for example, I have service with method GET with url: localhost:8080/letter-printing-eap-generator/testing cannot run in jboss but ran in local eclipse before deploy. how to fix this problem? any configuration xml in maven project? I just add jboss plugin. thanks. my code:
my pom.xml:
<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>
<groupId>com.prudential.letter.printing</groupId>
<artifactId>letter-printing-eap-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>LetterPrintingEapGenerator</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>1.2.2.RELEASE</spring-cloud.version>
<swagger.version>2.6.1</swagger.version>
<jboss.home>${env.JBOSS_HOME}</jboss.home>
<config.server>http://10.170.49.103/configserver</config.server>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- JBOSS maven plugin to simulate deployment to JBOSS -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.9.Final</version>
<configuration>
<jbossHome>${jboss.home}</jbossHome>
<serverArgs>
<serverArg>-Dspring.profiles.active=${run.profiles}</serverArg>
<serverArg>-Dspring.cloud.config.uri=${config.server}</serverArg>
</serverArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
SpringBootRestApiApp.java
#SpringBootApplication(scanBasePackages={"com.prudential.letter.printing"})
#Import({SpringDataRestConfiguration.class})
public class SpringBootRestApiApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestApiApp.class, args);
}
}
my controller:
#RestController
#RequestMapping(value="/")
public class TestingController {
#GetMapping("testing")
public String getTestingMethod(){
return "Hello Testing";
}
#GetMapping("data")
public Map<String, Object> getData(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", "200");
map.put("message", "ini messagenya");
map.put("content", "mantap");
return map;
}
}
this is my project structure:
application.yml :
server:
port: 8080
contextPath: /letter-printing-eap-generator
In the absence of any specific configuration, JBoss EAP will provide access to your web application at a context with a name that matches the WAR file name.
Therefore, your application should be accessible at:
http://localhost:8080/letter-printing-eap-generator-0.0.1-SNAPSHOT/testing
A common way of changing this for development purposes is to include a finalName element in the pom.xml file:
<build>
<finalName>${project.artifactId}</finalName>
...
</build>
This will generate a WAR file named letter-printing-eap-generator.war and the original URL that you tried should work.
Alternatively, you could add a jboss-web.xml file to your deployment (in the WEB-INF directory) that contains a context-root element:
<jboss-web>
<context-root>letter-printing-eap-generator</context-root>
</jboss-web>
You could also do this using the JBoss CLI or the web console to perform deployment as well.
I am developing a multi-module project in Spring Boot where the project structure is as follows:
com.app.parent <- parent pom with version numbers and common dependencies (POM)
com.app.core <- repository and service layer, models, DTOs (JAR)
com.app.rest <- rest API (WAR)
com.app.soap <- soap API (WAR)
The pom.xml file for the parent project is:
<artifactId>app-parent</artifactId>
<packaging>pom</packaging>
<name>app-parent</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The pom.xml file for the core project is:
<artifactId>app-core</artifactId>
<packaging>jar</packaging>
<name>app-core</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
The pom.xml for the rest project is:
<artifactId>app-rest</artifactId>
<packaging>war</packaging>
<name>app-rest</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>app-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The entry point for the app-core project is:
#SpringBootApplication
public class CoreApplication {
public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}
The entry point for the app-rest project looks exactly the same.
I have created an application.properties file within core/src/main/resources/ which contains database config etc. and I can compile/test the app-core project just fine. However, when I try to run or test the app-rest project I get errors related to the absence of application.properties file
Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
How do I get the child project to load the parent's application.properties file? Do I have to create a symlink to the parent file, or do I expressly load the parent's property file via #PropertySource? What do others do in this scenario?
Create another module for configurations (let's say config) and put your application.properties in config/src/main/resources/.
Add the config module as dependency to any module which use the same configs and there you go.