1. 程式人生 > >SpringBoot2.x搭建SpringBootAdmin2.x

SpringBoot2.x搭建SpringBootAdmin2.x

創建項目 rep ali lean 添加 進行 always asset 保護

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:[email protected]: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搭建SpringBootAdmin2.x