写该篇文章的目的是为了以后搭建微服务的时候避免踩坑
要求:搭建一个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
一、搭建eureka-server注册中心
注意点有三个,分别是
1.pom导入依赖(下面是完整的pom文件) : springboot、eureka-server、spring cloud
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zyy</groupId><artifactId>spring_boot_demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring_boot_demo</name><description>Demo project for Spring Boot</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR6</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2. application.yml文件:
server:port: 8001
eureka:instance:hostname: localhostclient:register-with-eureka: falsefetch-registry: falseservice-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);}}
二、搭建eureka-client客户端,注册在eureka上面
1.pom文件(主要依赖):
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 服务被spring admin监控需要下面依赖 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.2.2</version></dependency>
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);}}
三、搭建admin-server服务,同时注册在eureka上面
1.pom文件(完整的):导入依赖为springboot,spring-security,spring-actuator,eureka-client,springcloud
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zyy</groupId><artifactId>myadmin</artifactId><version>0.0.1-SNAPSHOT</version><name>myadmin</name><description>Demo project for Spring Boot</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- security作为登录admin需要的账户密码 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- 暴露端口,用于检查服务 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 标明是spring admin 服务 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.2.2</version></dependency><!-- 注册到eureka上面 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR6</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2. application.yml
spring:# 设置登录admin的账户密码 admin后台地址为 http://localhost:8004security:user:name: adminpassword: adminapplication:name: zyy-client-adminserver
server:port: 8004
eureka:client:service-url:defaultZone: http://localhost:8001/eurekaregistry-fetch-interval-seconds: 10management:# 开启所有监控终端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服务端监控。
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.2.2</version> </dependency>
访问 http://localhost:8001
访问 http://localhost:8004/
输入账号密码 admin
五、其他问题
本次是简单地搭建了eureka注册中心 和 admin服务监控 ,没有涉及到业务逻辑,也比较简单。若按照教程出现问题,请确认maven依赖是否正确,版本号。