springcould(1)Eureka服务发现与注册

Eureka学习笔记



概念

Eureka是一个服务发现框架(SpringCloud将Eureka集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能)

Eureka包含两个组件:Eureka Server(服务方)和Eureka Client(消费方)

Eureka的构成(三个部分):

  1. Eureka Server(注册中心)提供服务的发现与注册
  2. .Server Provider(服务提供方)服务提供方在注册中心注册服务,向消费方提供服务
  3. Server Consumer(服务消费方)服务消费方从注册中心中调用自己所需的服务

服务启动后,会向Eureka Server注册中心中注册自己,作为一个服务提供方的同时也是消费方,这样,多个服务之间就可以互相调用

Eureka的特点:

  • 同步:每个Eureka Server注册中心同时也是Eureka Client,多个注册中心通过共享注册信息的方式同步所有Eureka Server注册信息,实现高可用
  • 识别:Eureka Client会自动缓存注册中心注册的服务,这样即使某个服务宕掉,其调用该服务的消费方依然可用从缓存中调用服务
  • 续约:服务会周期性的(默认30秒一次)的向Eureka Server注册中心发送心跳(证明自己的服务还活着)来进行续约
  • 续期:Eureka Server注册中心会周期性的(默认60秒一次)执行失效服务检测,发现超过一定时间(默认90秒)没有Renew(更新)的服务后,会注销掉失效服务节点

1.创建Eureka Server注册中心

创建一个项目,作为Eureka Server注册中心

1.引入eureka-server依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
</dependencies>
2.配置eureka

在application.yml中配置eureka
基础配置

server:
  port: 56798
spring:
  application:
    name: jwxt-eureka02   #当前应用的名称
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/kytms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root

eureka的相关配置

eureka:
  instance:
    hostname: server1 #eureka的地址
    prefer-ip-address: true #是否显示ip
    instance-id: my_eureka56799 #注册后的实例id
  client:
    fetch-registry: false #是否拉取服务表(当前eureka是否是服务方)
    register-with-eureka: false #是否将当前服务注册到eureka中
    service-url: #服务的路径(当前服务要注册到哪个服务中)
      defaultZone: http://localhost:56799/eureka/ #指定默认的注册地址(当使用security时要加入对应的用户名和密码http://g01:123456@server2:8762/eureka/    其他端使用方法一致)
  server:
    enable-self-preservation: false   #是否开启eureka的自我保护机制(开发环境关闭,生产环境开启)
    eviction-interval-timer-in-ms: 60000 #指定心跳检测周期的时间(默认为60秒)
3.配置启动类

@EnableEurekaServer:在启动类上加入@EnableEurekaServer注解,标记当前项目是eureka的注册中心

@SpringBootApplication
@EnableEurekaServer     //标记当前项目是eureka注册中心
public class Eureka02Application {
    public static void main(String[] args) {
        SpringApplication.run(Eureka02Application.class,args);
    }
}

在这里插入图片描述

2.向Eureka注册中心中注册服务

在要注册到eureka注册的项目中

1.引入eureka-client依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
</dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
2.配置eureka-client
eureka:
  client:
    service-url:
      # 当需要注册到多个服务中时通过 , 分隔服务地址
      defaultZone : http://localhost:56798/eureka/,http://localhost:56799/eureka/
  instance:
    prefer-ip-address: true # 是否显示ip
    instance-id: teacher # 在注册中心中要显示的注册名称(不可以重名)
3.配置客户端的启动类
@SpringBootApplication
@EnableEurekaClient // 将当前服务注册到Eureka Server注册中心中
public class TeacherApplication {
    public static void main(String[] args) {
        SpringApplication.run(TeacherApplication.class,args);
    }
}

启动完成后,如果eureka页面的列表中出现该服务名称,则表示注册服务成功
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值