内容简介:新建一个配置
Spring Boot Admin 是一个 管理 和 监控 Spring Boot 应用程序 的一款开源软件。 Spring Boot Admin 分为 Server 端和 Client 端, Spring Boot Admin UI 部分使用 AngularJS 将数据展示在前端。
正文
1. 项目结构
-
Eureka Server: 服务注册中心 ,端口号为
8761。 -
Admin Server:用于对 微服务系统 进行统一的 监控 和 管理 。
-
Admin Client: 客户端 集成
Admin。
2. 构建Admin Server
新建一个 Spring Boot 的项目模块,取名为 admin-server ,其完整依赖如下:
<?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 http://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>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.github.ostenant.springcloud</groupId>
<artifactId>admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 管理界面与JMX-Beans交互 -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</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>${spring-cloud.version}</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>
复制代码
配置 application.yml ,设置 management.security.enabled=false ,保证 安全验证 关闭,设置 Spring Boot Admin 默认开启的 服务端点 。
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 5000
spring:
application:
name: admin-server
boot:
admin:
routes:
endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
management:
security:
enabled: false
logging:
file: "logs/boot-admin-sample.log"
复制代码
在 src/main/resources 目录下新建一个 logback-spring.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
</configuration>
复制代码
在应用的 启动类 上通过注解 @EnableAdminServer 开启 Admin Server 的功能。
@EnableEurekaClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
复制代码
Admin Server 创建完成,运行 AdminServerApplication 启动应用程序!
2. 构建Admin Client
新建一个 Spring Boot 的项目模块,取名为 admin-client ,其完整依赖如下:
<?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 http://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>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.github.ostenant.springcloud</groupId>
<artifactId>admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</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>${spring-cloud.version}</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>
复制代码
配置 application.yml 文件,设置 日志输出路径 ,并关闭 Actuator 模块的 安全验证 。
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: admin-client
management:
security:
enabled: false
logging:
file: "logs/boot-admin-client.log"
复制代码
在应用的 启动类 上加上 @EnableEurekaClient 注解,开启 EurekaClient 的功能。
@SpringBootApplication
@EnableEurekaClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
复制代码
3. 启动应用程序
依次启动 eureka-server 、 admin-server 和 admin-client 模块,访问 admin-server 的主页 http://localhost:5000 ,浏览器显示界面如图所示:
JOURNAL 选项为 服务注册 、 下线 、 剔除 的 时间线 。
4. 添加安全登录界面
Spring Boot Admin 提供了登录界面的组件,并且和 Spring Boot Security 结合使用,需要 用户登录 才能访问。在 Admin Server 的 pom.xml 文件中引入以下依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码
在 admin-server 模块的 application.yml 中完成如下配置,创建一个 security 的 user 用户,它的用户名为 admin ,密码为 123456 。通过 eureka.instance.metadate-map 配置属性 带上该 security 的 user 用户信息。
security:
user:
name: admin
password: 123456
eureka:
instance:
metadata-map:
user.name: admin
user.password: 123456
复制代码
然后在 应用程序 中配置 Spring Boot Security ,创建一个 SecurityConfig 的 配置类 ,给 静态资源 加上 permitAll() 权限,其他的 资源访问 则需要 权限认证 ,另外这些资源不支持 CSFR ( 跨站请求伪造 ),所以禁用掉 CSFR ,最后需要开启 HTTP 的基本认证,即 httpBasic() 方法。
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
http.logout().logoutUrl("/logout");
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
http.authorizeRequests().antMatchers("/**").authenticated();
http.httpBasic();
}
}
复制代码
重新启动 admin-server 项目模块,在浏览器中访问 admin-client 的地址 http://localhost:5000 ,在登录界面输入用户名 admin ,密码为 123456 ,登录即可。
以上所述就是小编给大家介绍的《Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 实战 Prometheus 搭建监控系统
- Go 监控的标配:实战 Prometheus
- 大屏监控系统实战(14)-打包上线及总结
- Zabbix分布式监控微信报警实战
- MySQL运维实战 之 价值一个亿的GTID监控
- 宜信开源|案例:UAVStack的慢SQL数据库监控实战
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
自己动手做iOS App
张子怡 / 电子工业出版社 / 2017-8 / 69.00
《自己动手做iOS App:从设计开发到上架App Store》为想要接触iOS 应用设计、开发的读者提供了由浅入深的详细指导。从iOS 应用制作的步骤是什么,应该使用什么软件,如何发布应用到App Store,到iOS 的设计理念是什么,如何正确书写Swift 语言,再到后端和客户端是如何交互运作的等,本书配合图示,精辟、直观地阐明了iOS 应用制作中的种种疑问。 如果你是一位第一次接触i......一起来看看 《自己动手做iOS App》 这本书的介绍吧!