第八节--Eureka服务注册与发现

Eureka其它基本信息在第一季有
 
Eureka是什么?
                   Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件
      它主要包括两个组件:Eureka Server 和 Eureka Client
         Eureka Client:一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)
         Eureka Server:提供服务注册和发现的能力(通常就是微服务中的注册中心)
/*****************************************************配置服务端**********************************************/
  依赖,不能乱写,必须springboot,springcloud相对应
  项目结合ssm+eureka
 2.搭建步骤,springcloud版本要和springboot版本相对应,cloud-eureka也是cloud项目的
 
项目搭建:搭建集群eureka服务注册中心
第一步:新建modul,项目名称cloud-eureka-server7001 
第二步:pom.xml
<dependencies>
    <!-- eureka-server服务端:现在新版本2020,和之前使用D版本结合springboot1.5不一样 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>com.hx.springcloud</groupId>
        <artifactId>cloud-api-commos</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- actuator监控信息完善 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

    <!--热部署  -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

第二步:applicaiton.yml文件

server:
  #服务注册中心端口号
  port: 7001
eureka:
  instance:
    #服务注册中心实例的主机名 这里在自己的主机上取了别名eureka7001=localhost
    hostname: eureka7001
  client:
    #是否向服务注册中心注册自己
    register-with-eureka: false
    #是否检索服务
    fetch-registry: false
    service-url:
      #服务注册中心的配置内容,指定服务注册中心的位置,集群配置暴露另外两个eureka
      #defaultZone: http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
      defaultZone: http://eureka7001:7001/eureka/

第三步:主方法

@SpringBootApplication
@EnableEurekaServer //服务端eureka
public class Eureka7001 {
    public static void main(String[] args) {
        SpringApplication.run(Eureka7001.class,args);
    }
}
第四步:同理,多新建一个项目工程eureka-server-7002
 
第五步:把服务提供者和消费者注册到eureka中
 1 . pom.xmL中加入依赖
<!-- 将微服务provider注册进eureka:引入eureka客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2 .在主方法中使用客户端组件

@EnableEurekaClient
@SpringBootApplication
public class OrderMain80 {

public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
3 .在appoicaiton.yml中填写注册基本信息
 
 

Eureka如何禁止自我保护,默认为开启
eureka:
  server:
    enable-self-preservation: false #关闭自我保护机制 不可用的及时剔除
    eviction-interval-timer-in-ms: 2000 #关闭的时间秒
eureka:
  instance:
    #服务注册中心实例的主机名 这里在自己的主机上取了别名eureka7001=localhost
    hostname: eureka7002
    #eureka客户端向服务端发送心跳的时间间隔 单位为秒(默认是30秒)
    lease-renewal-interval-in-seconds: 1
    #eureka服务端收到最后一次心跳等待时间上限 单位秒(默认90秒) 超时将剔除服务
    lease-expiration-duration-in-seconds: 2
 
原文地址:https://www.cnblogs.com/hexublog/p/13681782.html