I currently have two microservices:
- service - port 8080, this microservice tries to fetch config from the other microservice.
- config - port 8888, this microservice is supposed to provide config.
For some reason my service is unable to fetch configuration from config microservice.
My config microservice should work because I can curl localhost:8888/service/default on my machine I receive:
{"name":"service","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/shared/service.yml","source":{"server.port":8080,"spring.security.user.password":"admin"}},{"name":"classpath:/shared/service.yaml","source":{"server.port":8080,"spring.security.user.password":"admin"}}]}
Error received (full)
service | 2019-06-06 21:31:06.721 INFO 1 --- [main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://config:8888
service | 2019-06-06 21:31:06.894 INFO 1 --- [main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://config:8888. Will be trying the next url if available
service | 2019-06-06 21:31:06.904 ERROR 1 --- [main] o.s.boot.SpringApplication : Application run failed
Docker-compose.yaml
version: '3.7'
services:
config:
container_name: config
build: ./config
ports:
- 8888:8888
service:
container_name: service
build: ./service
ports:
- 8080:8080
depends_on:
- config
Service Dockerfile:
FROM openjdk:8-jdk-alpine
ADD target/service.jar /app.jar
CMD [ "java", "-Xmx200m", "-jar", "/app.jar" ]
EXPOSE 8080
Service bootstrap.yaml
spring:
application:
name: service
cloud:
config:
uri: http://config:8888
fail-fast: true
service.yaml (has service configuration)
server:
port: 8080
spring:
security:
user:
password: admin # doesnt set since no connection
Config Dockerfile
FROM openjdk:8-jdk-alpine
ADD target/config.jar /app.jar
CMD [ "java", "-Xmx200m", "-jar", "/app.jar" ]
EXPOSE 8888
Config application.yaml
spring:
application:
name: config
profiles:
active: composite
cloud:
config:
server:
composite:
- type: native
search-locations: classpath:/shared
server:
port: 8888
shared/service.yaml (has service configuration)
server:
port: 8080
spring:
security:
user:
password: admin # doesnt set since no connection
Any ideas?
I found some similar issues, although they only had issues with their URI, mine is set correctly.
Microservice can not reach to Config Server on Docker Compose
Docker - SpringConfig - Connection refused to ConfigServer
When one service depends on another you have to make sure that the latter is fully started before connecting to it.
In your case, most probably, config is started but not ready (context started) at the time service is run. As #Ganesh Karewad and #asolanki pointed out, a solution is to implement a reconnection logic. Another solution is to make sure config is initialized and accepting connections before you run service.
You can achieve that with a script that waits until the config app is up. In alternative you could configure the config container with a health check command and after you start it, wait until it is marked as healthy. Then you can run the service container.
Similar issue discussed here and here
Hope that helps.
Related
I have a problem about defining database in some services in my Spring Boot Microservice example.
When I run docker-compose.yml file through this command (docker-compose up -d), I have a datasource issue in user service, advertisement service and lastly report service.
All these services have their own database defined in their own properties file under configuration folder of config server.
Here is the database part of docker-compose.yml
database:
container_name: mysql-database
image: 'mysql:latest'
ports:
- "3366:3306"
restart: always
environment:
MYSQL_DATABASE: "springbootuser"
MYSQL_USER: "springmicroserviceuser"
MYSQL_PASSWORD: "111111"
MYSQL_ROOT_PASSWORD: "111111"
volumes:
- db-data:/var/lib/mysql
networks:
backend:
aliases:
- "database"
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
How can I fix the datasource issue in all services defined in docker-compose file
Here is my docker-compose.yml : Link
Here is the user service properties file : Link
Here is the advertisement service properties file : Link
Here is the report service properties file : Link
Edited 1st :
Here is my new docker-compose.yml : Link
Here is the problem with your datasource connection port localhost:3306.
user 3366 instead of 3306.
when you define internal port on same docker network you need to use database hostname instead of localhost.
if you use localhost or IP then you need to use docker to expose a port for database connections that route from the external network.
if needed update docker compose database section:
ports:
- "3366:3306"
expose:
- "3366"
I try to remote debug the application in attached mode with host: 192.168.99.100 and port 5005, but it tells me that it is unable to open the debugger port. The IP is 192.268.99.100 (the cluster is hosted locally via minikube).
Output of kubectl describe service catalogservice
Name: catalogservice
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=catalogservice
Type: NodePort
IP: 10.98.238.198
Port: web 31003/TCP
TargetPort: 8080/TCP
NodePort: web 31003/TCP
Endpoints: 172.17.0.6:8080
Port: debug 5005/TCP
TargetPort: 5005/TCP
NodePort: debug 32003/TCP
Endpoints: 172.17.0.6:5005
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
This is the pods service.yml:
apiVersion: v1
kind: Service
metadata:
name: catalogservice
spec:
type: NodePort
selector:
app: catalogservice
ports:
- name: web
protocol: TCP
port: 31003
nodePort: 31003
targetPort: 8080
- name: debug
protocol: TCP
port: 5005
nodePort: 32003
targetPort: 5005
And in here I expose the containers port
spec:
containers:
- name: catalogservice
image: elps/myimage
ports:
- containerPort: 8080
name: app
- containerPort: 5005
name: debug
The way I build the image:
FROM openjdk:11
VOLUME /tmp
EXPOSE 8082
ADD /target/catalogservice-0.0.1-SNAPSHOT.jar catalogservice-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n", "-jar", "catalogservice-0.0.1-SNAPSHOT.jar"]
When I execute nmap -p 5005 192.168.99.100 I receive
PORT STATE SERVICE
5005/tcp closed avt-profile-2
When I execute nmap -p 32003 192.168.99.100 I receive
PORT STATE SERVICE
32003/tcp closed unknown
When I execute nmap -p 31003 192.168.99.100 I receive
PORT STATE SERVICE
31003/tcp open unknown
When I execute kubectl get services I receive
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
catalogservice NodePort 10.108.195.102 <none> 31003:31003/TCP,5005:32003/TCP 14m
minikube service customerservice --url returns
http://192.168.99.100:32004
As an alternative to using a NodePort in a Service you could also use kubectl port-forward to access the debug port in your Pod.
kubectl port-forward allows using resource name, such as a pod name, to select a matching pod to port forward to since Kubernetes v1.10.
You need to expose the debug port in the Deployment yaml for the Pod
spec:
containers:
...
ports:
...
- containerPort: 5005
Then get the name of your Pod via
kubectl get pods
and then add a port-forwarding to that Pod
kubectl port-forward podname 5005:5005
In IntelliJ you will be able to connect to
Host: localhost
Port: 5005
Alternatively, you can use the Cloud Code Intellij plugin.
Also, if you use Fabric8, it provides the fabric8:debug goal.
There was a slip in the yaml you first posted as:
- containerPort: 5050
name: debug
Should be:
- containerPort: 5005
name: debug
You also need to use the external port of 32003 when configuring the IntelliJ debugger. With those changes it should work.
You may also want to think about how to make it more flexible. In the past when I've done this I've used a different form for the docker start command that allows you to turn remote debug on and off by an environment variable called REMOTE_DEBUG, which for you would be:
CMD if [ "x$REMOTE_DEBUG" = "xfalse" ] ; then java $JAVA_OPTS -jar catalogservice-0.0.1-SNAPSHOT.jar ; else java $JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n -jar catalogservice-0.0.1-SNAPSHOT.jar ; fi
You'll probably find you want to set the env var $JAVA_OPTS to limit jvm memory use to avoid issues in k8s.
I have a gRPC server written in Java and I'm currently trying to create a web client, with React. However, I can't seem to manage the connection between the envoy proxy to which the client is connecting and the actual server.
I would expect to receive the same message as with the Java client, but I get the error "Http response at 400 or 500 level", receiving an empty response with the web client, while the Java server doesn't even get the request.
The server runs on port 8080, and the envoy proxy is configured on port 9090, which is the one used by the web client.
Dockerfile:
FROM envoyproxy/envoy-dev:latest
COPY ./envoy.yaml /etc/envoy/envoy.yaml
CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml -l trace --log-path /tmp/envoy_info.log
envoy.yaml:
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 0.0.0.0, port_value: 9901 }
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 9090 }
filter_chains:
- filters:
- name: envoy.http_connection_manager
config:
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: m_service
cors:
allow_origin:
- "*"
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
expose_headers: grpc-status,grpc-message
enabled: true
http_filters:
- name: envoy.grpc_web
- name: envoy.cors
- name: envoy.router
clusters:
- name: m_service
connect_timeout: 0.25s
type: logical_dns
http2_protocol_options: {}
lb_policy: round_robin
hosts:
socket_address:
address: localhost
port_value: 8080
The commands I use for building and running the docker container are docker build -t m-server ., and docker run -p 9090:9090 -td m-server /bin/bash and the proto classes for the front-end are loaded statically.
If there's any more code that'd be useful to post, please let me know. Any advice is appreciated, thank you!
For me the solution was to change the command passed to run the container, thus docker run -p 9090:9090 -td m-server /bin/bash becoming docker run -d -p 9090:9090 -p 9901:9901 m-server. The main difference was putting -d instead of -td and the second port mapping is for the envoy server.
I am just learning Docker and from what I understood from the documentation, the explanation would be that I was running the container in detached mode, but with a pseudo-tty allocated, which is used in foreground mode. I've seen it here but the purpose was slightly different and at the time I misunderstood it as only keeping the container running was not what I needed.
Changing 'localhost' to '0.0.0.0', as suggested in this answer is also important.
Looks like Envoy is not forwarding the request to your Java server. Envoy has an admin interface https://www.envoyproxy.io/docs/envoy/latest/operations/admin . That and the Envoy log files should help troubleshoot this.
socket_address:
address: localhost
This is the problem. Your envoy tries to forward to itself if it's running as dockerized image, because localhost is not your docker host machine for running container (where grpc server is running) , but actually localhost of running container. Use docker compose, port mapping or external network. Good luck
I have 3 microservices, and I run them with docker.
Dockerfile of each of them.
Frontend:
FROM node:alpine
LABEL maintainer="2262288#gmail.com"
WORKDIR /usr/app/front
EXPOSE 3000
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
Backend 1 (back):
FROM openjdk:8-jdk-alpine
LABEL maintainer="2262288#gmail.com"
VOLUME /tmp
EXPOSE 8099
ARG JAR_FILE=build/libs/auth-0.0.3.jar
ADD ${JAR_FILE} digital.jar
ENTRYPOINT ["java","-jar","/digital.jar"]
Backend 2 (message):
FROM openjdk:8-jdk-alpine
LABEL maintainer="2262288#gmail.com"
VOLUME /tmp
EXPOSE 8082
ARG JAR_FILE=build/libs/sender-0.0.1.jar
ADD ${JAR_FILE} sender.jar
ENTRYPOINT ["java","-jar","/sender.jar"]
Frontend send REST-request to backend1, than, backend1 send REST-request to backend2 (message).
I published it on hub & run on external server in docker-compose:
version: '3.7'
services:
web:
image: account/front:0.0.1
restart: on-failure
ports:
- 80:3000
back:
image: account/back:0.0.3
restart: on-failure
ports:
- 8099:8099
message:
image: account/message:0.0.1
restart: on-failure
ports:
- 8082:8082
Backend services run on ports:
message_1_e8eb3b2d2477 | 2019-09-24 09:34:00.882 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
back_1_1982cc6e57f7 | 2019-09-24 09:34:07.403 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8099 (http) with context path ''
As we see, each service run on its own port.
Than, I try to send request to front -> back -> message. back send request to message & reseive answer:
java.net.ConnectException: Operation timed out (Connection timed out)
Than, requests to message service not reach it.
When I send request directly with Postman, it works.
What's wrong?
UPD.
request from front to back:
http://81.100.122.90:8099/auth/register
body:
{"username":"ksgcf","password":"123","firstName":"John","lastName":"Doe","email":"398456234785#gmail.com"}
request from back to message (IP changed):
String url = "http://81.100.122.90:8082/email";
EmailMessageDto request = new EmailMessageDto(
dto.getEmail(),
"slava_rossii#list.ru",
"Email confirmation",
"Press link: http://dig.lamb.ru/confirm?username="
+ registrationToken.getUsername() + "&token=" + registrationToken.getToken()
);
So, I see this message when docker-compose run for the first time:
Creating network "project_default" with the default driver
First, when you use docker-compose all services are available via there names. So you can access message from back like this
$ docker-compose exec back ping message
PING message (172.24.0.3) 56(84) bytes of data.
64 bytes from message (172.24.0.3): icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from message (172.24.0.3): icmp_seq=2 ttl=64 time=0.068 ms
Second, check port bindings. You have to bind 0.0.0.0 (not localhost which is default for most of the services and frameworks) to access to the service from other containers via network. It's same you get ordinary virtual machines.
You can check port availability with telnet
As example I'm checking is postresql available on 5432 from container called superset
$ docker-compose exec superset telnet postgres 5432
Trying 172.24.0.3...
Connected to postgres.
Escape character is '^]'.
I try to starting my application(Spring Boot + Spring Cloud + Eureka + MongoDB) with using docker image, but i can't get connection to MongoDB.
Exception:
exception "com.mongodb.MongoSocketOpenException: Exception opening socket."
I starting my application with execute command: docker-compose up --build
Docker log:
com.mongodb. MongoSocket0penException: Exception opening socket at com.mongodb. connection. SocketStream.open(SocketStream.java: 63) ~|mongo-java-driver-3.2.2.jar!/:naj
at com. monsoob: connection. berettaberverwontortser/ersonito-kunnablearancerainitservertonitolnava:1203-dionso-java-driver*3?2.2.jarl/:nal
at : java. lang. Thread.run (Thread. java: 745) Lna:1.8.0_111]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect (Native Method) ~ [na: 1.8.0_1111 at java.net.AbstractPlainSocketImp1.doConnect (AbstractplainSocketImpl.java:350) ~ [na: 1.8.0_111]
at java.net.AbstractPlainSocketImp1.connectToAddress(AbstractPlainSocketImp1.java:206)~[na:1.8.0_1111
at java.net .AbstractPlainSocketImp1.connect (AbstractPlain5ocket Imp],java:188) ~[na:1.8.0_111]
at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392) ~|na:1.8.0_111] java.net Socket. connect (Socket, java: 589) ~[na:1.8:0-111]
at com.mongodb.connection. SocketStreamHelper.initialize(SocketStreamHelper. java: 50) ~ [mongo-java-driver-3.2.2.ar!/:na] at com. mongodb. connection. SocketStream.open(SocketStream. java:58) ~ [mongo-java-driver-3.2.2.jar!/:na]
3 common frames omitted
application.yml:
# Spring properties
spring:
application:
name: car-service
data:
mongodb.host: localhost
mongodb.port: 32769
mongodb.uri: mongodb://localhost/test
mongo.repositories.enabled: true
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# HTTP Server (Tomcat) Port
server:
port: 2220
error:
whitelabel:
enabled: false
docker-compose.yml:
eureka:
build: ./eureka-discovery-service
ports:
- "8761:8761"
mongodb:
image: mongo:3.0.4
ports:
- "32769:32769"
postgresql:
image: postgres:9.6.1
ports:
- "32770:32770"
gateway-service:
build: ./gateway-service
ports:
- "9090:9090"
links:
- eureka
environment:
SPRING_APPLICATION_NAME: gateway-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
airplane-service:
build: ./airplane-service
ports:
- "2222:2222"
links:
- eureka
- postgresql
environment:
SPRING_APPLICATION_NAME: airplane-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
SPRING_SLEUTH_ENABLED: "true"
SPRING_DATASOURCE_POSTGRESQL_URL: jdbc:postgresql://localhost:32770/postgres
car-service:
build: ./car-service
ports:
- "2220:2220"
links:
- eureka
- mongodb
environment:
SPRING_APPLICATION_NAME: car-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
SPRING_SLEUTH_ENABLED: "true"
SPRING_DATA_MONGODB_URI: mongodb://localhost:32769/test
machine-service:
build: ./machine-service
ports:
- "2224:2224"
links:
- eureka
environment:
SPRING_APPLICATION_NAME: machine-service
SPRING_PROFILES_ACTIVE: enableEureka
EUREKA_INSTANCE_PREFER_IP_ADDRESS: "true"
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka:8761/eureka/
SPRING_SLEUTH_ENABLED: "true"
Why I have exception opening a socket? How to fix this problem?
You are setting mongodb host in property file as localhost. In a container localhost address itself, but your mongodb is not in that container(car-service) which car-service run in. While you are using docker compose, you can address a container with its name. In your case it is mongodb.
to clarify #barbakini's answer, to define it in applications.yaml use:
spring.data.mongodb.host: mongodb
Your mongoDB service is not UP ,
Check the status by following command
sudo service mongodb status
sudo service mongodb start
Hope it should work, there could be several reasons as well ,
like the configuration you defined in your application for mongodb service is incorrect such as port .
Try to add
network_mode: host
and remove
links: ....
for all services, that want to connect with mongo or eureka or postgresql, inside your docker-compose.yml.
By doing so you will connect to docker localhost.