spring cloud 基于注册中心 的spring admin监控,并设置密码 nacos为例,spring boot架构

基于单一应用(spring boot架构):

Admin Server端 第一步. 自动配置类

主启动类添加:@SpringBootApplication ,@EnableAdminServer

POM添加:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--监控服务包 spring-boot-admin服务端-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.3.0</version>
        </dependency>

        <!--需要密码验证包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

SpringBootAdmin 登录鉴权使用同下面的一样

application.yml 配置

server:
  port: 8777

spring:
  application:
    name: spring-boot-admin

  security:
    basic:
      enabled: true
    user:
      name: admin
      password: root

Admin Client端

主启动类添加:@SpringBootApplication 

POM添加:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--spring-boot-admin使用暴露监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- admin-client 客户端-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.6</version>
        </dependency>

application.yml 配置

server:
  port: 8770

spring:
  application:
    name: web-api

  boot:
    admin:
      client:
        url: http://127.0.0.1:8777
        username: admin
        password: root

## 暴露监控端点 spring boot admin 使用的
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

注意:连接的地址和需要密码验证时用户名和密码要相同

基于注册中心(spring cloud微服务架构):

Admin Server端 第一步. 自动配置类

主启动类添加:@SpringCloudApplication ,@EnableAdminServer

spring-boot-admi服务 POM添加:
          <!-- nacos 配置中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- nacos 发现服务 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

      <!--spring-boot-admin使用服务包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--监控服务包-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.3.0</version>
        </dependency>

        <!--需要密码验证包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

 一、SpringBootAdmin 登录鉴权使用

package com.wang.springbootadmin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * SpringBootAdmin 登录鉴权使用
 *
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String contextPath;

    public WebSecurityConfig(AdminServerProperties adminServerProperties) {
        this.contextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(contextPath + "/instances");

        http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
        http.authorizeRequests().antMatchers(contextPath + "/actuator/**").permitAll(); // 自身监控
        http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证

        // 整合spring-boot-admin-server-ui
        http.formLogin().loginPage("/login").permitAll();
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");

        // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
        http.httpBasic();
    }
}

 bootstrap.yml配置

server:
  port: 9091

spring:
  application:
    name: spring-boot-admin

  security:
    basic:
      enabled: true
    user:
      name: admin
      password: root

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos注册地址
        group: ONE_GROUP  #注册组 (服务组管理,不强制)
      config:
        server-addr: localhost:8848 #nacos注册地址
        file-extension: yaml #配置文件后缀支持 txt json yam xml html Properties 
        group: ONE_GROUP # 分组 (配置组管理,不强制)
        prefix: nocas_pubilc


## 暴露监控端点 spring boot admin 使用的
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

Admin Client端 第二步. 其它服务

POM添加:

     <!-- nacos 配置中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- nacos 发现服务 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

      <!--spring-boot-admin使用服务包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

bootstrap.yml配置

server:
  port: 9091

spring:
  application:
    name: spring-boot
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos注册地址
        group: ONE_GROUP  #注册组 (服务组管理,不强制)
      config:
        server-addr: localhost:8848 #nacos注册地址
        file-extension: yaml #配置文件后缀支持 txt json yam xml html Properties 
        group: ONE_GROUP # 分组 (配置组管理,不强制)
        prefix: nocas_pubilc


## 暴露监控端点 spring boot admin 使用的
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Nacos是一个服务注册中心,可以用于微服务架构中的服务注册与发现。要使用Spring Cloud Nacos作为注册中心,首先需要引入Nacos客户端依赖,并配置注册中心地址。在pom.xml文件中添加以下依赖: ```xml <!-- Nacos客户端依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> ``` 然后,在application.yml或application.properties中进行配置,指定Nacos注册中心地址: ```yaml spring: cloud: nacos: server-addr: 127.0.0.1:8848 ``` 这样就完成了Spring Cloud Nacos注册中心配置。使用Nacos作为注册中心与使用Eureka相比,并没有太大区别,因为Nacos也遵循了Spring Cloud定义的服务注册与发现规范。同时,Spring Cloud Nacos还提供了可视化界面,可以将微服务注册到Nacos,并支持CP和AP两种方式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringCloudNacos,服务注册中心](https://blog.csdn.net/qq_38668544/article/details/120066467)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [day2021-11-11(springcloud Nacos注册中心)](https://blog.csdn.net/TIM_Zhang1122/article/details/121268351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [spring cloud nacos 注册中心](https://download.csdn.net/download/weixin_43326401/12545665)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值