写该篇文章的目的是为了以后搭建微服务的时候避免踩坑
要求:搭建一个eureka-server注册中心,再构建两个eureka-client注册上去,然后再搭建admin服务注册到注册中心。实现在admin后管页面可观察已注册上去的服务
前提:使用的springboot版本为 2.2.2.RELEASE;cloud版本为:Hoxton.SR6、springboot-admin版本为2.2.2
项目已上传云盘,获取地址:
链接:https://pan.baidu.com/s/10icCd2VvQuD0dbuWUa8bUw
提取码:asdf
注意点有三个,分别是
1.pom导入依赖(下面是完整的pom文件) : springboot、eureka-server、spring cloud
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.zyy
spring_boot_demo
0.0.1-SNAPSHOT
spring_boot_demo
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR6
pom
import
org.springframework.boot
spring-boot-maven-plugin
2. application.yml文件:
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
eviction-interval-timer-in-ms: 5000 #清理间隔(单位毫秒, 默认是60*1000)eureka server提出服务不可用的时间窗
enable-self-preservation: false #默认为true,设置为false,关闭自我保护
#eureka server: 在运行期间会去统计心跳失败比例在15分钟之内是否低于85%
renewal-percent-threshold: 0.85 #默认0.85 指定自我保护机制的开启阈值
3. 启动类:添加@EnableEurekaServer注解
package com.zyy.spring_boot_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
1.pom文件(主要依赖):
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
de.codecentric
spring-boot-admin-starter-client
2.2.2
2.application.yml:
spring:
application:
name: zyy-client-one
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka
# 下面的配置作为spring admin使用
management:
# 开启所有监控终端
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
3. 启动类: 添加@EnableEurekaClient注解表示为eureka客户端
package com.zyy.myeurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class MyeurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(MyeurekaclientApplication.class, args);
}
}
1.pom文件(完整的):导入依赖为springboot,spring-security,spring-actuator,eureka-client,springcloud
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.zyy
myadmin
0.0.1-SNAPSHOT
myadmin
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-actuator
de.codecentric
spring-boot-admin-starter-server
2.2.2
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR6
pom
import
org.springframework.boot
spring-boot-maven-plugin
2. application.yml
spring:
# 设置登录admin的账户密码 admin后台地址为 http://localhost:8004
security:
user:
name: admin
password: admin
application:
name: zyy-client-adminserver
server:
port: 8004
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka
registry-fetch-interval-seconds: 10
management:
# 开启所有监控终端
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
3. 启动类: 添加@EnableAdminServer 注解 以及 @EnableEurekaClient注解
package com.zyy.myadmin;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class MyadminApplication {
public static void main(String[] args) {
SpringApplication.run(MyadminApplication.class, args);
}
}
启动euereka-server后,再启动其他服务,spring-admin也是作为eureka客户端注册在eureka注册中心。那admin后台能监控到注册中心上面的其他服务的原因是:
1. 服务都注册在注册中心上,admin可以获取注册中心上面的服务
2. eureka-client服务的pom都导入了下面的依赖,其被标注为一个admin 客户端,服务启动后能够被admin服务端监控。
de.codecentric spring-boot-admin-starter-client 2.2.2
访问 http://localhost:8001
访问 http://localhost:8004/
输入账号密码 admin
本次是简单地搭建了eureka注册中心 和 admin服务监控 ,没有涉及到业务逻辑,也比较简单。若按照教程出现问题,请确认maven依赖是否正确,版本号。