docker-compose部署eureka服务端、客户端的案例代码
作者:MAO_JIN_DAO
这篇文章主要介绍了docker-compose部署eureka服务端、客户端,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
使用场景
使用docker-compose方式部署eureka的服务端和客户端,并使用容器ip进行通信。
原理是使用docker-compose默认在一个网络下,并提供dns下的通过service通信的功能。
该方法也可以用于其它形式的容器内部服务调用的功能实现。
服务端主要代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.9</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.liuyang.sc</groupId> <artifactId>eurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eurekaserver</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.6</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: eureka-server server: port: 29108 #服务注册中心端口号 eureka: instance: hostname: 127.0.0.1 #服务注册中心IP地址 client: registerWithEureka: false #是否向服务注册中心注册自己 fetchRegistry: false #是否检索服务 serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
main.java
package com.liuyang.sc.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableEurekaServer @SpringBootApplication public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }
客户端主要代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.9</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sc</groupId> <artifactId>gateway-project</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway-project</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.6</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: gateway cloud: gateway: discovery: locator: enabled: true server: port: 29107 eureka: client: service-url: defaultZone: http://eurekaserver:29108/eureka/ #此处的eurakaserver为docker-compose.yml中eureka-server工程的容器定义 instance: prefer-ip-address: true # 使用IP注册,避免容器id:port调用报错
main.java
package com.sc.gatewayproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @EnableDiscoveryClient @SpringBootApplication public class GatewayProjectApplication { public static void main(String[] args) { SpringApplication.run(GatewayProjectApplication.class, args); } }
粗陋的DockerFile
eurekaserver
FROM java:openjdk-8u111-jdk COPY eurekaserver-0.0.1-SNAPSHOT.jar app.jar EXPOSE 29108 ENTRYPOINT ["java","-Xmx128m","-jar","app.jar"]
gateway
FROM java:openjdk-8u111-jdk COPY gateway-project-0.0.1-SNAPSHOT.jar app.jar EXPOSE 29107 ENTRYPOINT ["java","-Xmx128m","-jar","app.jar"]
docker-compose.yml
version: '2' services: eurekaserver: # 注意此处的名称需要添加到eureka客户端的defaultZone中,相当于docker内的主机名 #container_name: eureka_server image: maojindaobike/eurekaserver:230315 ports: - "29108:29108" gateway: #container_name: gateway image: maojindaobike/gateway:230316_v5 ports: - "29107:29107" depends_on: - eurekaserver
启动
docker-compose up -d
扩容
扩容时,需要显式端口映射,避免端口冲突
docker-compose.yml
version: '3' services: eurekaserver: #container_name: eureka_server image: maojindaobike/eurekaserver:230315 ports: - "29108:29108" gateway: #container_name: gateway image: maojindaobike/gateway:230316_v5 ports: - "29107" depends_on: - eurekaserver
docker-compose up -d docker-compose up -d --scale gateway=3
docker-compse ps
root@dockerserver:~/springcloud-dockerpose# docker-compose ps /snap/docker/2746/lib/python3.6/site-packages/paramiko/transport.py:32: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography (40.0) will be the last to support Python 3.6. from cryptography.hazmat.backends import default_backend Name Command State Ports ------------------------------------------------------------------------------------------------------------------------- springcloud-dockerpose_eurekaserver_1 java -Xmx128m -jar app.jar Up 0.0.0.0:29108->29108/tcp,:::29108->29108/tcp springcloud-dockerpose_gateway_1 java -Xmx128m -jar app.jar Up 0.0.0.0:49154->29107/tcp,:::49154->29107/tcp springcloud-dockerpose_gateway_2 java -Xmx128m -jar app.jar Up 0.0.0.0:49156->29107/tcp,:::49156->29107/tcp springcloud-dockerpose_gateway_3 java -Xmx128m -jar app.jar Up 0.0.0.0:49155->29107/tcp,:::49155->29107/tcp
到此这篇关于docker-compose部署eureka服务端、客户端的文章就介绍到这了,更多相关docker compose部署eureka内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!