Eureka入门

123 阅读4分钟

入门

官网介绍

Service Discovery is one of the key tenets of a microservice based architecture. Trying to hand configure each client or some form of convention can be very difficult to do and can be very brittle. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.
服务发现是基于微服务的架构的关键原则之一。尝试手动配置每个客户端或某种形式的约定可能非常困难并且非常脆弱。Eureka 是 Netflix 服务发现服务器和客户端。服务器可以配置和部署为高可用性,每个服务器将有关注册服务的状态复制到其他服务器。

什么是服务注册与发现

Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。

在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))

eureka两个模块

作为服务端

各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。每一个EurekaServer都是注册中心,维护服务实例,并不需要去检索服务。

作为客户端

也是一个微服务模块,只不过它是用来具体实现某个业务的微服务模块。然后把他们注册到服务中心去。如:euerka7001,eureka7002等。

image.png

快速入门

  1. 导入pom依赖
如果是服务端
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
客户端
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. 写.yml配置文件
服务端
server:
  port: 7001
eureka:
  instance:
    #eureka服务端的实例名称
    hostname: eureka7001.com
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false

    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
  port: 8001
  
spring:
  application:
    name: cloud-payment-service #eureka客户端的实例名称
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.hellojava.springcloud.pojo
  configuration:
   map-underscore-to-camel-case: true

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka #需要注册到的服务地址
  instance:
    instance-id: payment8001
    prefer-ip-address: true
  1. 编写主启动类
@SpringBootApplication
@EnableEurekaClient //当前为客户端
public class PaymentMain8001 {
    public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class, args);
    }
}
@SpringBootApplication
@EnableEurekaServer //当前为服务端
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}
  1. 写业务 controller,service,mapper,mapper.xml等

  2. 进行测试 localhost:7001/ localhost:8001/payment/get/1

集群部署

集群部署的原因

搭建Eureka注册中心集群 ,实现负载均衡+故障容错,如果只有一台服务,宕机后整个项目崩溃,影响严重。

步骤

  1. 新建几个euerka微服务项目
  2. 添加依赖、配置和启动类等
  3. 在c盘下找到找到C:\Windows\System32\drivers\etc路径下的hosts文件,
  4. 修改hosts,添加
    127.0.0.1 eureka7001.com

127.0.0.1 eureka7002.com 5. 测试

自我保护

默认是开启的。