SpringCloud搭建Eureka集群(一)

xiaoxiao2025-09-02  5

参考文章:

   https://www.cnblogs.com/hfultrastrong/p/8549590.html

  http://www.cnblogs.com/lovefendi/p/9817740.html

Step1:新建工程,引入依赖

a. idea创建项目结构(使用SpringBoot 1.5.10)

b.依赖文件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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cloud</groupId> <artifactId>euruka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-server</name> <description>EurekaServer</description> <!--基于Springboot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <!--<version>2.0.2.RELEASE</version>--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!--<version>2.0.6.RELEASE</version>--> </dependency> <!-- 修改后立即生效, 热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <!--<version>2.0.6.RELEASE</version>--> </dependency> </dependencies> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号--> <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> <configuration> <!-- 没有该配置,devtools 不生效 --> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> <!-- spring Boot在编译的时候,是有默认JDK版本的,这里自定义指定JDK版本 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.yame.Application</mainClass> <useUniqueVersions>false</useUniqueVersions> </manifest> <manifestEntries> <Class-Path>./</Class-Path> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

Step2:在启动程序类头部加入注解

package com.lx.helloworld.eureka.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }

Step3:在Step2程中添加多实例的配置文件

application.yml配置详情如下:

    其中, registerWithEureka: false和fetchRegistry: false表明自己是一个eureka server.

--- spring: application: name: spring-cloud-eureka profiles: peer1 server: port: 8000 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/ server: #注意:更改Eureka更新频率将打破服务器的自我保护功能,生产环境下不建议自定义这些配置。 eviction-interval-timer-in-ms: 4000 # 清理间隔(单位毫秒,默认是60*1000) #关闭注册中心的保护机制, 默认false, 不推荐关闭 #eureka.server.enable-self-preservation: false --- spring: application: name: spring-cloud-eureka profiles: peer2 server: port: 8001 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/ --- spring: application: name: spring-cloud-eureka profiles: peer3 server: port: 8002 eureka: instance: hostname: peer3 #Eureka client 默认 30秒会向 Server 端发送一次心跳; #Server端 默认是90秒对没有接收到 client 端的续租请求会认为client挂机 #lease-renewal-interval-in-seconds: 10 client: serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ fetch-registry: true register-with-eureka: true 说明:   1、fetch-registry: 表示是否从eureka server获取注册信息, 如果是单一节点, 不需要同步其他eureka server节点, 则可以设置为false, 但此处为集群, 应该设置为true, 默认为true, 可不设置。   2、register-with-eureka: 表示是否将自己注册到eureka server, 构建集群环境时, 需要将自己注册到及群众, 所以应该开启。 默认为true,可不显式设置。

Step4:修改hosts文件:

127.0.0.1 peer1 127.0.0.1 peer2 127.0.0.1 peer3

Step5:启动各个eureka注册中心

有两种启动方法:

a. IDEA 启动

配置启动文件:

方法一:

 --spring.profiles.active=peer1

方法二:

 按照这个步骤添加三个启动节点,注意: program arguments 参数依次更改为 peer2 、peer3,与配置文件中的 spring.application.files 参数值对应。

之后全部启动。 

b. 打包成jar之后,使用 java -jar命令启动

java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 java -jar spring-cloud-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

注:启动过程会报如下错误, 但是是正常现象:

Step6:查看集群效果

http://peer1:8000/

Step7:集群理解

转载请注明原文地址: https://www.6miu.com/read-5035604.html

最新回复(0)