Spring Cloud Netflix Eureka 详解

搭建单节点Eureka Server

创建 eureka-server 项目,其pom文件如下:

<!-- eureka 服务端依赖 -->
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!--打包插件-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.yangsx95.demo.netflix.EurekaServerApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

创建一个SpringBoot启动类:

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

/**
 * @author yangsx
 * @version 1.0
 * @date 2020/5/30
 */
@SpringBootApplication
@EnableEurekaServer // 启用EurekaServer
public class EurekaServerApplication {

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

}

配置项目相关参数:

spring:
  application:
    name: eureka-server
server:
  port: 8761

启动项目后访问, http://localhost:8761/即可加载主页EurekaServer主页:

file

搭建高可用Eureka Server

在生产环境中,由于单点环境可用性较差,所以一般不会部署单点服务。幸好Eureka Server本身就支持集群部署,这里演示部署三节点的Eureka Server集群。Eureka Server集群的原理就是每个Server

application.yml中添加三个profile,分别配置集群三个节点的信息:

spring:
  application:
    name: eureka-server-ha

# 分别定义三个配置信息
---

spring:
  profiles: peer1
server:
  port: 8761
eureka:
  instance:
    # 主机名,需要在/etc/hosts中配置
    hostname: peer1
  client:
    # Eureka Server本身也集成了Eureka Client,彼此通过Eureka Client同步数据给其它实例又或者从其他实例同步数据
    # 是否注册到eureka上,如果需要将当前eureka注册到其他eureka(defaultZone中定义的)上,则设置true
    # 如果是单机环境,可以设置为false
    register-with-eureka: false
    # 是否从其他eureka中同步数据,当前为集群环境,需要同步
    fetch-registry: true
    serviceUrl:
      # 将自己注册到peer2这个Eureka上面去
      defaultZone: http://peer2:8762/eureka/,http://peer2:8763/eureka/

---

spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    register-with-eureka: false
    fetch-registry: true
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer1:8763/eureka/

---

spring:
  profiles: peer3
server:
  port: 8763
eureka:
  instance:
    hostname: peer3
  client:
    register-with-eureka: false
    fetch-registry: true
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer1:8761/eureka/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值