您好,登錄后才能下訂單哦!
本篇內容介紹了“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服務端和客戶端”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。