`
wx1568746809
  • 浏览: 21561 次
文章分类
社区版块
存档分类
最新评论

Spring-boot-admin 2.x版本配置与使用

 
阅读更多

前言

利用SpringBoot作为微服务单元的实例化技术选型时,我们不可避免的要面对的一个问题就是如何实时监控应用的运行状况数据,比如:健康度、运行指标、日志信息、线程状况等等。spring-boot-admin(SBA)就是一个很好的服务健康监测工具,使用的spring-boot-admin 2.x版本的服务治理开源框架,可用于项目运行过程中对服务的健康检查,状态监控,及在线日志级别修改等功能;

由于现在公司的项目基本都是spring-boot1.5x版本或者更低,然而SBA的1.5.x版本的UI实在是有点丑,尤其是在尝试了2.x的版本过后,所以,这里介绍的是spring-boot2.0以下版本的交给SBA 2.x版本管理的流程

在线演示实例:点我查看演示 账号:admin 密码:123456

下面是一些启动信息:

登陆

登陆

首页

启动首页

应用

在运行项目

应用详情

应用详情

日志查看

日志

下面将介绍如何使用spring-boot-admin管理服务

1、需要搭建SBA服务端,类似一个注册中心,仅需要进行配置而无需硬编码 2、客户端配置,同样也是添加配置,无需硬编码

SBA服务端搭建:

pom依赖

<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>
  <groupId>com.alan</groupId>
  <artifactId>cloud</artifactId>
  <version>0.0.11</version>
  
  
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Cairo-SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
    	<!-- 若解开此依赖,则需在配置文件中配置eureka地址,SBA将会从eureka中自动读取服务信息 -->
        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency> -->

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.3</version>
        </dependency>
        
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
       <plugins>
		  	<!--解决SpringBoot打包成jar后运行提示没有主清单属性-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
			
		</plugins>
    </build>
</project>

application.yml配置

server:
  port: 8231
spring:
  application:
    name: SpringBootAdmin
  security:
    user:
      name: "admin" #登陆用户名
      password: "123456"  #登陆密码
  boot:
    admin:
      ui:
        title: adminTest  #管理页面的title

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
#eureka:
#  client:
#    service-url: #服务注册中心的配置内容,指定服务注册中心的位置
#      defaultZone: http://admin:123456@127.0.0.1:8761/eureka/

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;

@EnableAdminServer
//@EnableEurekaClient
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }
    
    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;

        public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");

            http.authorizeRequests()
                    .antMatchers(adminContextPath + "/assets/**").permitAll()
                    .antMatchers(adminContextPath + "/login").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                    .logout().logoutUrl(adminContextPath + "/logout").and()
                    .httpBasic().and()
                    .csrf()
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                    .ignoringAntMatchers(
                            adminContextPath + "/instances",
                            adminContextPath + "/actuator/**"
                    );
        }
    }

}

到这SBA服务端就搭建好了,直接启动运行就ok

客户端:

客户端配置:

Maven的pom文件中需要添加如下依赖:

	<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>1.5.6</version>
        </dependency>

在配置文件(application.yml/application.properties/bootstrap.yml/bootstrap.properties)中添加如下配置: 
*.yml文件的需要添加如下配置:
# spring-boot-admin 监控配置 start
  # spring-boot-admin 监控配置 start
  boot: # 注意,这是spring节点下的
    admin: 
      auto-registration: true
      url : http://localhost:8231  #运行的admin服务端ip和端口
      api-path: instances
      username: admin # 1.5x的做法
      password: 123456  # 1.5x的做法
      #spring.boot.admin.client.username=admin
      #spring.boot.admin.client.password=123456
management: 
  security: 
    enabled : false
# spring-boot-admin 监控配置 end
#可在线查看日志
endpoints: 
  logfile: 
    enabled: true
  shutdown: 
    enabled: true
logging: 
  file: log.log  #输出日志文件地址
#保护客户端端点数据
security: 
  user: 
    name: admin
    password: 123456

客户端启动入口:

@SpringBootApplication
public class FileServiceApp {
    public static void main(String[] args) {
        SpringApplication.run(FileServiceApp.class, args);
    }
}

到这SBA客户端就搭建好了,直接启动运行就ok;

至此,spring-boot 2.0版本以下向 SBA 2.x注册的所有步骤已完成,感谢查看,如有错误之处欢迎指正与交流

转载于:https://my.oschina.net/73114dh/blog/2991490

分享到:
评论

相关推荐

    spring-boot-admin-client-2.6.2-API文档-中文版.zip

    赠送jar包:spring-boot-admin-client-2.6.2.jar; 赠送原API文档:spring-boot-admin-client-2.6.2-javadoc.jar; 赠送源代码:spring-boot-admin-client-2.6.2-sources.jar; 赠送Maven依赖信息文件:spring-boot-...

    spring-boot-admin-server-ui-2.5.2-API文档-中文版 (1).zip

    赠送jar包:spring-boot-admin-server-ui-2.5.2.jar; 赠送原API文档:spring-boot-admin-server-ui-2.5.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-ui-2.5.2-sources.jar; 赠送Maven依赖信息文件:...

    spring-boot-admin-server-2.5.2-API文档-中文版.zip

    赠送jar包:spring-boot-admin-server-2.5.2.jar; 赠送原API文档:spring-boot-admin-server-2.5.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-2.5.2-sources.jar; 赠送Maven依赖信息文件:spring-boot-...

    spring-boot-admin-server-cloud-2.6.2-API文档-中文版.zip

    赠送jar包:spring-boot-admin-server-cloud-2.6.2.jar 赠送原API文档:spring-boot-admin-server-cloud-2.6.2-javadoc.jar 赠送源代码:spring-boot-admin-server-cloud-2.6.2-sources.jar 包含翻译后的API文档...

    spring-boot-admin-server-2.6.2-API文档-中文版.zip

    赠送jar包:spring-boot-admin-server-2.6.2.jar; 赠送原API文档:spring-boot-admin-server-2.6.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-2.6.2-sources.jar; 赠送Maven依赖信息文件:spring-boot-...

    spring-boot-admin-server-ui-2.6.2-API文档-中文版.zip

    赠送jar包:spring-boot-admin-server-ui-2.6.2.jar 赠送原API文档:spring-boot-admin-server-ui-2.6.2-javadoc.jar 赠送源代码:spring-boot-admin-server-ui-2.6.2-sources.jar 包含翻译后的API文档:spring-...

    spring-boot-admin-server-cloud-2.5.2-API文档-中文版.zip

    赠送jar包:spring-boot-admin-server-cloud-2.5.2.jar; 赠送原API文档:spring-boot-admin-server-cloud-2.5.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-cloud-2.5.2-sources.jar; 赠送Maven依赖信息...

    spring-boot-admin-client-2.6.2-API文档-中英对照版.zip

    赠送jar包:spring-boot-admin-client-2.6.2.jar; 赠送原API文档:spring-boot-admin-client-2.6.2-javadoc.jar; 赠送源代码:spring-boot-admin-client-2.6.2-sources.jar; 赠送Maven依赖信息文件:spring-boot-...

    spring-boot 2.7.10 jar包

    spring-boot 2.7.10 jar包

    spring-boot-admin-server-ui-2.6.2-API文档-中英对照版.zip

    赠送jar包:spring-boot-admin-server-ui-2.6.2.jar; 赠送原API文档:spring-boot-admin-server-ui-2.6.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-ui-2.6.2-sources.jar; 赠送Maven依赖信息文件:...

    spring-boot-admin-server-2.6.2-API文档-中英对照版.zip

    赠送jar包:spring-boot-admin-server-2.6.2.jar; 赠送原API文档:spring-boot-admin-server-2.6.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-2.6.2-sources.jar; 赠送Maven依赖信息文件:spring-boot-...

    spring-boot-admin-server-cloud-2.6.2-API文档-中英对照版.zip

    赠送jar包:spring-boot-admin-server-cloud-2.6.2.jar; 赠送原API文档:spring-boot-admin-server-cloud-2.6.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-cloud-2.6.2-sources.jar; 赠送Maven依赖信息...

    spring-webmvc-5.0.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....

    springboot2.0.x+dubbo-spring-boot-starter

    而 Dubbo 2.6.x 版本则包含了对 Spring Boot 的官方支持,提升了服务治理、监控和配置的便利性。 在 "SpringBootWithDubbo-master" 这个压缩包文件中,我们可以期待找到一个完整的 Spring Boot 与 Dubbo 集成的示例...

    spring-boot-reference.pdf

    20.5.2. Remote Update 21. Packaging Your Application for Production 22. What to Read Next IV. Spring Boot features 23. SpringApplication 23.1. Startup Failure 23.2. Customizing the Banner 23.3. ...

    spring-boot-activemq-demo.zip

    **Spring Boot与ActiveMQ集成** 1. **添加依赖**: 首先,你需要在Spring Boot项目的`pom.xml`文件中添加ActiveMQ的依赖。这通常通过Maven或Gradle的仓库来完成,确保你引用了Spring Boot对JMS和ActiveMQ的支持。 2...

    最新Spring Boot Admin 官方参考指南-中文版-2.x

    这可以通过引入`spring-boot-admin-starter-client`依赖来实现,或者如果你正在使用Spring Cloud Discovery,你的应用程序会自动被Spring Boot Admin Server发现。此外,SBA还支持静态配置选项,以便手动管理应用...

    spring-batch-admin-1.3.0.RELEASE

    - 使用版本控制:对Spring Batch Admin的配置文件进行版本控制,便于回滚和协同开发。 - 监控报警:结合日志系统和监控工具,设置报警规则,及时发现异常情况。 7. **扩展性与社区支持** - Spring Batch Admin ...

    参照阿里druid整理druid-spring-boot-starter的demo

    这个"参照阿里druid个人整理druid-spring-boot-starter可运行demo"是基于Spring Boot 2.x版本的,包含了Druid的基本配置和使用。为了构建一个完整的Druid配置,你需要以下几个步骤: 1. **依赖添加**:在`pom.xml`...

    spring-boot-admin-server-2.1.6.jar

    java运行依赖jar包

Global site tag (gtag.js) - Google Analytics