SpringCloud中的Eureka的集群配置

微服务框架中最为重要的就是注册中心,如果只是单注册中心,一旦出现问题,容易导致整个微服务环境不可用,所以建议注册中心集群。

目前SpringCloud框架中使用Eureka作为注册中心,本文简单介绍一下Eureka的集群配置,主要的思路就是相互注册,形成一组相互注册的注册中心,达到高可用的效果。

一般来说集群配置至少需要2台以上的服务器,这里是采用本机测试,道理是一样的。

集群配置

依赖配置

复制代码
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.3.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>


<!-- 管理依赖 -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-dependencies</artifactId>
			<version>Finchley.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>


<dependencies>
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>
</dependencies>

<repositories>
	<repository>
	    <id>spring-milestones</id>
	    <name>Spring Milestones</name>
	    <url>https://repo.spring.io/milestone</url>
	    <snapshots>
	        <enabled>false</enabled>
	    </snapshots>
	</repository>
</repositories>

注册中心配置

第一个注册中心配置

复制代码
# 服务端口号
server:
  port: 8100
# 服务名称必须一样
spring:
  application:
 	name: app-eureka
eureka:
  instance:
    # 注册中心ip地址(本机地址)
    hostname: 127.0.0.1
  client:
    service-url:
    # 注册地址,注册到9100的注册中心,如果是三台以上,一样可以在这边添加
    # 主要是实现相互注册
      defaultZone: http://127.0.0.1:9100/eureka/  
    # 将自己注册给自己的注册中心
    register-with-eureka: true
    # 检索自己服务信息
    fetch-registry: false

第二个注册中心配置

复制代码
# 服务端口号
server:
  port: 9100
# 服务名称必须一样
spring:
  application:
    name: app-eureka

eureka:
  instance:
    # 注册中心ip地址(本机地址)
    hostname: 127.0.0.1
  client:
    service-url:
    # 注册地址,注册到8100的注册中心,如果是三台以上,一样可以在这边添加
    # 主要是实现相互注册
      defaultZone: http://127.0.0.1:8100/eureka/  
    # 将自己注册给自己的注册中心
    register-with-eureka: true
    # 检索自己服务信息
    fetch-registry: false

启动

复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //开启EurekaServer服务 开启注册中心
public class AppEureka {

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

客户端配置

复制代码
# 订单服务的端口号
server:
  port: 8001
  
# 服务别名 -- 服务注册到注册中心名称
spring:
  application:
	name: app-order
    
eureka:
  client:
    service-url:
    # 当前订单服务注册到eureka服务地址,两个注册中心都注册
      defaultZone: http://localhost:8100/eureka,http://localhost:9100/eureka
    # 需要将服务注册到eureka
    register-with-eureka: true
    # 需要检索服务
    fetch-registry: true

客户端启动

复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class AppOrder {
	
	public static void main(String[] args) {
		SpringApplication.run(AppOrder.class, args);	
	}
}

总结

以上即是Eureka的集群配置,还是比较简单易懂的。

相关推荐
无心水8 小时前
【全域智能营销实战】2、Spring AI 模块化架构深度解读:从 1.0 到 2.0 的演进与最佳实践
人工智能·spring·架构·harness·顶尖架构师·全域智能营销·harmess
Tian_Hang9 小时前
Eclipse Ditto 物模型相关代码
java·运维·服务器·ide·eureka·eclipse
livemetee13 小时前
【关于Spring声明式事务】
java·后端·spring
Esaka_Forever14 小时前
Python 完整内存管理机制详解
开发语言·python·spring
殳翰15 小时前
spring对junit的支持
spring·junit·sqlserver
Devin~Y15 小时前
抖音级短视频推荐与直播带货平台面试实战:从 Java 微服务到 RAG 智能客服全链路解析
java·spring boot·redis·spring cloud·kafka·agent·rag
happyprince17 小时前
09-vLLM KV Cache 系统完整分析
java·spring·vllm
AI人工智能+电脑小能手17 小时前
【大白话说Java面试题 第154题】【06_Spring篇】第14题:Spring 支持的 Bean 作用域
java·开发语言·spring·面试
河阿里18 小时前
SLF4J深度指南(Java):从原理到 Spring 项目实战
java·开发语言·spring
Ricky_Theseus18 小时前
CrewAI 生产化:缓存、回调、LLM 配置
java·spring·缓存