SpringCloud注册中心eureka


在springcloud中eureka被用来作为一个注册中心组件。

但是eureka2.x已经不再维护了,不过目前使用还是没什么问题,而且1.x也够用了。而且还有多种注册中心可供选择,比如consul。

eureka服务端搭建

依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
</parent>
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
</dependency>
<!--eureka服务服务端需要引入的依赖-->
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

配置文件

spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  client:
    serviceUrl:
      #与eureka客户端(将要注册的服务)交互的uri
      defaultZone: http://localhost:8761/eureka
      #不注册自己
    registerWithEureka: false
    fetchRegistry: false

启动类

@SpringBootApplication
//启动eureka服务端的注解
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run( EurekaServerApplication.class, args );
    }
}

此时启动工程,在浏览器中输入http://localhost:8761/,你将会看到以下界面,表示启动eureka服务启动成功了。
在这里插入图片描述

服务注册

依赖


<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
</parent>
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
</dependency>
<!--注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<!--服务调用的依赖-->

配置

spring:
  application:
    name: client-service

eureka:
  client:
    serviceUrl:
    #总得知道在哪注册
      defaultZone: http:/localhost:8761/eureka/

启动类

添加上发现的注解@EnableDiscoveryClient


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


启动项目,然后刷新http://localhost:8761/,你会发现有一个服务已经注册成功了。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pbeKWaYw-1596249656690)(springcloud-注册中心eureka/eureka-client.jpg)]

流程简述

当eureka启动,开始监听8761端口,当client启动,会发送一条消息给localhost:8761,eureka收到消息后就将这条消息(含有该服务的地址与名字)给存起来,注册成功。

服务会周期性地向Eureka Server发送心跳以续约自己的信息。如果Eureka Server在一定时间内没有接收到某个微服务节点的心跳,将会注销该微服务节点

Eureka Client会缓存Eureka Server中的信息。即使所有的Server节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值