高端网站定制北京,中铁建设集团有限公司华北分公司,快速关键词排名首页,沈阳男科医院好吗Spring Boot Admin 环境搭建与基本使用 一、Spring Boot Admin是什么二、提供了那些功能三、 使用Spring Boot Admin3.1搭建Spring Boot Admin服务pom文件yml配置文件启动类启动admin服务效果 3.2 common-apipom文件feignhystrix 3.3服务消费者pom文件yml配置文件启动类control… Spring Boot Admin 环境搭建与基本使用 一、Spring Boot Admin是什么二、提供了那些功能三、 使用Spring Boot Admin3.1搭建Spring Boot Admin服务pom文件yml配置文件启动类启动admin服务效果 3.2 common-apipom文件feignhystrix 3.3服务消费者pom文件yml配置文件启动类controller 3.4服务提供者pom文件yml配置文件项目启动类controller 服务整体启动之后的效果 四、 总结 一、Spring Boot Admin是什么
它是用于监控和管理Spring Boot应用程序的开源工具。它为开发人员或者是运维人员提供了友好的Web界面。可以实时监控和管理部署在不同环境中的Spring Boot应用。
二、提供了那些功能 应用程序监控可以显示程序的基本信息内存使用情况、线程信息。应用程序管理可以管理监控的应用动态配置日志的级别。通知和报警可以配置通知和警报当应用程序出现问题或者叨叨预定的阈值时及时通知相关人员。微服务支持可以适用微服务架构一次性监控和管理多个微服务应用。安全性可以与Spring Security集成实现对监控和管理界面的访问控制。 三、 使用Spring Boot Admin
示例项目整体结构 这里为什么要使用Eureka主要是想体现复用的思想。所有服务都注册到了Eureka之后而Spring Boot Admin只要集成了Eureka之后就能够获取到所有的服务信息注册信息。能够对所有注册到Eureka中的服务进行监控和管理。 Eureka的搭建可以参考这篇博客【Spring Cloud 三】Eureka服务注册与服务发现
3.1搭建Spring Boot Admin服务
pom文件
?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.3.12.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.wangwei/groupIdartifactIdadmin-server-05/artifactIdversion0.0.1-SNAPSHOT/versionname05-admin-server/namedescription05-admin-server/descriptionpropertiesjava.version8/java.versionspring-boot-admin.version2.3.0/spring-boot-admin.versionspring-cloud.versionHoxton.SR12/spring-cloud.version/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdde.codecentric/groupIdartifactIdspring-boot-admin-starter-server/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependencydependencygroupIdde.codecentric/groupIdartifactIdspring-boot-admin-dependencies/artifactIdversion${spring-boot-admin.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project
yml配置文件
##???
server:port: 10086 #端口号 0-65535spring:application:name: admin-servereureka:client:service-url:defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #设置为fasle 不往eureka-server注册默认为truefetch-registry: true #应用是否拉取服务列表到本地registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题时间越短脏读越少 性能相应的消耗回答instance: #实例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主机名称或者服务ipprefer-ip-address: true #以ip的形式显示具体的服务信息lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔management:endpoints:web:exposure:include: * #暴露所有的监控端点 #如果一个服务需要被监控那么就要将自身的一些清苦一些信息接口暴露出去
启动类
SpringBootApplication
EnableEurekaClient
EnableAdminServer //#开启admin服务端
public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}
启动admin服务效果 3.2 common-api
这个模块是抽离出来的提供接口用于两个服务之间的跨服务调用。之后由服务消费者集成。
pom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdfeign-project/artifactIdgroupIdcom.wangwei/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdcommon-api/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdcom.wangwei/groupIdartifactIdproject-domain/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactId/dependency/dependencies/projectfeign
FeignClient(value order-service,fallback UserOrderFeignHystrix.class)
public interface UserOrderFeign {GetMapping(order/getOrderByUserId/{id})Order getOrderByUserId (PathVariable(id)Integer id);}
hystrix
Component
public class UserOrderFeignHystrix implements UserOrderFeign {/*** 一般远程调用的熔断可以直接返回null* param id* return*/Overridepublic Order getOrderByUserId(Integer id) {return null;}
}3.3服务消费者
pom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdfeign-project/artifactIdgroupIdcom.wangwei/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIduser-center/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!--用于在应用程序中添加各种监控和管理功能--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdcom.wangwei/groupIdartifactIdcommon-api/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency/dependencies/projectyml配置文件
server:port: 8081spring:application:name: user-serviceeureka:client:service-url: #??????defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #设置为fasle 不往eureka-server注册fetch-registry: true #应用是否拉取服务列表到本地registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题时间越短脏读越少 性能相应的消耗回答instance: #实例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主机名称或者服务ipprefer-ip-address: true #以ip的形式显示具体的服务信息lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔
feign:hystrix:enabled: true #开启熔断
management:endpoints:web:exposure:include: *
启动类
SpringBootApplication
EnableEurekaClient
EnableFeignClients
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class,args);}
}controller
RestController
public class UserController {Autowiredprivate UserOrderFeign userOrderFeign;GetMapping(findOrder)public Order findOrder(){return userOrderFeign.getOrderByUserId(1);}}3.4服务提供者
服务提供者与服务消费者的主要区别是没有依赖actuator以及对应的暴露端点的配置。所以在admin的Web页面中是不为看到服务提供者的详细信息。 !--用于在应用程序中添加各种监控和管理功能--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencypom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdfeign-project/artifactIdgroupIdcom.wangwei/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdorder-center/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdcom.wangwei/groupIdartifactIdcommon-api/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/projectyml配置文件
server:port: 8080spring:application:name: order-serviceeureka:client:service-url: #??????defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #设置为fasle 不往eureka-server注册fetch-registry: true #应用是否拉取服务列表到本地registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题时间越短脏读越少 性能相应的消耗回答instance: #实例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主机名称或者服务ipprefer-ip-address: true #以ip的形式显示具体的服务信息lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔
项目启动类
SpringBootApplication
EnableEurekaClient
public class OrderServiceApplication {public static void main(String[] args) {SpringApplication.run(OrderServiceApplication.class,args);}
}controller
RestController
public class OrderController {GetMapping(order/getOrderByUserId/{id})Order getOrderByUserId (PathVariable(id)Integer id){System.out.println(id);Order orderOrder.builder().name(青椒肉丝盖饭).price(15D).orderId(1).build();return order;}}服务整体启动之后的效果 由于Eureka服务没有依赖actuator所以不能看到详细信息。
四、 总结
本篇博客主要是对于Spring Boot Admin的基本认识和基本运用通过本篇博客能够对Spring Boot Admin有一个宏观认知和能够快速上手。Spring Boot Admin还可以设置通知可报警本篇博客并没有涉及到。