Make HTTP request from one container to another - java

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).

Related

Docker doesn't save permanently files, which create my java app

My java app (backend) create some files during work. And when i make rebuild after some changes, this file deletes and my app need to create it again. How to save this files permanently? I try to create volume but it doesn't work.
This is my docker-compose config:
version: '3'
services:
examledb:
container_name: examle-docker-db
image: postgres
volumes:
- examle-docker-db:/var/lib/postgresql/data
ports:
- "5555:5432"
expose:
- "5555"
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=examle
- PGDATA=/var/lib/postgresql/data/pgdata
networks:
- examle-docker-network
restart: unless-stopped
backend:
container_name: examle-docker-backend
build: ./backend
volumes:
- /var/lib/docker/volumes/example_prod_example-backend-volume/_data:/root/projects/example_PROD/backend
ports:
- "8080:8080"
- "8888:8888"
depends_on:
- examledb
networks:
- examle-docker-network
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://examle-docker-db:5432/examle
restart: unless-stopped
frontend:
container_name: examle-docker-frontend
build: ./frontend
restart: unless-stopped
command: serve -s dist/vu4y-frontend -l 4200
networks:
- examle-docker-network
nginx:
image: nginx:stable
container_name: examle-docker-nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
depends_on:
- frontend
- backend
networks:
- examle-docker-network
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
networks:
- examle-docker-network
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
volumes:
examle-docker-db: { }
networks:
examle-docker-network:
driver: bridge
Also I try to create volume like this:
volumes:
- example-backend-volume:/root/projects/example_PROD/backend
It also doesn't work.
My docker-compose.yml layout in /root/projects/Example
Any advice will be very helpful. All files creates inside backend folder in the same category with src and pom.xml.
I solved the problem. My problem was that I don't understand how docker's volume works. I thought that need to write root where my docker app is run, but need to write any path in right path (in my case /root/projects/example_PROD/backend) and saves files in app there.

IntelliJ and Docker: Create a remote debug configuration: custom command

