Feign retryable exception error in Docker - java

I have created 4 services Eureka service, Gateway service, Authentication service and User service. Authentication and User service are routing through gateway service on port 8000 and FeignClient is used to communicate between Auth and User service for creating user and authentication.
My services are running fine on local server but when docker images are created I'm getting a Feign retryable exception error.
docker-compose.yml file
version: "3.8"
services:
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=bank_app
ports:
- 3307:3306
volumes:
- db:/var/lib/mysql
networks:
- main-network
eureka-service:
build:
context: ../code/eureka-service
dockerfile: Dockerfile
image: eureka-service
container_name: eureka-service
restart: on-failure
ports:
- 8761:8761
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_APPLICATION_NAME=eureka-service
networks:
- main-network
gateway-service:
build:
context: ../code/gateway-service
dockerfile: Dockerfile
image: gateway-service
container_name: gateway-service
restart: on-failure
ports:
- 8000:8000
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_APPLICATION_NAME=gateway-service
- SPRING_APPLICATION_JSON={"eureka":{"client":{"serviceUrl":{"defaultZone":"http://eureka-service:8761/eureka"}}}}
networks:
- main-network
authentication-authorization-service:
build:
context: ../code/authentication-authorization-service
dockerfile: Dockerfile
image: authentication-authorization-service
container_name: authentication-authorization-service
restart: on-failure
ports:
- 3000:3000
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_APPLICATION_NAME=authentication-authorization-service
- SPRING_APPLICATION_JSON={"eureka":{"client":{"serviceUrl":{"defaultZone":"http://eureka-service:8761/eureka"}}}}
networks:
- main-network
user-service:
build:
context: ../code/user-service
dockerfile: Dockerfile
image: user-service
container_name: user-service
restart: on-failure
depends_on:
- mysqldb
ports:
- 3005:3005
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_APPLICATION_NAME=user-service
- SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb:3306/bank_app?useSSL=false
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=123456
- SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.MySQLDialect
- SPRING_JPA_PROPERTIES_DDL_AUTO=update
- SPRING_APPLICATION_JSON={"eureka":{"client":{"serviceUrl":{"defaultZone":"http://eureka-service:8761/eureka"}}}}
networks:
- main-network
networks:
main-network:
external:
name: $NETWORK_NAME
volumes:
db:
application.properties file for Eureka and Gateway service is as follows
application.properties file for Eureka-service
spring.application.name=eureka-service
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
application.properties file for Gateway-service
spring.application.name=gateway-service
server.port=8000
security.enable-csrf=false
spring.main.web-application-type=reactive
spring.main.allow-bean-definition-overriding=true
user-service.path=/api/v1/users/**
user-service.uri=http://localhost:3005
authentication-authorization-service.path=/api/v1/auth/**
authentication-authorization-service.uri=http://localhost:3000
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka
eureka.client.instance.preferIpAddress = true
The following stack trace is generated
2023-02-17 16:22:28 2023-02-17 10:52:28.540 INFO 1 --- [ main] c.m.g.GatewayServiceApplication : Started GatewayServiceApplication in 236.273 seconds (JVM running for 242.047)
2023-02-17 16:27:12 2023-02-17 10:57:12.251 ERROR 1 --- [or-http-epoll-3] a.w.r.e.AbstractErrorWebExceptionHandler : [5d73c5ea] 500 Server Error for HTTP POST "/api/v1/auth/signup"
2023-02-17 16:27:12
2023-02-17 16:27:12 io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:3000
2023-02-17 16:27:12 Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
2023-02-17 16:27:12 Error has been observed at the following site(s):
2023-02-17 16:27:12 *__checkpoint ⇢ org.springframework.web.cors.reactive.CorsWebFilter [DefaultWebFilterChain]
2023-02-17 16:27:12 *__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
2023-02-17 16:27:12 *__checkpoint ⇢ HTTP POST "/api/v1/auth/signup" [ExceptionHandlingWebHandler]

Related

HikariPool-1 - Exception during pool initialization. Trying to build Spring boot application using MySQL docker image

I am trying to create a application using Spring boot and MySQL.
The application is running successfully when working on local server, but when try to trying to create docker image the service is throwing Exception during pool initialization error.
Below are my project config
Spring boot version: 3.0.1
Java version: 17
MySQL version: 8
application properties
server.port=3005
spring.application.name=user-service
spring.datasource.url=jdbc:mysql://localhost:3307/bank_app?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
docker-compose.yml
version: "3.8"
services:
mysqldb:
image: mysql:latest
container_name: mysqldb
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=bank_app
- MYSQL_USER=root
- MYSQL_PASSWORD=123456
ports:
- 3307:3306
volumes:
- db:/var/lib/mysql
networks:
- main-network
user-service:
build:
context: ../code/user-service
dockerfile: Dockerfile
image: user-service
container_name: user-service
restart: on-failure
depends_on:
- mysqldb
ports:
- 3005:3005
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_APPLICATION_NAME=user-service
- SPRING_APPLICATION_JSON={"eureka":{"client":{"serviceUrl":{"defaultZone":"http://eureka-service:8761/eureka"}}}}
networks:
- main-network
networks:
main-network:
external:
name: main-network
volumes:
db:
pom.xml
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
<type>pom</type>
</dependency>
I tried by replacing the localhost with container name mysqldb in the datasource URL but still I'm facing same error
Issue
2023-02-14 22:44:07 2023-02-14T17:14:07.221Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 3005 (http)
2023-02-14 22:44:07 2023-02-14T17:14:07.292Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-02-14 22:44:07 2023-02-14T17:14:07.293Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-02-14 22:44:08 2023-02-14T17:14:08.255Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-02-14 22:44:08 2023-02-14T17:14:08.294Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 18593 ms
2023-02-14 22:44:10 2023-02-14T17:14:10.390Z INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-02-14 22:44:11 2023-02-14T17:14:11.085Z INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.1.6.Final
2023-02-14 22:44:13 2023-02-14T17:14:13.010Z WARN 1 --- [ main] org.hibernate.orm.deprecation : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2023-02-14 22:44:13 2023-02-14T17:14:13.810Z INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-02-14 22:44:15 2023-02-14T17:14:15.664Z ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
2023-02-14 22:44:15
2023-02-14 22:44:15 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2023-02-14 22:44:15
2023-02-14 22:44:15 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2023-02-14 22:44:15 at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2023-02-14 22:44:15 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2023-02-14 22:44:15 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2023-02-14 22:44:15 at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2023-02-14 22:44:15 at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2023-02-14 22:44:15 at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.31.jar!/:8.0.31]
2023-02-14 22:44:15 at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-5.0.1.jar!/:na]
2023-02-14 22:44:15 at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-6.1.6.Final.jar!/:6.1.6.Final]
2023-02-14 22:44:15 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:284) ~[hibernate-core-6.1.6.Final.jar!/:6.1.6.Final]
2023-02-14 22:44:15 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:177) ~[hibernate-core-6.1.6.Final.jar!/:6.1.6.Final]
Your port mapping for mysqldb is maybe not needed? That is unless you really want to expose MySQL's server to the outside world (outside:inside). In your case on port 3307.
ports:
- 3307:3306
I'm not sure if you really mean to expose your server, but inside of your container you should be using the real port, localhost:3306.
I solved this issue by adding the following lines in the docker-compose.yml files.
- SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb:3306/bank_app?useSSL=false
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=123456
- SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.MySQLDialect
- SPRING_JPA_PROPERTIES_DDL_AUTO=update
The new docker-compose.yml file looks like this
version: "3.8"
services:
mysqldb:
image: mysql:latest
container_name: mysqldb
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=bank_app
- MYSQL_USER=root
- MYSQL_PASSWORD=123456
ports:
- 3307:3306
volumes:
- db:/var/lib/mysql
networks:
- main-network
user-service:
build:
context: ../code/user-service
dockerfile: Dockerfile
image: user-service
container_name: user-service
restart: on-failure
depends_on:
- mysqldb
ports:
- 3005:3005
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_APPLICATION_NAME=user-service
- SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb:3306/bank_app?useSSL=false
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=123456
- SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.MySQLDialect
- SPRING_JPA_PROPERTIES_DDL_AUTO=update
- SPRING_APPLICATION_JSON={"eureka":{"client":{"serviceUrl":{"defaultZone":"http://eureka-service:8761/eureka"}}}}
networks:
- main-network
networks:
main-network:
external:
name: main-network
volumes:
db:

Unable to consume messages Kafka Spring Boot Docker Compose

My code is working fine while running separately using IDE. But when using docker-compose, the producer produce the messages correctly and I can consume the messages using docker CLI composer also but my Spring Boot microservice responsible for consuming the messages is not consuming.
There are no Error shows in the consumer container (name: process), it logs only the following...
request.timeout.ms = 30000
retry.backoff.ms = 100
sasl.client.callback.handler.class = null
sasl.jaas.config = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.min.time.before.relogin = 60000
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
sasl.kerberos.ticket.renew.window.factor = 0.8
sasl.login.callback.handler.class = null
sasl.login.class = null
sasl.login.refresh.buffer.seconds = 300
sasl.login.refresh.min.period.seconds = 60
sasl.login.refresh.window.factor = 0.8
sasl.login.refresh.window.jitter = 0.05
sasl.mechanism = GSSAPI
security.protocol = PLAINTEXT
security.providers = null
send.buffer.bytes = 131072
session.timeout.ms = 10000
ssl.cipher.suites = null
ssl.enabled.protocols = [TLSv1.2]
ssl.endpoint.identification.algorithm = https
ssl.engine.factory.class = null
ssl.key.password = null
ssl.keymanager.algorithm = SunX509
ssl.keystore.location = null
ssl.keystore.password = null
ssl.keystore.type = JKS
ssl.protocol = TLSv1.2
ssl.provider = null
ssl.secure.random.implementation = null
ssl.trustmanager.algorithm = PKIX
ssl.truststore.location = null
ssl.truststore.password = null
ssl.truststore.type = JKS
value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
2021-04-07 14:58:07.123 INFO 1 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka version: 2.6.0
2021-04-07 14:58:07.126 INFO 1 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka commitId: 62abe01bee039651
2021-04-07 14:58:07.127 INFO 1 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka startTimeMs: 1617807487120
2021-04-07 14:58:07.138 INFO 1 --- [ main] o.a.k.clients.consumer.KafkaConsumer : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Subscribed to topic(s): test
2021-04-07 14:58:07.146 INFO 1 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService
2021-04-07 14:58:07.241 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
2021-04-07 14:58:07.354 INFO 1 --- [ main] c.h.p.p.ProcessFcmDataServiceApplication : Started ProcessFcmDataServiceApplication in 9.916 seconds (JVM running for 11.719)
2021-04-07 14:58:08.036 WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2021-04-07 14:58:08.037 WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
2021-04-07 14:58:08.165 WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2021-04-07 14:58:08.165 WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
2021-04-07 14:58:08.316 WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2021-04-07 14:58:08.317 WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-testgroup-1, groupId=testgroup] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
Here is my docker-compose.yml
version: "3"
services:
register:
container_name: register
build: register-FCM-token-service/
networks:
- push-notification
ports:
- "8001:8001"
send:
container_name: send
build: send-FCM-notification-service/
networks:
- push-notification
depends_on:
- process
links:
- kafka:kafka
environment:
kafka.boot.server: kafka:9092
ports:
- "8083:8083"
process:
container_name: process
build: process-FCM-data-service/
networks:
- push-notification
depends_on:
- recieve
links:
- kafka:kafka
environment:
kafka.boot.server: kafka:9092
ports:
- "8082:8082"
recieve:
container_name: recieve
build: recieve-push-request-service/
depends_on:
- kafka
links:
- kafka:kafka
ports:
- "8080:8080"
environment:
kafka.boot.server: kafka:9092
networks:
- push-notification
zookeeper:
container_name: zookeeper
image: wurstmeister/zookeeper
restart: always
networks:
- push-notification
kafka:
container_name: kafka
image: wurstmeister/kafka
ports:
- "9092:9092"
networks:
- push-notification
restart: always
environment:
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
ALLOW_PLAINTEXT_LISTENER: "yes"
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ADVERTISED_PORT: 9092
KAFKA_AUTO_OFFSET_RESET: 'latest'
networks:
push-notification:
The "process" container (the consumer) is not working.
When trying using the container CLI, the kafka consumer works fine inside the kafka container, here is the output...
/ # kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test
{"source":"salesforce","userid":"1235aa","messageTitle":"test 1 title","messageBody":"test 1 body"}
Here is my Kafka Config Java file for the process microservice (the consumer)...
#Configuration
#EnableKafka
public class KafkaConfig {
#Value("${kafka.boot.server}")
private String kafkaServer;
#Value("${kafka.consumer.group.id}")
private String kafkaGroupId;
public ProducerFactory<String, PushMessageModel> getProducerFactory(){
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer);
configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); //use StringSerializer.class instead for simple string value
return new DefaultKafkaProducerFactory<>(configs);
}
#Bean
public KafkaTemplate<String, PushMessageModel> getKafkaTemplate(){
return new KafkaTemplate<>(getProducerFactory());
}
#Bean
public ConsumerFactory<String, FCMMessageContainer> getConsumerFactory(){
Map<String, Object> configs = new HashMap<>();
configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer);
configs.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaGroupId);
configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
return new DefaultKafkaConsumerFactory<>(configs, new StringDeserializer(), new JsonDeserializer<>(FCMMessageContainer.class));
}
#Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, FCMMessageContainer>> getKafkaListener(){
ConcurrentKafkaListenerContainerFactory<String, FCMMessageContainer> listener = new ConcurrentKafkaListenerContainerFactory<>();
listener.setConsumerFactory(getConsumerFactory());
listener.setErrorHandler(new KafkaErrHandler());
return listener;
}
}
How can I debug this issue? How to resolve this? There is no issue with my code clearly as it works on the IDE, then what is the problem?

I cannot produce message to my kafka topic from my java producer

Error Message: : [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
.yaml file below:
version: "3.7"
networks:
kafka-net:
name: kafka-net
driver: bridge
services:
zookeeper:
image: zookeeper:3.6.1
container_name: zookeeper
restart: always
networks:
- kafka-net
ports:
- "2181:2181"
volumes:
- c:/Users/snzey/IdeaProjects/kafka-producer:/bitnami/zookeeper
kafka:
image: wurstmeister/kafka:2.12-2.5.0
container_name: kafka
restart: always
networks:
- kafka-net
ports:
- "9092:9092"
volumes:
- c:/Users/snzey/IdeaProjects/kafka-producer:/bitnami/kafka
environment:
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: DOCKER_INTERNAL:PLAINTEXT,DOCKER_EXTERNAL:PLAINTEXT
KAFKA_LISTENERS: DOCKER_INTERNAL://:29092,DOCKER_EXTERNAL://:9092
KAFKA_ADVERTISED_LISTENERS: DOCKER_INTERNAL://kafka:29092,DOCKER_EXTERNAL://${DOCKER_HOST_IP:-127.0.0.1}:9092
KAFKA_INTER_BROKER_LISTENER_NAME: DOCKER_INTERNAL
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zookeeper

docker compose with services and mongo databases throws error opening socket

I've two services, these are application.yml's:
category-service -> application.yml
spring:
application:
name: category-service
data:
mongodb:
host: categoriesdb
port: 27017
database: categoriesdb
server:
port: 8081
productcatalog-service -> application.yml
spring:
application:
name: productcatalog-service
data:
mongodb:
host: productsdb
port: 27018
database: productsdb
server:
port: 8080
And docker-compose:
version: "3.8"
services:
productcatalog-service:
container_name: productcatalog-service
image: productcatalog-service
build:
./productcatalog-service
ports:
- 8080:8080
links:
- productsdb
category-service:
container_name: category-service
image: category-service
build:
./category-service
ports:
- 8081:8081
links:
- categoriesdb
categoriesdb:
container_name: categoriesdb
image: mongo:latest
volumes:
- ./data/categories-db:/data/db"
ports:
- 27017:27017
productsdb:
container_name: productsdb
image: mongo:latest
volumes:
- ./data/products-db:/data/db
ports:
- 27018:27017
And I've a problem with running, because I get after docker-compose up:
productcatalog-service | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.1.jar!/:na]
productcatalog-service | at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
productcatalog-service | Caused by: java.net.UnknownHostException: productsdb: Name or service not known
productcatalog-service | at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
productcatalog-service | at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932) ~[na:na]
productcatalog-service | at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517) ~[na:na]
productcatalog-service | at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851) ~[na:na]
productcatalog-service | at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507) ~[na:na]
productcatalog-service | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366) ~[na:na]
productcatalog-service | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300) ~[na:na]
productcatalog-service | at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-4.1.1.jar!/:na]
productcatalog-service | ... 6 common frames omitted
productcatalog-service |
category-service | 2020-12-05 23:41:05.335 INFO 1 --- [127.0.0.1:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 127.0.0.1:27017
category-service |
category-service | com.mongodb.MongoSocketOpenException: Exception opening socket
category-service | at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:124) ~[mongodb-driver-core-4.1.1.jar!/:na]
category-service | at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:129) ~[na:na]
category-service | at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:285) ~[na:na]
category-service | at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:200) ~[na:na]
category-service | at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:215) ~[na:na]
category-service | at java.base/sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:306) ~[na:na]
category-service | at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) ~[na:na]
category-service | at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
category-service | at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
category-service | at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
category-service | Caused by: java.net.ConnectException: Connection refused
category-service | at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method) ~[na:na]
category-service | at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:254) ~[na:na]
To sum up, I'd like to run these two services using one docker-compose, each service could have one mongodb assigned to it. If my thoughts about architecture are wrong, please correct me.
Thank you for help!
it seems productsdb needs change:
productsdb:
container_name: productsdb
image: mongo:latest
volumes:
- ./data/products-db:/data/db
ports:
- 27018:27018
if you use spring boot 2.4.0 try to downgrade version to 2.3.5

Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

I'm getting the below error while giving docker-compose up.
springbootapp | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
springbootapp | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.8.jar!/:42.2.8]
springbootapp | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.2.jar!/:na]
springbootapp | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.2.jar!/:na]
springbootapp | at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.Flyway.execute(Flyway.java:438) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.flywaydb.core.Flyway.migrate(Flyway.java:149) [flyway-core-6.0.8.jar!/:na]
springbootapp | at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:65) [spring-boot-autoconfigure-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8082
RUN mkdir -p /app/
RUN mkdir -p /app/logs/
ADD target/household-0.0.1-SNAPSHOT.jar /app/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=container", "-jar", "/app/app.jar"]
docker-compose.yml:
version: '3.2'
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5432
ports:
- 5434:5434
environment:
- POSTGRES_PASSWORD=
- POSTGRES_USER=
- POSTGRES_DB=test
restart: unless-stopped
# APP*****
springbootapp:
image: springbootapp:latest
network_mode: bridge
container_name: springbootapp
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
The error is definitely due to postgres is not running correctly or the app is not able to communicate to postgres. Use below docker-compose file and check.
version: '2'
services:
postgresql:
image: postgres:10.4
volumes:
- ./postgresql/:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=someuser
- POSTGRES_PASSWORD=
ports:
- 5432:5432
You can always use following command to check if db is accepting connection.
psql -h [HostIP] -U [db_user] -d [db_name]
I had same problem. the solution is simple.
first open docker app,
in Terminal process;
then you have to go to the directory where the docker-compose.yml
file is located.
then write docker-compose up
next, open pgAdmin app and choose db to your spring project,
next, enter the password and username found in docker-compose.yml
(username: image, password: POSTGRES_PASSWORD),
finally run the spring boot project.

Categories