海阔淘宝客助手wordpress演示站 | 紫色清新商城模板,网页设计论文总结怎么写,网站建设新闻动态,广州骏域网站建设专家 V写该篇文章的目的是为了以后搭建微服务的时候避免踩坑
要求#xff1a;搭建一个eureka-server注册中心#xff0c;再构建两个eureka-client注册上去#xff0c;然后再搭建admin服务注册到注册中心。实现在admin后管页面可观察已注册上去的服务
前提#xff1a;使用的spri…写该篇文章的目的是为了以后搭建微服务的时候避免踩坑
要求搭建一个eureka-server注册中心再构建两个eureka-client注册上去然后再搭建admin服务注册到注册中心。实现在admin后管页面可观察已注册上去的服务
前提使用的springboot版本为 2.2.2.RELEASEcloud版本为: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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.2.2.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.zyy/groupIdartifactIdspring_boot_demo/artifactIdversion0.0.1-SNAPSHOT/versionnamespring_boot_demo/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionHoxton.SR6/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2. 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*1000eureka 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文件(主要依赖) dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency!-- 服务被spring admin监控需要下面依赖 --dependencygroupIdde.codecentric/groupIdartifactIdspring-boot-admin-starter-client/artifactIdversion2.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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.2.2.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.zyy/groupIdartifactIdmyadmin/artifactIdversion0.0.1-SNAPSHOT/versionnamemyadmin/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- security作为登录admin需要的账户密码 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency!-- 暴露端口用于检查服务 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!-- 标明是spring admin 服务 --dependencygroupIdde.codecentric/groupIdartifactIdspring-boot-admin-starter-server/artifactIdversion2.2.2/version/dependency!-- 注册到eureka上面 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionHoxton.SR6/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2. 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服务端监控。 dependencygroupIdde.codecentric/groupIdartifactIdspring-boot-admin-starter-client/artifactIdversion2.2.2/version
/dependency 访问 http://localhost:8001 访问 http://localhost:8004/
输入账号密码 admin 五、其他问题
本次是简单地搭建了eureka注册中心 和 admin服务监控 没有涉及到业务逻辑也比较简单。若按照教程出现问题请确认maven依赖是否正确版本号。