Eureka-搭建

1.搭建Eureka服务端

搭建一个简单的Eureka服务端十分容易(搭建高可用的注册中心也不难,Eureka直接支持)。 
- 第一步:添加Mavan依赖 :加入Spring Boot和Spring Cloud相关依赖,并对应好版本; 
- 第二步:注解启用类 :在启用类上加入@EnableEurekaServer注解; 
- 第三步:配置application.yml文件 :修改默认配置,适应生产环境。

第一步:添加Mavan依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>euraka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>euraka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

第二步:注解启用类 :

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

第三步:配置application.yml文件 

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  server:
    enable-self-preservation: false      # 关闭自我保护模式(默认为打开)
    eviction-interval-timer-in-ms: 5000  # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

eureka.client.register-with-eureka:对于同一个进程,既可以作为注册中心的服务端,也可以作为注册中心的客户端,在本例中,注册中心提供单一的注册服务功能,不提供其他接口服务能力,所以设置为false,最终不作为服务实例注册在注册中心中供其他服务调用。 

eureka.server.eviction-interval-timer-in-ms:eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒。服务下线存在两种情况,一种是客户端主要发送“服务下线”的请求,另外一种是通过定时任务扫描,每隔固定的时间清理无效的节点信息。清理无效节点的策略由客户端决定,客户端节点会设置相应的上报时间和最大超时时间(最大超时时间必须超过上报时间,推介设置为3倍关系),eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。!

 启动Spring Boot程序,访问http://localhost:8761/即可访问到如下web界面:

如图所示,注册中心已正常启动,并关闭了保护模式,且当前尚无实例注册到该Eureka Server。

 

2.搭建Eureka客户端

成功搭建Eureka Server后,接着我们就可以将服务实例注册到该服务中心了,具体步骤如下: 
- 第一步:添加Mavan依赖 :加入Spring Boot和Spring Cloud相关依赖,并对应好版本; 
- 第二步:注解启用类 :在启用类上加入@EnableEurekaClient注解; 
- 第三步:配置application.yml文件 :修改默认配置,适应生产环境。


第一步:添加Mavan依赖

Eureka 客户端的maven配置和服务器端配置基本相同,唯一不同的地方在于客户端需要设置健康状态信息,需要引入spring-boot-starter-actuator依赖,如下所示:

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

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

注意一定要 添加spring-boot-starter-web依赖,不然客户端注册会报204错误。

 

第二步:注解启用类

在启用类上加入@EnableEurekaClient注解即可,通过该注解,项目下所有被@RestController注解的对外服务接口将可通过注册-发布的方式对外提供服务。

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

}

 

第三步:配置application.yml文件

server:
  port: 8762
spring:
  application:
    name: service-hi
  #zipkin:
    #base-url: http://localhost:9411

eureka:
  instance:
    lease-renewal-interval-in-seconds: 5      # 心跳时间,即服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 15  # 发呆时间,即服务续约到期时间(缺省为90s)
  client:
    registry-fetch-interval-seconds: 10 # 拉取服务注册信息间隔(缺省为30s)
    service-url:
      defaultZone: http://localhost:8761/eureka/
    healthcheck:
      enabled: true # 开启健康检查(依赖spring-boot-starter-actuator)

eureka.instance.lease-renewal-interval-in-seconds: 表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量,默认30秒。

eureka.instance.lease-expiration-duration-in-seconds: lease-expiration-duration-in-seconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance,该值默认为90秒。

eureka.client.registry-fetch-interval-seconds: 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒。

配置结束之后,启动Spring-Boot程序,可以发现原来的注册界面多了一个注册的实例,则注册成功: 


è¿éåå¾çæè¿°

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值