1. 程式人生 > >SpringCloud環境搭建---Springboot-Admin

SpringCloud環境搭建---Springboot-Admin

springboot- AdminServer

搭建過程

    1. 修改pom.xml
<dependencies>
        <!--Spring admin-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <!--配置中心客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--新增spring-boot-starter-security後,如果沒有一下依賴,會報錯 :-->
        <!--Error:(17, 8) java: 無法訪問javax.servlet.Filter 找不到javax.servlet.Filter的類檔案-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • 2.新建bootstrap.yml
spring:
  application:
    name: doraemon-monitor
  cloud:
    config:
      enabled: true
      discovery:
        enabled: true
        service-id: doraemon-configServer
  security:
    user:
      name: moyang
      password: moyang1
  profiles:
    active: secure
server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://${eureka_service_url:eureka-peer1:8761}/eureka/
    registry-fetch-interval-seconds: 10
  instance:
    metadata-map:
      user.name: moyang
      user.password: moyang1
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
  • 3.修改application.yml
turbine:
  aggregator:
    cluster-config: default # 指定聚合哪些叢集,多個使用","分割,預設為 default
  app-config: doraemon-zuul,doraemon-example-service,doraemon-configServer
  clusterNameExpression: new String("dor-")
spring:
  boot:
    admin:
      monitor:
        period: 1000ms # 監控重新整理時間
      discovery:
        enabled: true # 開啟自動發現服務
  • 4.修改啟動類

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

啟動後,檢視eureka頁面:
在這裡插入圖片描述

訪問:127.0.0.1:8764,可以看到
bootAdmin監控

在這裡插入圖片描述

點選對應的例項可以看到詳細資訊:
在這裡插入圖片描述

新增安全控制

  • 1.引入security依賴:
 <!--安全驗證-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
  • 2.新建一個安全控制的配置類:
package com.moyang.doraemon.monitor.configurations;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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;

/**
 * doraemon -- 監控新增許可權驗證
 *
 * @author 墨陽
 * @date 2018-10-21
 */
@Configuration
@Profile("secure") // secure:安全
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;


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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        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().disable();
    }
}

3.新建一個允許所有的配置類:

package com.moyang.doraemon.monitor.configurations;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


/**
 * doraemon -- 允許所有的人訪問。
 *
 * @author 墨陽
 * @date 2018-10-21
 */
@Configuration
@Profile("insecure") //insecure:不安全。
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()//
                .and().csrf().disable();
    }
}

重新啟動,這時候,重新訪問Admin頁面,就需要填寫用於使用者名稱和密碼了。
在這裡插入圖片描述

docker整合

修改pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!--指定映象的名稱-->
                    <imageName>${project.artifactId}:${project.version}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <forceTags>true</forceTags>
                    <!--那些需要和Dockerfile放在一起,在構建映象時使用的檔案,一般應用 jar 包需要納入-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!--用於指定需要複製的根目錄,${project.build.directory}表示target目錄-->
                            <directory>${project.build.directory}</directory>
                            <!--用於指定需要複製的檔案,${project.build.finalName}.jar指的是打包後的jar包檔案。-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

新建Dockerfile(src/main/docker):

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD dor-monitor-0.0.1-SNAPSHOT.jar app.jar
RUN chmod 755 /app.jar
EXPOSE 8764
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

使用mvn clean package docker:build打包