SpringCloud -- Eureka集群搭建


依赖包

单机版Eureka相同。

<?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.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.meihaocloud</groupId>
	<artifactId>SCEureka</artifactId>
	<version>0.0.1</version>
	<name>SCEureka</name>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR4</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>
		</plugins>
	</build>

</project>

启动类

package com.meihaocloud.scblog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ScEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(ScEurekaApplication.class, args);
	}

}


application.yml

spring:
  application:
    name: Eureka-7001
  profiles: 7001
server:
  port: 7001
eureka:
  client: #客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
    register-with-eureka: true #false则当前微服务不注册到eureka中
    fetch-registry: true  #false则不通过eureka获取注册信息
  instance: #eureka实例定义
    hostname: Eureka-7001 #定义eureka实例所在的主机名称
    instance-id: Eureka-7001 #在信息列表时显示主机名称
    prefer-ip-address: true  #访问路径变为IP地址

#注意这里是三个"减号"
---
spring:
  application:
    name: Eureka-7002
  profiles: 7002
server:
  port: 7002
  
eureka:
  client: #客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
    register-with-eureka: true #false则当前微服务不注册到eureka中
    fetch-registry: true  #false则不通过eureka获取注册信息
  instance: #eureka实例定义
    hostname: Eureka-7002 #定义eureka实例所在的主机名称
    instance-id: Eureka-7002 #在信息列表时显示主机名称
    prefer-ip-address: true  #访问路径变为IP地址

---
spring:
  application:
    name: Eureka-7003
  profiles: 7003
server:
  port: 7003
eureka:
  client: #客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
    register-with-eureka: true #false则当前微服务不注册到eureka中 
    fetch-registry: true  #false则不通过eureka获取注册信息
  instance: #eureka实例定义
    hostname: Eureka-7003 #定义eureka实例所在的主机名称
    instance-id: Eureka-7003 #在信息列表时显示主机名称
    prefer-ip-address: true  #访问路径变为IP地址



开发时启动集群,根据profiles启动。

Eclipse中找到Run--> Run Configurations  --> SpringBoot APP  , 选中启动类,右边输入profile,然后run

分别启动7001/7002/7003


image.png


启动日志:

4fccba5107f7439ea141c89bd1a243be1045156.png




http://localhost:7001


865fdf8519724263b12aaa757c5252d51223860.png


访问http://localhost:7002  

和  

http://localhost:7003  

都可以看到 3个eureka都已经注册成功, 


集群搭建成功。




Eureka客户端访问配置:

spring:
  application:
    name: OrderService-8001
server:
  port: 8001
eureka:
  client: #客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
  instance: #eureka实例定义
    hostname: OrderService-8001 #定义eureka实例所在的主机名称
    instance-id: OrderService-8001 #在信息列表时显示主机名称
    prefer-ip-address: true  #访问路径变为IP地址










eureka 集群

2020.11.18 00:29

https://www.meihaocloud.com.com/268.html , 欢迎转载,请在文章页标出原文连接 !


Copyright © 2020 千夕网 联系站长

粤公网安备 44030302001408号 粤ICP备19099833号-1