Eureka

Eureka

1.eureka-server

1.1 pom文件

构建eureka-server微服务时,需要先在pom文件中引入相应的坐标

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2 配置文件

需要在配置文件中,对相应的参数进行配置application.yml

spring.application.name: eureka-server

server.port: 8761
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
  1. spring.application.name:定义服务名称
  2. server.port: 定义服务端口号为8761
  3. eureka.client.fetch-registry: 设置为false表示不要再本地缓存注册表信息
  4. eureka.client.registerWithEureka 属性会告知服务,在应用程序启动时不要通过Eureka服务注册,因为它本身就是Eureka服务
  5. 每次服务注册需要30s的时间才能显示在eureka服务中,因为eureka需要从服务接收3次连续心跳包ping,每次心跳包ping间隔10s,然后才能使用这个服务。
1.3 添加注解

在建立eureka服务时, 需要进行的最后一项工作是在服务启动类上添加注解

@SpringBootApplication
@EnableEurekaServer---Spring服务中启用Eureka服务器
public class EurekaServerApplication {
    public static void main(String[] args) {
    	SpringApplication.run(EurekaServerApplication.class, args);
    }
}

2.eureka-client

2.1pom文件

构建eureka-client服务的时候,在pom文件中引入相应坐标

<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-web</artifactId>
</dependency>
  1. spring-cloud-starter-netflix-eureka-client:eureka客户端包
  2. spring-boot-starter-web:spring的web包,需要引入此包,才能注册成功
2.2配置文件
eureka:
  instance:
    prefer-ip-address: true ⇽--- 注册服务的IP,
  client:
    register-with-eureka: true ⇽---向Eureka注册服务
    fetch-registry: true ⇽---拉取注册表的本地副本
    service-url:
      defaultZone: http://localhost:8761/eureka ⇽---Eureka服务的位置
2.3添加注解
@SpringBootApplication
@EnableDiscoveryClient---eureka客户端注解
public class EurekaClient1Application {

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

}

3.注册成功

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值