[英]Docker / docker-compose / nginx / springboot
I have two spring-boot microservices, order-service & payment-service.我有两个 spring-boot 微服务,订单服务和支付服务。 Both of them are connected to Eureka server.
它们都连接到 Eureka 服务器。 order-service makes a call to payment-service using feign.
order-service 使用 feign 调用 payment-service。
Activity 1活动一
I have used maven spotify plugin to create the docker image and a docker-compose file to create a network and make all services up and running.我使用 maven spotify 插件来创建 docker 图像和 docker-compose 文件来创建网络并使所有服务启动并运行。 This is all perfect.
这一切都很完美。
Activity 2活动二
Now I need to scale the services.现在我需要扩展服务。 So I removed the host port in the docker-compose file and then ran the command as follows:
于是我把docker-compose文件中的主机端口去掉,然后运行命令如下:
docker-compose up -d --scale order-service=2 --scale payment-service=2
This created 2 instances of the services and attached them to the eureka server.这创建了 2 个服务实例并将它们附加到 eureka 服务器。
docker-compose ps
The above command gave the following output上面的命令给出了以下 output
service-registry_order-service_1 sh -c java $JAVA_OPTS -Dja ... Up 0.0.0.0:32776->8181/tcp, 8182/tcp
service-registry_order-service_2 sh -c java $JAVA_OPTS -Dja ... Up 0.0.0.0:32777->8181/tcp, 8182/tcp
service-registry_payment-service_1 sh -c java $JAVA_OPTS -Dja ... Up 8181/tcp, 0.0.0.0:32774->8182/tcp
service-registry_payment-service_2 sh -c java $JAVA_OPTS -Dja ... Up 8181/tcp, 0.0.0.0:32775->8182/tcp
service-registry_service-registry_1 sh -c java $JAVA_OPTS -Dja ... Up 0.0.0.0:8761->8761/tcp
You can see that services are running in the available ports in the host system.您可以看到服务正在主机系统的可用端口中运行。 Now I need to add a load balancer so that I don't have to bother about the port in which the service is running.
现在我需要添加一个负载均衡器,这样我就不必担心运行服务的端口了。
I followed the post here and added the nginx configuration我按照这里的帖子添加了 nginx 配置
Step 1: Added the nginx.conf file as follows:-第 1 步:添加 nginx.conf 文件如下:-
user nginx;
events {
worker_connections 1000;
}
http {
server {
listen 4000;
location / {
proxy_pass http://order-service:8181;
}
}
}
Step 2: I have modified the docker-compose file to add nginx details.第 2 步:我已修改 docker-compose 文件以添加 nginx 详细信息。 The final file looks like below:
最终文件如下所示:
version: '3.7'
networks:
order-payment:
services:
service-registry:
image: shefzee/service-registry:0.0.1-SNAPSHOT
ports:
- "8761:8761"
restart: always
networks:
- order-payment
order-service:
image: shefzee/order-service:0.0.1-SNAPSHOT
ports:
#- "8181:8181"
- "8181"
restart: always
depends_on:
- service-registry
- payment-service
networks:
- order-payment
payment-service:
image: shefzee/payment-service:0.0.1-SNAPSHOT
ports:
#- "8182:8182"
- "8182"
restart: always
depends_on:
- service-registry
networks:
- order-payment
nginx:
image: nginx:latest
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- order-service
ports:
- "4000:4000"
Now, ran the docker-compose现在,运行 docker-compose
docker-compose up -d --scale order-service=2 --scale payment-service=2
and, listed the containers..并且,列出了容器..
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------
service-registry_nginx_1 nginx -g daemon off; Exit 1
service-registry_order-service_1 sh -c java $JAVA_OPTS -Dja ... Up 0.0.0.0:32776->8181/tcp, 8182/tcp
service-registry_order-service_2 sh -c java $JAVA_OPTS -Dja ... Up 0.0.0.0:32777->8181/tcp, 8182/tcp
service-registry_payment-service_1 sh -c java $JAVA_OPTS -Dja ... Up 8181/tcp, 0.0.0.0:32774->8182/tcp
service-registry_payment-service_2 sh -c java $JAVA_OPTS -Dja ... Up 8181/tcp, 0.0.0.0:32775->8182/tcp
service-registry_service-registry_1 sh -c java $JAVA_OPTS -Dja ... Up 0.0.0.0:8761->8761/tcp
If you see, nginx is not running.如果您看到,nginx 没有运行。 I am not able to figure out the issue here.
我无法在这里弄清楚这个问题。 Please help.
请帮忙。
What I need is, if I do, http://localhost:4000/ uri , it should forward the request to any of the order-service instances.我需要的是,如果我这样做, http://localhost:4000/ uri ,它应该将请求转发到任何订单服务实例。
Thanks !谢谢 !
I realized that I just need an API gateway and eureka will manage which instance it should route the request to.我意识到我只需要一个 API 网关,eureka 将管理它应该将请求路由到哪个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.