Spring-Boot-Admin-结合Eureka注册中心使用

Spring-Boot-Admin-结合Eureka注册中心使用


一、搭建Eureka注册中心

1、代码

pom.xml 中添加依赖

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

在这里插入图片描述

启动类中添加注解:@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

application.yml 添加配置

server:
  port: 10086
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url:
      # eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址,用逗号分割
      defaultZone: http://127.0.0.1:10086/eureka
    # 注册自己
    register-with-eureka: false
    # 拉取服务
    fetch-registry: false

二、Admin 结合Eureka

  1. admin-server 会自己拉取 Eureka 上注册的服务信息,自动完成注册。
  2. admin-client 不需要配置 admin 地址,一切全部由 admin-server 拉取 Eureka 上注册的服务信息实现。

2.1、Admin Server注册到Eureka注册中心

pom.xml 中添加Eureka Client依赖 ,parent 中 spring-boot 版本使用: 2.3.5.RELEASE

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

在这里插入图片描述

启动类中添加注解:@EnableEurekaClient

//开启 Admin 的 Server
@EnableAdminServer
@SpringBootApplication
@EnableEurekaClient
public class AdminServerApplication {

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

}

application.yml 修改配置

# 应用程序名称
spring:
  application:
    name: admin-server
# 应用程序端口
server:
  port: 8080

#  默认情况下,大多数端点都不通过http公开,我们公开了所有端点。
#  对于生产,您应该仔细选择要公开的端点。

management:
  endpoints:
    web:
      exposure:
        include: ["*"]
    health:
      show-details: always

# eureka客户端配置
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
  instance:
    # iP方式显示
    ip-address: true
    #注册中心显示的实例ID,可以用ip地址加端口来区别
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    # 检查健康的actuator端点地址
    health-check-url-path: /actuator/health
    # admin 中已ip显示
    hostname: ${spring.cloud.client.ip-address}

2.2、Admin Client注册到Eureka注册中心

pom.xml 中添加Eureka Client依赖 ,parent 中 spring-boot 版本使用: 2.3.5.RELEASE

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

启动类中添加注解:@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class AdminClientApplication {

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

}

application.yml 修改配置

# 应用程序名称
spring:
  application:
    name: admin-client

# 应用程序端口
server:
  port: 9090


# 通过注册中心发现,就不需要自己注册了
#  boot:
#    admin:
#      client:
#        # admin 的server地址
#        url: http://localhost:8080
#        instance:
#          # 使用IP的方式
#          prefer-ip: true


#  默认情况下,大多数端点都不通过http公开,我们公开了所有端点。
#  对于生产,您应该仔细选择要公开的端点。

management:
  endpoints:
    web:
      exposure:
        include: ["*"]
  endpoint:
    health:
      show-details: always
# 通过注册中心发现,就不需要自己注册了
# eureka客户端配置
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
  instance:
    # iP方式显示
    ip-address: true
    #注册中心显示的实例ID,可以用ip地址加端口来区别
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    # 检查健康的actuator端点地址
    health-check-url-path: /actuator/health
    # admin 中已ip显示
    hostname: ${spring.cloud.client.ip-address}	

三、验证:

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值