温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

docker-compose怎么部署eureka服务端和客户端

发布时间:2023-03-21 09:52:37 来源:亿速云 阅读:340 作者:iii 栏目:开发技术

本篇内容介绍了“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怎么部署eureka服务端和客户端

扩容

扩容时,需要显式端口映射,避免端口冲突
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服务端和客户端

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服务端和客户端”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI