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

Eureka服务注册与发现

 GitHub地址https://github.com/yudiandemingzi/spring-cloud-studygit

1、Eureka概述

 一、Eureka特色

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

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

          就能够访问到服务,不须要修改服务调用的配置文件。app

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

二、Eureka两大组件

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

   系统中的其余微服务使用Eureka客户端链接到Eureka服务端维持心跳链接(即注册)。SpringCloud的其余模块能够经过Eureka Server 来发现系统中的微服务并加以调用fetch

三、Eureka三大角色

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

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

     Service Consumer:服务消费方,从Eureka获取注册服务列表,从而可以消费服务。server

 

 

2、Eureka Server服务注册中心

    一、pom.xml

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

    二、application.yml

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

    三、启动类

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

运行结果:输入:http://localhost:7001/

 

3、Service Provider服务提供方

假设这个商品微服务。

   一、pom.xml

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

   二、application.yml

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

     三、启动类

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

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

 发如今服务注册中心已经注册了一个服务

    五、换端口号再启动一个

   六、在看服务中心

 

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

 

4、Service Consumer服务消费方

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

订单服务确定须要去调商品微服务,因此这个订单微服务对于商品来说能够理解服务提供方。一个微服务便可以是服务方也同时是提供方。

      一、pom.xml

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

     二、application.yml

server:
  port: 9001

#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

#服务的名称
spring:
  application:
    name: order-service

    三、启动类

@SpringBootApplication
public class OrderApplication {

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

四、查看注册中心

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

 

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

 

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

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

相关文章
相关标签/搜索