springboot2.x搭建springbootadmin2.x

忧臣解读      2022-04-15     448

关键词:

1 说明

  1. 全部配置基于1.8.0_111
  2. 当前SpringBoot使用2.0.5
  3. SpringBootAdmin基于Eureka进行Client发现,Eureka搭建参见SpringBoot2.x搭建Eureka
  4. SpringBootAdmin项目文档参见SpringBootAdmin参考文档

2 创建项目

SpringBoot项目生成器中,输入GroupArtifact,如下配置:

3 编辑pom.xml文件

pom.xml中新添加如下内容:

 <properties>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
 </properties>
<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>
<dependencies>
        <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-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
</dependencies>

4 修改application.properties为application.yml文件

编辑application.properties为application.yml文件,添加以下内容:

server:
  port: 8099
spring:
  application:
    name: SpringBootAdmin
  security:
    user:
      name: anxminise
      password: 123456
  boot:
    admin:
      ui:
        title: 忧臣解读

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
eureka:
  instance:
    metadata-map:
      user.name: anxminise
      user.password: 123456
    easeRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    ip-address: 127.0.0.1
    prefer-ip-address: true
    instance-id: ${eureka.instance.ip-address}:${server.port}
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://anxminise:123456@127.0.0.1:8888/eureka/

配置参数解释:

参数 说明
security.user.name SpringBootAdmin登录时的用户名
security.user.password SpringBootAdmin登录时的密码
eureka.instance.metadata-map.user.name SpringBootAdmin本身作为一个Eureka客户端被发现,这里由于SpringBootAdmin需要进行登录,因此,此处配置SpringBootAdmin登录时使用的用户名
eureka.instance.metadata-map.user.password 同上,配置SpringBootAdmin登录使用的密码

5 编辑SpbadminApplication.java文件

最后编辑SpbadminApplication.java文件,修改为:

