Springboot中Eureka的使用方法

在微服务架构中,注册中心是核心的基础服务。

它主要记录各个微服务和微服务地址的映射关系,各个微服务都将自己注册到这个注册中心上面,当微服务之间需要互相调用时,就可以从注册中心上面去发现微服务和进行调用。
Spring Cloud是一个开箱即用的微服务框架,秉承了微服务的真正理念而设计。

Spring Cloud中,谁是服务的注册中心 ? Eureka Server即服务的注册中心,核心配置如下

1. 加入依赖,pom.xml

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

2. 程序入口类添加@EnableEurekaServer,启用Eureka Server。例如:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class SpringCloudEurekaServerApplication {

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

3. 单机版Eureka Server,配置application.yml

server:
  port: 8761
eureka:
  instance:
    hostname: localhost                                                         # 服务注册中心实例的主机名
    lease-renewal-interval-in-seconds: 30                                       # 客户端向Eureka发送心跳周期(s)
    lease-expiration-duration-in-seconds: 90                                    # Eureka Server接收实例的最后一次发出的心跳后,删除需要等待时间(s)
  server:
    enable-self-preservation: true                                              # Eureka自我保护模式
  client:
    register-with-eureka: false                                                 # 是否向服务注册中心注册自己
    fetch-registry: false                                                       # 是否检索发现服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    # 指定服务注册中心的位置

4. Eureka Server集群配置(HA,高可用),实际上就是将自己作为服务向其他服务注册中心注册自已,这样就可以形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果。多个Eureka Server的实例互相注册。配置application.yml

Eureka Server 实例1 配置
server:
  port: 8761
eureka:
  instance:
    hostname: localhost                                                         # 服务注册中心实例的主机名
  server:
    enable-self-preservation: true                                              # Eureka自我保护模式
  client:
    register-with-eureka: true                                                  # 是否注册到Eureka Server
    fetch-registry: true                                                        # 是否检索发现服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8762/eureka/,http://${eureka.instance.hostname}:8763/eureka/  
      #指定多个服务注册中心的位置,并向服务注册中心注册自己
      
Eureka Server 实例2 配置
server:
  port: 8762
eureka:
  instance:
    hostname: localhost                                                         # 服务注册中心实例的主机名
  server:
    enable-self-preservation: true                                              # Eureka自我保护模式
  client:
    register-with-eureka: true                                                  # 是否注册到Eureka Server
    fetch-registry: true                                                        # 是否检索发现服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/,http://${eureka.instance.hostname}:8763/eureka/  
      #指定多个服务注册中心的位置,并向服务注册中心注册自己

== 谁注册到Eureka Server ? Eureka Client 即注册的服务,基本配置如下==
1. 加入依赖,pom.xml

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

2. 主类上面加入@EnableDiscoveryClient ,开启该注解使该服务能够被注册中心发现,例如:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

3. 客户端的最基本的配置,application.yml ,例如:

server:
  port: 8090
spring:
  application:
    name: spring-cloud-eureka-client-application 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

4. 启动Eureka Server,访问http://localhost:8761 可以进入到Eureka Server的查看页面 :
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青春是首不老歌丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值