Zuul Load Balancer - java

So I have little problem here. I have 4 module:
Eureka Server
Zuul Gateway
Authentication Service
Another MicroService
When I starting it on a local computer it doesn't show any problem like
Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client: authentication-service
But when I started building it on docker it's always getting error like that. Can you tell me the part that I'm wrong?
eureka server properties
spring.application.name=bms-server
# default port for eureka server
server.port=8761
eureka.instance.hostname=bms-server
# eureka by default will register itself as a client. So, we need to set it to false.
# What's a client server? See other microservices (student, auth, etc).
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Zuul Server properties
server.port=8762
spring.application.name=bms-api-gateway
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://bms-server:8761/eureka/
zuul.ignored-services=*
zuul.routes.authentication-service.path=/auth/**
zuul.routes.authentication-service.service-id=authentication-service
zuul.routes.general-setup-service.path=/general-setup/**
zuul.routes.general-setup-service.service-id=general-setup-service
zuul.routes.authentication-service.strip-prefix=false
zuul.routes.authentication-service.sensitive-headers=Cookie,Set-Cookie
zuul.retryable=true
zuul.ignored-headers=Access-Control-Allow-Credentials, Access-Control-Allow-Origin
ribbon.eureka.enabled=false
ribbon.ConnectTimeout=60000
ribbon.ReadTimeout=60000
hystrix.command.default.execution.enabled=false
hystrix.command.default.execution.isolation.strategy=THREAD
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=240000
Authentication service properties
spring.application.name=authentication-service
server.port=9100
eureka.client.service-url.defaultZone=http://bms-server:8761/eureka/
authentication-service.ribbon.listOfServers=http://localhost:9100
spring.cloud.loadbalancer.ribbon.enabled=false
this is my Another service properties
spring.application.name=authentication-service
server.port=9100
eureka.client.service-url.defaultZone=http://bms-server:8761/eureka/
general-setup-service.ribbon.listOfServers=http://localhost:9100
spring.cloud.loadbalancer.ribbon.enabled=false
And last this is my docker-compose.yml
version: '3.5'
services:
bms-server:
image: bms-server:v1
container_name: bms-server
hostname: bms-server
build:
context: ./bms-server
dockerfile: Dockerfile
volumes:
- maven-home:/root/.m2
ports:
- "8761:8761"
networks:
- bms-network
bms-api-gateway:
image: bms-api-gateway:v1
container_name: bms-api-gateway
build:
context: ./bms-api-gateway
dockerfile: Dockerfile
ports:
- "8762:8762"
depends_on:
- bms-server
volumes:
- maven-home:/root/.m2
links:
- bms-server:bms-server
hostname: bms-api-gateway
networks:
- bms-network
bms-authentication-service:
image: bms-authentication-service:v1
container_name: bms-authentication-service
build:
context: ./bms-authentication-service
dockerfile: Dockerfile
ports:
- "9100:9100"
volumes:
- maven-home:/root/.m2
depends_on:
- bms-server
links:
- bms-server:bms-server
hostname: authentication-service
networks:
- bms-network
bms-general-setup-service:
image: bms-general-setup-service:v1
container_name: bms-general-setup-service
build:
context: ./bms-general-setup-service
dockerfile: Dockerfile
ports:
- "9102:9102"
depends_on:
- bms-server
links:
- bms-server:bms-server
volumes:
- maven-home:/root/.m2
hostname: general-setup-service
networks:
- bms-network
volumes:
maven-home:
networks:
bms-network:
name: bms-network
driver: bridge
Please tell me the part that I'm wrong. Thank you very much.

Related

Spring boot and logstash tcp link dosent work with docker compose "localhost/<unresolved>:5000"

It works for me in local but i had an error when using docker :
error is : localhost/<unresolved>:5000: connection,
how can i set this unresolved value for logstash destination id
docker-compose
version: '3.2'
services:
elasticsearch:
image: elasticsearch:$ELK_VERSION
volumes:
- elasticsearch:/usr/share/elasticsearch/data
environment:
ES_JAVA_OPTS: "-Xmx256m -Xms256m"
# Note: currently there doesn't seem to be a way to change the default user for Elasticsearch
ELASTIC_PASSWORD: $ELASTIC_PASSWORD
# Use single node discovery in order to disable production mode and avoid bootstrap checks
# see https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
discovery.type: single-node
# X-Pack security needs to be enabled for Elasticsearch to actually authenticate requests
xpack.security.enabled: "true"
ports:
- "9200:9200"
- "9300:9300"
healthcheck:
test: "wget -q -O - http://$ELASTIC_USER:$ELASTIC_PASSWORD#localhost:9200/_cat/health"
interval: 1s
timeout: 30s
retries: 300
networks:
- internal
restart: unless-stopped
# https://www.elastic.co/guide/en/logstash/current/docker-config.html
logstash:
image: logstash:$ELK_VERSION
ports:
- "5000:5000"
- "9600:9600"
environment:
LS_JAVA_OPTS: "-Xmx256m -Xms256m"
ELASTIC_USER: $ELASTIC_USER
ELASTIC_PASSWORD: $ELASTIC_PASSWORD
XPACK_MONITORING_ELASTICSEARCH_USERNAME: $ELASTIC_USER
XPACK_MONITORING_ELASTICSEARCH_PASSWORD: $ELASTIC_PASSWORD
XPACK_MONITORING_ELASTICSEARCH_HOSTS: "elasticsearch:9200"
XPACK_MONITORING_ENABLED: "true"
volumes:
- ./logstash/pipeline:/usr/share/logstash/pipeline:ro
networks:
- internal
restart: unless-stopped
depends_on:
- elasticsearch
# https://www.elastic.co/guide/en/kibana/current/docker.html
kibana:
image: kibana:${ELK_VERSION}
environment:
ELASTICSEARCH_USERNAME: $ELASTIC_USER
ELASTICSEARCH_PASSWORD: $ELASTIC_PASSWORD
# Because Elasticsearch is running in a containerized environment
# (setting this to false will result in CPU stats not being correct in the Monitoring UI):
XPACK_MONITORING_UI_CONTAINER_ELASTICSEARCH_ENABLED: "true"
ports:
- "5601:5601"
networks:
- internal
restart: unless-stopped
depends_on:
- elasticsearch
- logstash
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- db:/var/lib/mysql
app:
depends_on:
- mysqldb
build: ./../
restart: on-failure
env_file: ./.env
ports:
- $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update",
"spring.application.name" : "ebnelhaythem"
}'
volumes:
- .m2:/root/.m2
networks:
internal:
volumes:
elasticsearch:
db:
AND LOGS ARE :
elastic_log_docker-app-1 | 14:15:45,959 |-WARN in net.logstash.logback.appender.LogstashTcpSocketAppender[logstash] - Log destination localhost/<unresolved>:5000: connection
failed. java.net.ConnectException: Connection refused
elastic_log_docker-app-1 | at java.net.ConnectException: Connection refused
elastic_log_docker-app-1 | at at java.base/sun.nio.ch.Net.pollConnect(Native Method)
elastic_log_docker-app-1 | at at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
Your Spring Boot application expect logstash to be available at localhost:
logstash:
host: localhost
However, when you run everything inside docker-compose, logstash will not be present at localhost since this now refers to the app container.
To resolve this, override the logstash host property when you run the application with docker-compose with the value logstash (so the name of the docker-compose service). Similar as you do for the MySQL url.

Make HTTP request from one container to another

I am trying to do a httpRequest from library to people-service, but not that i've tried works.
i saw that if use the name it shoud ork, so i have tried http://library:8080, but it returns the error
java.lang.IllegalArgumentException: unsupported URI
Here is my Dockerfile:
FROM openjdk:11-jdk-slim
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
and my docker-compose:
version: '3.5'
services:
library:
build:
context: ./
ports:
- "8081:8081"
networks:
- library-network
restart: on-failure
mysql-service:
image: mysql:5.7
ports:
- "3306:3306"
networks:
- library-network
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_USER=admin
- MYSQL_PASSWORD=admin
- MYSQL_DATABASE=bootdb
restart: on-failure
people-service:
build:
context: ./
args:
JAR_FILE: ./target/library.jar
ports:
- "8080:8080"
environment:
- DB_HOST=jdbc:mysql://mysql-service:3306/library
networks:
- library-network
depends_on:
- mysql-service
- library
restart: on-failure
networks:
library-network:
driver: bridge
Your question says:
I am trying to do a httpRequest from library to people-service
From that I assume the request would be initiated in library. Then the URL should look like http://people-service:8080.
But from the error message I'd assume your library is not able to run http requests - regardless of where they are going to, which is a bit strange. Investigate on that first, using simple urls like http://stackoverflow.com (which will return http status 301).

Keycloak docker container throws `java.net.UnknownHostException` during startup

I'm trying to setup a keycloak container on my docker host. Unfortunately the keycloak container can't connect to my db container and always throws a java.net.UnknownHostException.
My docker-compose file:
version: '2'
services:
keycloak:
image: quay.io/keycloak/keycloak:latest
container_name: keycloak
restart: always
networks:
- webgateway
- keycloak-net
environment:
- DB_VENDOR=MYSQL
- DB_ADDR=keycloak-db
- DB_DATABASE=keycloak
- DB_USER=keycloak
- DB_PASSWORD=password
- KEYCLOAK_USER=admin
- KEYCLOAK_PASSWORD=password
depends_on:
- keycloak-db
keycloak-db:
image: mysql:5.7
container_name: keycloak-db
restart: always
volumes:
- /var/keycloak/database:/var/lib/mysql
networks:
- keycloak-net
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=keycloak
- MYSQL_USER=keycloak
- MYSQL_PASSWORD=password
networks:
keycloak-net:
webgateway:
external:
name: docker_webgateway
The error message:
Caused by: java.net.UnknownHostException: keycloak-db: Name or service not known,
at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method),
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929),
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515),
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848),
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505),
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364),
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298),
at com.mysql.jdbc#8.0.19//com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132),
at com.mysql.jdbc#8.0.19//com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65),
... 64 more,
,
23:44:04,490 FATAL [org.keycloak.services] (ServerService Thread Pool -- 72) java.lang.RuntimeException: Failed to connect to database,
Does someone know what I'm doing wrong?
P.S. The docker host is running locally so don't worry about my weak passwords.
Flag depends_on only ensures the order in which container is started but not whether the container is ready to serve the requests or not and that's what is happening here, where SQL container is started but it's not ready to receive the requests as it takes time. Please follow the below commands and the file, in which I added the health checks as well in docker-compose and you need to change the version to 2.1 or higher.
Create an external docker network
docker network create docker_webgateway
docker-compose.yaml
container_name: keycloak
restart: always
networks:
- webgateway
- keycloak-net
environment:
- DB_VENDOR=MYSQL
- DB_ADDR=keycloak-db
- DB_DATABASE=keycloak
- DB_USER=keycloak
- DB_PASSWORD=password
- KEYCLOAK_USER=admin
- KEYCLOAK_PASSWORD=password
depends_on:
- keycloak-db
keycloak-db:
image: mysql:5.7
container_name: keycloak-db
restart: always
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
volumes:
- /var/keycloak/database:/var/lib/mysql
networks:
- keycloak-net
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=keycloak
- MYSQL_USER=keycloak
- MYSQL_PASSWORD=password
networks:
keycloak-net:
webgateway:
external:
name: docker_webgateway
Create the stack by running the below command
docker-compose up

API-gateway never getting config from config server when both are run with docker compose

I have three services.
Config server
Eureka server
api-gateway
If I run them individually it's working fine. Then I am trying to introduce docker on above services. So I have prepare 3 dockerfile for each services:
VOLUME /tmp
ADD config-server/build/libs/config-server-0.0.1-SNAPSHOT.jar config-server-0.0.1-SNAPSHOT.jar
CMD ["java", "-jar", "config-server-0.0.1-SNAPSHOT.jar"]
VOLUME /var/lib/config-repo
EXPOSE 10270
FROM java:8
VOLUME /tmp
ADD eureka-server/build/libs/eureka-server-1.0-SNAPSHOT.jar eureka-server-1.0-SNAPSHOT.jar
CMD ["java","-jar","eureka-server-1.0-SNAPSHOT.jar"]
EXPOSE 10210
FROM java:8
VOLUME /tmp
ADD api-gateway/build/libs/api-gateway-0.0.1-SNAPSHOT.jar api-gateway-0.0.1-SNAPSHOT.jar
RUN bash -c 'touch /api-gateway-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/api-gateway-0.0.1-SNAPSHOT.jar"]
And then I have prepared my docker-compose file:
version: '3'
services:
eureka-server:
restart: always
container_name: eureka-server
build:
context: .
dockerfile: eureka-server/Dockerfile_eureka_server
expose:
- 10210
ports:
- 10210:10210
networks:
- servicenet
config-server:
restart: always
container_name: config-server
build:
context: .
dockerfile: config-server/Dockerfile_config_server
expose:
- 10270
ports:
- 10270:10270
healthcheck:
test: ["CMD", "curl", "-f", "http://config-server:10270"]
interval: 30s
timeout: 10s
retries: 5
networks:
- servicenet
api-gateway:
restart: on-failure
container_name: api-gateway
build:
context: .
dockerfile: api-gateway/Dockerfile_api_gateway
expose:
- 10200
ports:
- 10200:10200
networks:
- servicenet
links:
- config-server
- eureka-server
networks:
servicenet:
driver: bridge
But api-gateway start before config-server completely start its service. Thats why api gateway start on 8080 port and looking for eureka server at host localhost and port 8621. though its not getting this ports and hosts in docker its continuing looking for eureka server but not again fetching config from config server. Is there anything wrong with my configuration?
My application.properties file on github like this
server.port=10200
#Eureka configuration
eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
lombok.equalsAndHashCode.callSuper = call
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.client.registryFetchIntervalSeconds=5
eureka.client.serviceUrl.defaultZone=http://eureka-server:10210/eureka
spring.cloud.service-registry.auto-registration.enabled=true
eureka.client.enabled=true
eureka.client.serviceUrl.registerWithEureka=true
lombok.anyConstructor.suppressConstructorProperties = true
#Zuul Configuration
# A prefix that can added to beginning of all requests.
#zuul.prefix=/api
# Disable accessing services using service name (i.e. gallery-service).
# They should be only accessed through the path defined below.
zuul.ignored-services=*
# Map paths to services
zuul.routes.gallery-service.path=/gallery/**
zuul.routes.gallery-service.service-id=gallery-manager
zuul.routes.image-service.path=/image/**
zuul.routes.image-service.service-id=image-service
zuul.routes.book-manager.path=/book-manager/**
zuul.routes.book-manager.service-id=book-manager
zuul.routes.auth-service.path=/auth/**
zuul.routes.auth-service.service-id=auth-manager
zuul.routes.remote-library.path=/remote/**
zuul.routes.remote-library.service-id=remote-library
#zuul.routes.auth-service.strip-prefix=false
# Exclude authorization from sensitive headers
zuul.routes.auth-service.sensitive-headers=Cookie,Set-Cookie
NB: If I try with other services rather than api-gateway its working fine. I am using zuul proxy for api gateway service.
A quick fix would be to add a 'depends_on' clause so that the api-service depends on the config server - that way the api-service won't start until the config server is up.
To make this workable we have to wait still other services are up. So I have written a script for waiting and rewrite docker compose like this
version: '3'
services:
eureka-server:
restart: always
container_name: eureka-server
build:
context: .
dockerfile: eureka-server/Dockerfile_eureka_server
expose:
- 10210
ports:
- 10210:10210
networks:
- servicenet
config-server:
restart: always
container_name: config-server
build:
context: .
dockerfile: config-server/Dockerfile_config_server
expose:
- 10270
ports:
- 10270:10270
healthcheck:
test: ["CMD", "curl", "-f", "http://config-server:10270"]
interval: 30s
timeout: 10s
retries: 5
networks:
- servicenet
api-gateway:
restart: always
container_name: api-gateway
build:
context: .
dockerfile: api-gateway/Dockerfile_api_gateway
expose:
- 10200
ports:
- 10200:10200
networks:
- servicenet
links:
- config-server
depends_on:
- config-server
command: ["./wait-for-it.sh", "config-server:10270","--","./wait-for-it.sh", "eureka-server:10210", "--", "java","-jar", "api-gateway-0.0.1-SNAPSHOT.jar"]
And also rewrite docker files like this(for api-gateway)
FROM java:8
VOLUME /tmp
ADD api-gateway/build/libs/api-gateway-0.0.1-SNAPSHOT.jar api-gateway-0.0.1-SNAPSHOT.jar
ADD wait-for-it.sh wait-for-it.sh
RUN chmod +x wait-for-it.sh

Java service in docker container not connecting to host mysql

I have Java microservices running in docker container which is not able to connect to mysql hosted locally.
docker is running in network having ip address as 172.0...
If I execute Java service directly as java -jar, it is able to connect to mysql running in 10.0..
docker-compose file
version: '2.0'
services:
config-server:
image: test/config-server
container_name: config-server
environment:
- GIT_USERNAME=${GIT_USERNAME}
- GIT_PASSWORD=${GIT_PASSWORD}
ports:
- 8889:8889
entrypoint: ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Drun.arguments=GIT_USERNAME=${GIT_USERNAME}, GIT_PASSWORD=${GIT_PASSWORD} -Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
discovery-server:
image: test/discovery-server
container_name: discovery-server
links:
- config-server
depends_on:
- config-server
entrypoint: ["./wait-for-it.sh","config-server:8889","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8761:8761
web-authentication:
image: test/web-authentication
container_name: web-authentication
links:
- config-server
- discovery-server
depends_on:
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 8444:8444
gateway:
image: test/gateway
container_name: gateway
links:
- config-server
- discovery-server
- web-authentication
depends_on:
- discovery-server
entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ports:
- 81:8765
The issue was resolved after configuring networks configuration in docker-compose.yml, the issue was mysql and Docker containers were running in different subnet.

Categories

Resources