Nacos整合Admin(admin client存在上下文context-path,以admin为例)

Nacos整合Admin(admin client存在上下文context-path,以admin为例)

一,创建admin服务端

1.1 pom.xml

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.1</version>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.4.4</version>
        </dependency>

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

1.2 bootstrap.yml

bootstrap.yml

server:
  port: 9002
  servlet:
    context-path: /admin
spring:
  application:
    name: tools2-admin
  profiles:
    active: dev

management:
  endpoints:
    web:
      exposure:
        include: "*"

bootstrap-dev.yml

spring:
  cloud:
    nacos:
      server-addr: localhost:9000
      discovery:
        namespace: 8628e5dd-a236-4016-b94f-565a001faf2f
        group: ${spring.profiles.active}

1.3 Application.java

package pers.lmc.tools.admin;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author lmc
 * @Description: TODO
 * @Create 2022-04-07 20:51
 * @version: 1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class Admin2Applicatin {

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

}

启动项目,访问 localhost:9002/admin, 页面正常。

1.4 上下文问题

虽然页面能够访问正常,但是我们发现此时admin本身没有被监控到。

这是因为我给admin server添加了server.servlet.context-path,导致admin server获取本身心跳检测等(默认 http://ip:port/actuator/**)不正确,应在nacos配置中补充metadata.management.context-path

spring:
  cloud:
    nacos:
      server-addr: localhost:9000
      discovery:
        namespace: 8628e5dd-a236-4016-b94f-565a001faf2f
        group: ${spring.profiles.active}
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator

重新启动项目,在 localhost:9002/admin 中可以看到admin server本身也已经被监控到了。

之后,若还有服务本身使用了server.servlet.context-path,再通过metadata.management.context-path使admin生效。

备注:通过eureka方式的话,是要补充以下:

eureka:
  instance:
    metadata-map:
      management:
        context-path: ${server.servlet.context-path}/actuator
    health-check-url: http://${spring.cloud.client.ip-address}:${server.port}${server.servlet.context-path}/actuator/health
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值