ITPub博客

首页 > IT职业 > IT生活 > SpringCloud(3)---Eureka服务注册与发现

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

IT生活 作者:yzf01 时间:2021-09-09 09:51:06 0 删除 编辑

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 来发现系统中的微服务并加以调用

图片描述

3、Eureka三大角色

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

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

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

 图片描述

 

二、Eureka Server服务注册中心

    1、pom.xml

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

    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);
    }
}

图片描述

运行结果

图片描述

 

三、Service Provider服务提供方

假设这个商品微服务。

   1、pom.xml

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

   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、启动后查看服务注册中心

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

图片描述

    5、换端口号再启动一个

图片描述

   6、在看服务中心

 图片描述

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

 

四、Service Consumer服务消费方

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

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

      1、pom.xml

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

     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、查看注册中心

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

图片描述

 

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

 

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

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

 

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2334/viewspace-2813386/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-09-19

  • 博文量
    88
  • 访问量
    74987