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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/81605.html