为了账号安全,请及时绑定邮箱和手机立即绑定

SpringCloud(3)---Eureka服务注册与发现

标签:
Java

Eureka服务注册与发现

 

一、Eureka概述

 1、Eureka特点

  (1) Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。

  (2) Eureka 主管服务注册与发现,在微服务中,以后了这两者,只需要使用服务的标识符(==就是那个在每个服务的yml文件中取得服务名称==),

          就可以访问到服务,不需要修改服务调用的配置文件。

  (3) Eureka遵循AP原则(高可用,分区容错性),因为使用了自我保护机制所以保证了高可用。

2、Eureka两大组件

    两大组件:Eureka Server(提供注册服务)、 Eureka Client(JAVA客户端,负责发送心跳)

   系统中的其他微服务使用Eureka客户端连接到Eureka服务端维持心跳连接(即注册)。SpringCloud的其他模块可以通过Eureka Server 来发现系统中的微服务并加以调用

https://img1.sycdn.imooc.com//5b8bb28f00012b2a06380307.jpg

3、Eureka三大角色

            Eureka Server:提供服务注册和发现

        Service Provider:服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到

     Service Consumer:服务消费方,从Eureka获取注册服务列表,从而能够消费服务。

 https://img1.sycdn.imooc.com//5b8bb2970001febb05820361.jpg

 

二、Eureka Server服务注册中心

    1、pom.xml

<!--注册服务中心的jar要多个-server--><dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>

    2、application.yml

复制代码

server:  port: 7001eureka:  instance:    hostname: localhost  client:  #声明自己是个服务端
    registerWithEureka: false    #false表示不向注册中心注册自己
    fetchRegistry: false         #false表示自己就是注册中心,职责是维护实例,不参加检索
    serviceUrl:                  #设置eureka server的交互地址,即对外暴露的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

复制代码

    3、启动类

复制代码

//注意:要在类前加@EnableEurekaServer标注@SpringBootApplication@EnableEurekaServerpublic class Eureka7001_APP {    public static void main(String[] args) {
        SpringApplication.run(Eureka7001_APP.class,args);
    }
}

复制代码

运行结果

https://img1.sycdn.imooc.com//5b8bb29f000190bd15020192.jpg

 

三、Service Provider服务提供方

假设这个商品微服务。

   1、pom.xml

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

   2、application.yml

复制代码

server:  port: 8001#指定注册中心地址eureka:  client:    serviceUrl:      defaultZone: http://localhost:7001/eureka/#服务的名称spring:  application:    name: product-service

复制代码

     3、启动类

复制代码

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

复制代码

    4、启动后查看服务注册中心

 发现在服务注册中心已经注册了一个服务

https://img1.sycdn.imooc.com//5b8bb2a80001cfa621080176.jpg

    5、换端口号再启动一个

https://img1.sycdn.imooc.com//5b8bb2ae0001d54911120106.jpg

   6、在看服务中心

 https://img1.sycdn.imooc.com//5b8bb2b40001b20f22040174.jpg

这就是搭建了商品微服务集群。

 

四、Service Consumer服务消费方

      其实服务方和消费在配置时候没有任何区别,它们都属于Eureka Client组件。只是涉及服务间的调用,所以就把被调方称为提供方,调用方称为消费方。就好比订单微服务,

订单服务肯定需要去调商品微服务,所以这个订单微服务对于商品来讲可以理解服务提供方。一个微服务即可以是服务方也同时是提供方。

      1、pom.xml

    <!--这个对于每个不是注册中心的微服务都要添加-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

     2、application.yml

复制代码

server:  port: 9001#指定注册中心地址eureka:  client:    serviceUrl:      defaultZone: http://localhost:7001/eureka/#服务的名称spring:  application:    name: order-service

复制代码

    3、启动类

复制代码

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

复制代码

4、查看注册中心

发现订单微服务也成功注册到注册中心

https://img1.sycdn.imooc.com//5b8bb2bf0001a63c21740258.jpg

 

至于订单微服务如何调商品微服务呢,下一遍博客在写咯。

 

     我只是偶尔安静下来,对过去的种种思忖一番。那些曾经的旧时光里即便有过天真愚钝,也不值得谴责。毕竟,往后的日子,还很长。不断鼓励自己,

 天一亮,又是崭新的起点,又是未知的征程(上校5)

 

原文出处:https://www.cnblogs.com/qdhxhz/p/9357502.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消