springBoot整合注册中心eureka

一、注册中心
1、新建service服务,导入jar包
spring-cloud-starter-eureka-server 已经被遗弃,现使用spring-cloud-starter-netflix-eureka-server

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

2、启动服务注解
启动服务类添加注解@EnableEurekaServer

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

3、添加配置信息

server:
  port: 8761   # 一般默认为8761

spring:
  application:
    name: eureka
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defalultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    register-with-eureka: false # 不允许注册服务到注册中心
    fetch-registry: false # 不允许请求被注册的服务

4、访问注册中心
http://localhost:8761/
注: 先启动注册中心,再启动client。

5、心跳机制
默认是30 ,注册中心每过一段时间和client服务交互一次,确认服务是正常,若服务异常则剔出服务。

二、client服务
1、新建client服务,导入jar包

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

2、启动服务注解
启动服务类添加注解@EnableEurekaClient

@EnableDiscoveryClient 和 @EnableEurekaClient 都是用于服务注册。
区别:
@EnableEurekaClient只能用于Eureka注册中心
@EnableDiscoveryClient 可用于任何注册中心

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

3、添加配置信息

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值