SpringCloud之Eureka注册中心

SpringCloud之Eureka注册中心

 

前言

接触运用springcloud微服务很久了,工作中一直在用,一直没有时间写博文,作为一个开发人员,不善言辞,因此没什么可说的了,简单粗暴,直接上代码

一、EurekaServer搭建

1.1 创建EurekaServer注册中心服务

1.2 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">
    <parent>
        <artifactId>spring-cloud-study</artifactId>
        <groupId>com.wj</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>

    <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>
    </dependencies>

</project>

 1.3 配置文件application.yml

#端口号配置
server:
  port: 8761
#应用名称配置
spring:
  application:
    name: eureka-server
eureka:
  instance:
    # 设置注册中心hostname
    hostname: localhost
  client:
    # 取消检索服务;自己为服务端,本身就是维护服务实例,不需要去检索
    fetch-registry: false
    # 不把自己注册到注册中心
    register-with-eureka: false
  server:
    # 关闭注册中心的自我保护机制,默认开启
    enable-self-preservation: false

 1.4 启动类EurekaServerApplication

package com.wj.eureka.server;

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

/**
 * @author wangjian
 */
//开启eureka注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

二、EurekaClient搭建

2.1 搭建门户portal-server服务

2.2 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">
    <parent>
        <artifactId>spring-cloud-study</artifactId>
        <groupId>com.wj</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>portal-server</artifactId>

    <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-client</artifactId>
        </dependency>
    </dependencies>

</project>

2.3 application.yml 文件

# 端口号配置
server:
  port: 9100

# 应用名称配置
spring:
  application:
    name: portal-server

#eureka客户端配置
eureka:
  instance:
    # 向注册中心发送心跳间隔时间,告诉注册中心自己还活着;默认时间是30s
    lease-renewal-interval-in-seconds: 10
    # 如果30秒内没有向注册中心发送心跳,代表发生故障,从注册中心移除掉,默认时间是90s
    lease-expiration-duration-in-seconds: 30
    #告诉服务端,服务实例以ip作为连接,而不是机器名,默认是false
    prefer-ip-address: true
    #告诉服务端,服务实例的名称
    instance-id: ${spring.application.name}
  client:
    #注册中心连接地址
    service-url:
      defaultZone: http://localhost:8761/eureka

2.4 启动类PortalServerApplication配置

package com.wj.portal.server;

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

/**
 * @EnableDiscoveryClient和@EnableEurekaClient共同点:让注册中心发现,扫描到该服务。
 * 不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心,比如zookeeper。
 * @author wangjian
 */
//开启eureka客户端
@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class PortalServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(PortalServerApplication.class,args);
    }
}

2.5 @EnableEurekaClient和@EnableDiscoveryClient比较

@EnableEurekaClient:只能用于注册中心为eureka的情况,功能是让eureka发现服务

@EnableDiscoveryClient:适用于注册中心为eureka、consul、zookeeper等的情况,功能和@EnableEurekaClient相同,都是让注册中心发现服务。

参考文档:https://www.jianshu.com/p/f6db3117864f

三、注册中心访问

四、EurekaServer集群搭建

4.1 搭建EurekaServer第二个节点

4.1.1 复制一个节点,修改配置文件

#端口号配置
server:
  port: 8762
#应用名称配置
spring:
  application:
    name: eureka-server2
eureka:
  instance:
    # 设置注册中心hostname
#    hostname: localhost
    #以IP地址注册到服务中心,相互注册使用IP地址
#    prefer-ip-address: true
  server:
    # 关闭注册中心的自我保护机制,默认开启
    enable-self-preservation: false
  client:
    # 取消检索服务;自己为服务端,本身就是维护服务实例,不需要去检索
    fetch-registry: false
    # 不把自己注册到注册中心
    register-with-eureka: false
    service-url:
      #配置其余的注册中心地址,多个用逗号隔开
      defaultZone: http://127.0.0.1:8761/eureka/

4.1.2 修改第一个EurekaServer配置文件

#端口号配置
server:
  port: 8761
#应用名称配置
spring:
  application:
    name: eureka-server
eureka:
  instance:
    # 设置注册中心hostname
#    hostname: localhost
    #以IP地址注册到服务中心,相互注册使用IP地址
#    prefer-ip-address: true
  server:
    # 关闭注册中心的自我保护机制,默认开启
    enable-self-preservation: false
  client:
    # 取消检索服务;自己为服务端,本身就是维护服务实例,不需要去检索
    fetch-registry: false
    # 不把自己注册到注册中心
    register-with-eureka: false
    service-url:
      #配置其余的注册中心地址,多个用逗号隔开
      defaultZone: http://localhost:8762/eureka/

4.2 修改portal-server配置文件

# 端口号配置
server:
  port: 9100

# 应用名称配置
spring:
  application:
    name: portal-server

#eureka客户端配置
eureka:
  instance:
    # 向注册中心发送心跳间隔时间,告诉注册中心自己还活着;默认时间是30s
    lease-renewal-interval-in-seconds: 10
    # 如果30秒内没有向注册中心发送心跳,代表发生故障,从注册中心移除掉,默认时间是90s
    lease-expiration-duration-in-seconds: 30
    #告诉服务端,服务实例以ip作为连接,而不是机器名,默认是false
    prefer-ip-address: true
    #告诉服务端,服务实例的名称
    instance-id: ${spring.application.name}
  client:
    #注册中心连接地址
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://localhost:8762/eureka/

4.3 eureka访问

五、Eureka和Zookeeper的区别

分布式 CAP 理论指出,一个分布式系统不可能同时满足 C(一致性)、A(可用性) 和 P(分区容错性);

任何分布式只能同时保证两个,由于分区容错性在是分布式系统中必须要保证的,因此我们只能在 A 和 C 之间进行权衡,在此 Zookeeper 保证的是 CP, 而 Eureka 则是 AP。

p 全称:Partition tolerance (分区容忍)

主要是指网络问题, 比如:A 、B、C 三台机器之间相互ping不通、网络不通,这种情况在分布式系统里面是允许的,也是很有可能发生的,我们要容忍这种情况的出现,在这种情况出现的时候,我们 是选择 “一致性的C” 还是选择 “可用性的A”,就看应用场景。

Zookeeper 保证 CP

在 ZooKeeper 中,当 master 节点因为网络故障与其他节点失去联系时,剩余节点会重新进行 leader 选举,但是问题在于,选举 leader 需要一定时间, 且选举期间整个 ZooKeeper 集群都是不可用的,这就导致在选举期间注册服务瘫痪。在云部署的环境下,因网络问题使得 ZooKeeper 集群失去 master 节点是大概率事件,虽然服务最终能够恢复,但是在选举时间内导致服务注册长期不可用是难以容忍的。

Eureka 保证 AP

Eureka 优先保证可用性,Eureka 各个节点是平等的,某几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而 Eureka 的客户端在向某个 Eureka 注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台 Eureka 还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。

所以 Eureka 在网络故障导致部分节点失去联系的情况下,只要有一个节点可用, 那么注册和查询服务就可以正常使用,而不会像 zookeeper 那样使整个注册服务瘫痪,Eureka 优先保证了可用性;

总结

客户端配置详情参见资料:Eureka客户端配置详解

文章中部分内容来源于网络,如有侵权请告知!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值