I am trying to run my java application (a project at my uni) in debug mode in IntelliJ with a dockerfile.
I found this tutorial:
https://www.jetbrains.com/help/idea/debug-a-java-application-using-a-dockerfile.html#create-remote-debug-config
I tried to follow each step (#create-remote-debug-configuration), but at 4:
Select the Docker configuration that runs your app (MyPassApp) and
specify the command to use when running your app in the Custom Command
field. The remote debug configuration will use this custom command
instead of the one defined in the Dockerfile. This command should
contain the -agentlib option to let the debugger attach to the
process:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 -jar JavaPassFromConsole.jar
I don't know what any of those commands mean, so I dont know what to put in the custom command section.
This is my docker-compose.yml file:
version: '3'
volumes: mysql_data: {}
networks:
back:
services:
backend:
build:
context: '.'
dockerfile: 'docker_config/backend/Dockerfile'
depends_on:
- db
links:
- db
ports:
- 8080:8080
- 9990:9990
environment:
CONTACT_USERNAME: <XXX>
CONTACT_PASSWORD: <XXX>
networks:
- back db:
container_name: db
image: mysql:5.7
restart: always
volumes:
- mysql_data:/var/lib/mysql
command: --lower_case_table_names=1
environment:
MYSQL_DATABASE: <XXX>
MYSQL_ROOT_PASSWORD: root
networks:
- back
ports:
- 3306:3306
- 5005:5005 phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 4000:80
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- back
Any help is appreciated!!

Zuul Load Balancer

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.

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

Tracing with Jaeger doesn't work with docker-compose

I instrumented a simple Spring-Boot application with Jaeger, but when I run the application within a Docker container with docker-compose, I can't see any traces in the Jaeger frontend.
I'm creating the tracer configuration by reading the properties from environment variables that I set in the docker-compose file.
This is how I create the tracer:
Configuration config = Configuration.fromEnv();
return config.getTracer();
And this is my docker-compose file:
version: '2'
services:
demo:
build: opentracing_demo/.
ports:
- "8080:8080"
environment:
- JAEGER_SERVICE_NAME=hello_service
- JAEGER_AGENT_HOST=jaeger
- JAEGER_AGENT_PORT=6831
jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "5775:5775/udp"
- "6831:6831/udp"
- "6832:6832/udp"
- "5778:5778"
- "16686:16686"
- "14268:14268"
- "9411:9411"
You can also find my project on GitHub.
What am I doing wrong?
I found the solution to my problem, in case anybody is facing similar issues.
I was missing the environment variable JAEGER_SAMPLER_MANAGER_HOST_PORT, which is necessary if the (default) remote controlled sampler is used for tracing.
This is the working docker-compose file:
version: '2'
services:
demo:
build: opentracing_demo/.
ports:
- "8080:8080"
environment:
- JAEGER_SERVICE_NAME=hello_service
- JAEGER_AGENT_HOST=jaeger
- JAEGER_AGENT_PORT=6831
- JAEGER_SAMPLER_MANAGER_HOST_PORT=jaeger:5778
jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "5775:5775/udp"
- "6831:6831/udp"
- "6832:6832/udp"
- "5778:5778"
- "16686:16686"
- "14268:14268"
- "9411:9411"
For anyone finding themselves on this page looking at a simialr issue for jaeger opentracing in .net core below is a working docker compose snippet and working code in Startup.cs. The piece I was missing was getting jaeger to read the environment variables from docker-compose as by default it was trying to send the traces over udp on localhost.
version: '3.4'
services:
jaeger-agent:
container_name: 'tracing.jaeger.agent'
image: jaegertracing/all-in-one:latest
networks:
- jaeger-demo
ports:
- "5775:5775/udp"
- "6831:6831/udp"
- "6832:6832/udp"
- "5778:5778/tcp"
- "16686:16686"
- "14268:14268"
- "9411:9411"
environment:
- LOG_LEVEL=debug
labels:
NAME: "jaeger-agent"
orderService:
container_name: 'tracing.orders.api'
image: ${DOCKER_REGISTRY-}orderservice.api
build:
context: .
dockerfile: OrdersApi/Dockerfile
networks:
- jaeger-demo
ports:
- "16252:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- JAEGER_SERVICE_NAME=OrdersApi
- JAEGER_AGENT_HOST=jaeger-agent
- JAEGER_AGENT_PORT=6831
- JAEGER_SAMPLER_TYPE=const
- JAEGER_SAMPLER_PARAM=1
depends_on:
- jaeger-agent
customerService:
container_name: 'tracing.customers.api'
image: ${DOCKER_REGISTRY-}customerservice.api
build:
context: .
dockerfile: CustomerApi/Dockerfile
networks:
- jaeger-demo
ports:
- "17000:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- JAEGER_SERVICE_NAME=CustomersApi
- JAEGER_AGENT_HOST=jaeger-agent
- JAEGER_AGENT_PORT=6831
- JAEGER_SAMPLER_TYPE=const
- JAEGER_SAMPLER_PARAM=1
depends_on:
- jaeger-agent
networks:
jaeger-demo:
Add following nuget packages to your api's.
<PackageReference Include="OpenTracing.Contrib.NetCore" Version="0.6.2" />
<PackageReference Include="Jaeger" Version="0.4.2" />
Then inside your Startup.cs Configure method, you will need to configure jaeger and opentracing as below.
services.AddSingleton<ITracer>(serviceProvider =>
{
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
Jaeger.Configuration.SenderConfiguration.DefaultSenderResolver = new SenderResolver(loggerFactory).RegisterSenderFactory<ThriftSenderFactory>();
var config = Jaeger.Configuration.FromEnv(loggerFactory);
ITracer tracer = config.GetTracer();
GlobalTracer.Register(tracer);
return tracer;
});
services.AddOpenTracing();

Categories

Resources