I am currently working on a project base on microservices using Spring Boot and Docker. I have created a microservice which will be taking care of database access, for now it's main method is just simply displaying something just to check if it works. The basic concept is that other services may send requests regarding data to DataAccessService which accesses the database and sends reply back.
I was able to load dependencies, compile the project without warnings, dockerize it without issues. Running the app in Docker container shows no problems. However, when I'm trying to access the address:
localhost:8080/hello"
I get the ERR_CONNECTION_REFUSED
. Not really sure what could be the issue here... All kinds of help appreciated
Below are the dependencies from the pom file:
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.0-RC1</spring-cloud.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
The application properties file:
spring.cloud.config.server.bootstrap=true
spring.cloud.config.uri=http://configs:20101
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
The main class of the application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Configuration
@EnableAutoConfiguration
@SpringBootApplication
public class DataAccessServiceApplication {
@GetMapping("/hello")
public String index(final Model model) {
model.addAttribute("title", "Docker + Spring Boot");
model.addAttribute("msg", "Welcome to the docker container!");
return "index";
}
public static void main(String[] args) {
SpringApplication.run(DataAccessServiceApplication.class, args);
}
}
When running it in Docker container I get the output:
EDIT: Adding the Dockerfile info:
FROM openjdk:11-jdk
ADD /target/data-access-service-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD java -jar data-access-service-0.0.1-SNAPSHOT.jar --envname=prod