溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

docker-compose怎么部署eureka服務(wù)端和客戶端

發(fā)布時(shí)間:2023-03-21 09:52:37 來源:億速云 閱讀:304 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“docker-compose怎么部署eureka服務(wù)端和客戶端”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

使用場(chǎng)景

使用docker-compose方式部署eureka的服務(wù)端和客戶端,并使用容器ip進(jìn)行通信。
原理是使用docker-compose默認(rèn)在一個(gè)網(wǎng)絡(luò)下,并提供dns下的通過service通信的功能。
該方法也可以用于其它形式的容器內(nèi)部服務(wù)調(diào)用的功能實(shí)現(xiàn)。

服務(wù)端主要代碼

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 #服務(wù)注冊(cè)中心端口號(hào)
eureka:
  instance:
    hostname: 127.0.0.1 #服務(wù)注冊(cè)中心IP地址
  client:
    registerWithEureka: false #是否向服務(wù)注冊(cè)中心注冊(cè)自己
    fetchRegistry: false #是否檢索服務(wù)
    serviceUrl: #服務(wù)注冊(cè)中心的配置內(nèi)容,指定服務(wù)注冊(cè)中心的位置
      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注冊(cè),避免容器id:port調(diào)用報(bào)錯(cuò)

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中,相當(dāng)于docker內(nèi)的主機(jī)名
    #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

啟動(dòng)

docker-compose up -d

docker-compose怎么部署eureka服務(wù)端和客戶端

擴(kuò)容

擴(kuò)容時(shí),需要顯式端口映射,避免端口沖突
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-compose怎么部署eureka服務(wù)端和客戶端

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服務(wù)端和客戶端”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI