摘要:本文主要讲解如何将Spring Boot Admin
与Spring Cloud Alibaba中的spring-cloud-starter-alibaba-nacos-discovery
进行整合使用。
通过搭建一个Spring Boot Admin Server,并将其通过spring-cloud-starter-alibaba-nacos-discovery注册到Nacos Server中即可。
源码工程地址为:https://github.com/SoftwareKing/sba-nacos
说明本文使用的Spring Boot Admin的版本为2.1.2,Spring Cloud的版本为Greenwich.RELEASE,Spring Cloud Alibaba的版本为0.2.1.RELEASE。至于其它版本,请读者自行适配处理。
1.创建sba-nacos工程,添加maven依赖如下所示:
1
2
5
6 4.0.0modelVersion>
7
8 org.springframework.bootgroupId>
9 spring-boot-starter-parentartifactId>
10 2.1.2.RELEASEversion>
11 parent>
12
13 org.xujin.mossgroupId>
14 sba-nacosartifactId>
15
16
17 2.1.2spring.boot.admin.version>
18 properties>
19
20
21
22
23 org.springframework.cloudgroupId>
24 spring-cloud-dependenciesartifactId>
25 Greenwich.RELEASEversion>
26 pomtype>
27 importscope>
28 dependency>
29
30 org.springframework.cloudgroupId>
31 spring-cloud-alibaba-dependenciesartifactId>
32 0.2.1.RELEASEversion>
33 pomtype>
34 importscope>
35 dependency>
36 dependencies>
37 dependencyManagement>
38
39
40
41
42 org.springframework.bootgroupId>
43 spring-boot-starter-actuatorartifactId>
44 dependency>
45
46
47 org.springframework.cloudgroupId>
48 spring-cloud-starter-alibaba-nacos-discoveryartifactId>
49 dependency>
50
51
52 de.codecentricgroupId>
53 spring-boot-admin-starter-serverartifactId>
54 ${spring.boot.admin.version}version>
55 dependency>
56
57
58 org.springframework.bootgroupId>
59 spring-boot-starter-webartifactId>
60 dependency>
61
62
63 org.jolokiagroupId>
64 jolokia-coreartifactId>
65 dependency>
66 dependencies>
67
68 sba-nacosfinalName>
69
70
71 org.springframework.bootgroupId>
72 spring-boot-maven-pluginartifactId>
73 plugin>
74 plugins>
75 build>
76project>
创建主入口程序,代码如下所示:
1package org.xujin.moss.nacos;
2
3import de.codecentric.boot.admin.server.config.EnableAdminServer;
4import org.springframework.boot.SpringApplication;
5import org.springframework.boot.autoconfigure.SpringBootApplication;
6import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7
8
9@SpringBootApplication
10@EnableAdminServer
11@EnableDiscoveryClient
12public class MossNacosApplication {
13 public static void main(String[] args) {
14 SpringApplication.run(MossNacosApplication.class, args);
15 }
16}
3.application.yml配置如下所示:
1spring:
2 application:
3 name: sba-nacos
4 cloud:
5 nacos:
6 discovery:
7 server-addr: 127.0.0.1:8848
8
9server:
10 port: 8080 #启动端口
11
12info.groupId: @project.groupId@
13info.artifactId: @project.artifactId@
14info.version: @project.version@
15
16
17#开启端点
18management:
19 endpoints:
20 web:
21 exposure:
22 include: '*'
23 security:
24 enabled: false
25 endpoint:
26 health:
27 show-details: ALWAYS
其中127.0.0.1:8848为Nacos Server的地址。
至于什么是Nacos,这里不做过多介绍。有兴趣者可以访问http://nacos.io,或者访问: http://nacos.net.
说明: http://nacos.net.目前内容为快照版本。
访问https://github.com/alibaba/nacos ,使用Git克隆Nacos代码,直接导入到IDEA中,如下所示设置启动参数,直接启动。
从IDE中启动Nacos是我个人喜欢的方式,因为可以随时Debug Nacos任何代码,其它启动方式请参考官网。
以Debug方式启动,sba-nacos,如下图所示:
启动之后访问nacos控制台,我们可以看到sba-nacos已经注册到Nacos注册中心。如下图所示.
说明访问Nacos控制台,需要输入用户名和密码默认为用户名为nacos,密码:nacos
访问http://localhost:8080/#/instances/92cfba09790d/details,我们可以看到Spring Boot Admin Server已经成功启动,并且通过EndPoint获取到自己的端点信息,如下图所示:
原文链接:http://xujin.org/sc/sba-nacos/