@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class SpbadminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpbadminApplication.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/**"
                    );
        }
    }
}

6 编译并启动运行

注:本次SpringBootAdmin是基于Eureka搭建的,需要先启动Eureka
./mvnw clean package -DskipTests #编辑应用
java -jar target/spbadmin-0.0.1-SNAPSHOT.jar #启动应用,注:jar包的名称应换成自己的



7 Eureka客户端监控

SpringBootAdmin的作用是:监控Eureka中的微服务,这里的微服务,我们使用SpringBoot2.x进行开发,对于SpringBoot2.x的SpringBoot项目,只需在application.yml中新加入以下内容:

logging:
  path: logs/log
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

参数解释说明

参数 说明
logging.path 配置日志的存放路径,此处用于SpringBootAdmin的日志监控
management.* 配置SpringBoot的全部监控点,此处注意:当前配置未对endpoint进行访问保护,勿对外网开放,后续我们加入对其的访问保护

springboot2.x版本整合redis集群

参考技术A启动Redis集群搭建方式SpringBoot1.x版本默认使用jedis连接,2.x版本使用lettuce连接ymlyml测试 查看详情

springboot2.x入门——helloworld

Springboot2.x入门——helloWorld一、简介1.1Springboot简介SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化... 查看详情

springboot2.x:入门篇(代码片段)

什么是SpringBootSpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架遵循”约定优于配置“的思想,清除了原先使用Spring框架的那些样板化的配置,继承了原有Spring框架的优... 查看详情

网红框架springboot2.x之花式运行项目

成功搭建SpringBoot开发环境后,我们回顾一下,SpringBoot项目有哪几种启动方式:1、最傻瓜式的IDE方式启动如果你用的是IDEA的话,做一下运行配置,选择下入口类,直接就可以启动,如果你用的是Myeclipse那就更简单了,直接项目... 查看详情

springboot2.x最佳实践《一》之springboot2.x初体验

SpringBoot2.X最佳实践前言本系列文章,从零基础接触 SpringBoot2.x新版本,基础入门使用,热部署,到整合各个主流框架Redis4.x,消息队列AciveMQ,RocketMQ等,搜索框架ElasticSearch5.6版本,到web-flux反应式编程,到Actuator监控应用信息... 查看详情

《深入浅出springboot2.x》发个目录和定价信息给大家瞧瞧

《深入浅出SpringBoot2.x》预计月底出版销售。本来是500页的著作,原本编辑建议109元,主要是为了降低读者购书成本考虑,经过我多方的协调,人邮编辑经过努力减小字间距和压缩篇幅大小,再次表示感谢,... 查看详情

springboot2.x集成knife4j(代码片段)

...。如何使用knife4j?简略的说一下,基础环境搭建可参考:SpringBoot2.x集成Swagger2这里我说一下主要配置区别:环境说明:新增knife4j.version:2.0.21.导入pom依赖<dependency& 查看详情

springboot2.x

 SpringBoot2.x https://blog.csdn.net/GitChat/article/details/78454120 API文档可以使用Swagger2或者APIDOC来实现https://www.cnblogs.com/minsons/articles/7154090.html 查看详情

(汇总)springboot实践折腾记&springboot2.x实践记

SpringBoot2.x实践记SpringBoot2.x实践记:Retry(annotion)SpringBoot2.x实践记:GsonSpringBoot2.x实践记:MailSpringBoot2.x实践记:H2DatabaseSpringBoot实践折腾记SpringBoot实践折腾记(1&# 查看详情

《springboot免费教程》连载目录

...Star关注支持一下,随时获得更新信息!快速入门SpringBoot2.x基础教程:版本关系SpringBoot2.x基础教程:快速入门SpringBoot2.x基础教程:工程结构推荐配置详解SpringBoot2.x基础教程:配置文件详解SpringBoot2.x基础教... 查看详情

《springboot免费教程》连载目录

...Star关注支持一下,随时获得更新信息!快速入门SpringBoot2.x基础教程:版本关系SpringBoot2.x基础教程:快速入门SpringBoot2.x基础教程:工程结构推荐配置详解SpringBoot2.x基础教程:配置文件详解SpringBoot2.x基础教... 查看详情

2018最新springboot2.0教程(零基础入门)

一、零基础快速入门SpringBoot2.01、SpringBoot2.x课程全套介绍和高手系列知识点简介:介绍SpringBoot2.x课程大纲章节java基础,jdk环境,maven基础2、SpringBoot2.x依赖环境和版本新特性说明简介:讲解新版本依赖环境和springboot2新特性概述3... 查看详情

springboot2.x集成单节点redis

Springboot2.x集成单节点Redis说明在Springboot1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot2.x版本中,默认使用Lettuce客户端来操作Redis。Springboot提供了RedisTemplate来统一封装了对Redis操作,开发者只需要使用RedisTemplate就可以... 查看详情

springboot2.x配置文件讲解

SpringBoot2.x配置文件讲解简介:SpringBoot2.x常见的配置文件xml、yml、properties的区别和使用xml、properties、json、yaml1、常见的配置文件xx.yml,xx.properties,1)YAML(YetAnotherMarkupLanguage)写YAML要比写XML快得多(无需关注标签或引号)使用空格Spa... 查看详情

springboot2.x教程-thymeleaf原理是什么

layout:posttitle:SpringBoot2.x教程-Thymeleaf原理是什么categories:SpringBootdescription:SpringBoot2.x教程-Thymeleaf原理是什么keywords:SpringBoot,Spring,Thymeleaf---如要要理清楚Thymeleaf的原理,那么就要从模板引擎的原理说起。Thymeleaf只不过是众多模板 查看详情

springboot2.x-springboot整合amqp之rabbitmq

文章目录SpringBoot2.X-SpringBoot整合AMQP之RabbitMQRabbitMQ简介引入依赖编写配置编写接口启用Rabbit注解消息监听消息测试SpringBoot2.X-SpringBoot整合AMQP之RabbitMQSpringBoot2整合RabbitMQ案例。RabbitMQ简介简介RabbitMQ是一个由erlang开发的AMQP(AdvanvedMess... 查看详情

springboot2.x:springboot

 几个重要的事件回调机制: 1、配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunlistener 2、需要放在ioc容器中     3、ApplicationRunner &nbs 查看详情

springboot2.x文件上传实战

设置上传的文件大小:@ConfigurationpublicclassfileConfigure{@BeanpublicMultipartConfigElementmultipartConfigElement(){MultipartConfigFactoryfactory=newMultipartConfigFactory();//单个文件的大小factory.setMaxFileSize("20 查看详情