spring-boot-admin(2.5.4)使用

Spring Boot Admin是一個管理和監(jiān)視Spring Boot應用程序的項目撒穷。應用程序通過Spring Boot Admin客戶端(通過HTTP)注冊,或者使用Spring Cloud服務發(fā)現(xiàn)注冊禽笑。

1、spring-boot-admin-server服務

server端只要加入spring-boot-admin-starter-server依賴即可僚稿,默認的情況下是不需要用戶名及密碼即可登入查看服務詳情蚀同,所以可以加入spring-boot-starter-security增加安全策略蠢络。

  • 關鍵依賴如下
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>${project.parent.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • 代碼及配置如下

SpringBootAdminApplication.java

@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminApplication {

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

}

SecurityConfig.java

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecurityConfig(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()
                //1.配置所有靜態(tài)資源和登錄頁可以公開訪問
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                //2.配置登錄和登出路徑
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3.開啟http basic支持刹孔,admin-client注冊時需要使用
                .httpBasic().and()
                .csrf()
                //4.開啟基于cookie的csrf保護
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //5.忽略這些路徑的csrf保護以便admin-client注冊
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }

}

application.yml

server:
  port: 8081

spring:
  security:
    user:
      name: root
      password: root123
  • 界面演示

使用用戶名root密碼root123登錄

image.png
image.png
2冕杠、spring-boot-admin-client客戶端服務
  • 關鍵依賴
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
  • 代碼及配置如下,使用一個schedulecache演示每秒輸出日志并獲取cache數(shù)據(jù)

SpringBootAdminClientApplication .java

@EnableCaching
@EnableScheduling
@SpringBootApplication
public class SpringBootAdminClientApplication {

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

}

application.yml兢交,注意需要配置logging.file.name才可在日志中查看到實時日志

server:
  port: 8082

spring:
  application:
    name: spring-boot-admin-client
  boot:
    admin:
      client:
        url: http://localhost:8081
        username: root
        password: root123

logging:
  level:
    root: info
  file:
    name: logs/client.log
management:
  endpoints:
    web:
      exposure:
        include: "*"

CacheService.java

@Slf4j
@Service
@CacheConfig(cacheNames = "cacheService")
public class CacheService {

    @Cacheable(key = "#name")
    public String  get(String name) {
        log.info("{}", name);
        return "name:" + name;
    }

}

AdminSchedule.java 模擬每秒獲取緩存數(shù)據(jù),可以spring-boot-admin控制臺中清除此緩存

@Slf4j
@Component
public class AdminSchedule {

    @Autowired
    private CacheService cacheService;

    @Scheduled(cron = "0/1 * * * * ?")
    public void process() {
        cacheService.get("name" + System.currentTimeMillis() / 10000);
        log.info("schedule success");
    }

}
  • 查看spring-boot-admin-client日志
image.png
  • 查看spring-boot-admin-client緩存定義凳干,可清除緩存
image.png
  • 查看基本詳情
image.png
3涧团、使用spring-cloud注冊中心注冊

如果微服務已經(jīng)使用eureka泌绣、consul、zookeeper等注冊中心阿迈,可更加方便集成苗沧,如eureka,增加以下依賴即可

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

示例代碼:https://gitee.com/viturefree/spring-boot-admin.git

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市飒焦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌牺荠,老刑警劉巖休雌,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杈曲,死亡現(xiàn)場離奇詭異,居然都是意外死亡恰响,警方通過查閱死者的電腦和手機胚宦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門枢劝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卜壕,“玉大人,你說我怎么就攤上這事鹤盒∽虻浚” “怎么了跃洛?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵汇竭,是天一觀的道長穴张。 經(jīng)常有香客問我两曼,道長,這世上最難降的妖魔是什么偿枕? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任渐夸,我火速辦了婚禮墓塌,結果婚禮上苫幢,老公的妹妹穿的比我還像新娘。我一直安慰自己韩肝,他們只是感情好伞梯,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著漾峡,像睡著了一般生逸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上烙无,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天截酷,我揣著相機與錄音迂苛,去河邊找鬼。 笑死就漾,一個胖子當著我的面吹牛抑堡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播夷野,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼悯搔,長吁一口氣:“原來是場噩夢啊……” “哼妒貌!你這毒婦竟也來了灌曙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤在刺,失蹤者是張志新(化名)和其女友劉穎蚣驼,沒想到半個月后相艇,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坛芽,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡咙轩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年丐膝,在試婚紗的時候發(fā)現(xiàn)自己被綠了尤误。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡软棺,死狀恐怖喘落,靈堂內(nèi)的尸體忽然破棺而出瘦棋,到底是詐尸還是另有隱情赌朋,我是刑警寧澤篇裁,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布团甲,位于F島的核電站黍聂,受9級特大地震影響躺苦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜产还,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一匹厘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧雕沉,春花似錦集乔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尤溜。三九已至倔叼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宫莱,已是汗流浹背丈攒。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留际插,地道東北人。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親允悦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容