nacos springbootadmin 监控查看日志

        在网上看了很多帖子,都不行,最后东拼西凑,凑着凑着可以了,所以下面会有一些多余的包啊代码什么的,我也懒得删了,你们删吧。下面如果有些地方不对的,请大家指出,拜谢。话不多说,上货。

Admin 服务端

pom包如下


		     de.codecentric
		     spring-boot-admin-starter-server
		     2.6.7
		  
		
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
		  
		     org.springframework.boot
		     spring-boot-starter-web
		 
		 
		     org.springframework.boot
		     spring-boot-starter-actuator
		 
		 
		 
		     org.springframework.boot
		     spring-boot-starter-security
		 

为了方便,我把所有配置都放在bootstrap里了。当初弄了半天不显示日志就是因为logging那块没配置对,那里是个坑,我这里的日志地址就再项目根目录下(或者说是跟logback里log.path一样)。

下面bootstrap内容:

server:
  port: 8090
  servlet:
    context-path: /admin-server
spring:
  application:
    name: admin-server
  cloud:
    nacos:
      discovery:
        username: nacos用户名
        password: nacos密码
        server-addr: nacos地址:8848
        namespace: 你们自己的命名空间
        # 下面这几行解决项目已经启动但是在监控里还是down状态
        metadata:
         #  user.name: "admin"
          # user.password: "admin"
          management:
            context-path: ${server.servlet.context-path}/actuator
  boot:
    admin:
      client:
        username: admin
        password: admin
        url: http://localhost:8090
        instance:
          prefer-ip: true
      ui:
        title: ${spring.application.name}
#下面这个用户密码用来启动完访问登录的
  security:
    user:
      name: admin
      password: admin

#系统日志配置,name路径和resource下的log.path 路径一样,路径后面再加上文件名就可以
logging:
  file:
    name: ./logs/error.log
# 服务端点检查 没有下面代码监控左侧只有一个细节,没有别的东西
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

下面是一个安全控制的类(完全是粘贴别人的,由于参考太多文章了,也不知道是谁的了,抱歉)


import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

	private final String adminContextPath;

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

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		// @formatter:off
        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()
                //启用HTTP-Basic支持。这是Spring Boot Admin Client注册所必需的
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        //	禁用CRSF保护Spring引导管理客户端用来注册的端点。
                        adminContextPath + "/instances/**",
                        // 禁用执行器端点的CRSF保护
                        adminContextPath + "/actuator/**"
                );
    }
 
 
}

最后再启动类上加上admin注解

@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

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

}

最后访问,由于我配置了servlet:   context-path: /admin-server 所以访问的时候要加上这个http://localhost:8090/admin-server,就能打开登录页面,账号密码是配置文件里的admin/admin。到此服务端完事了(本来想放个图片的,公司网断了,连不上nacos了,项目起不来了,你们自己脑补一下吧)

-------------------------------------------------------------------------------------------------------------

客户端,也就是我们要监控的项目

pom


		    org.springframework.boot
		    spring-boot-starter-logging
		
		
	      org.jolokia
	      jolokia-core
		
		
			de.codecentric
			spring-boot-admin-starter-client
		
		
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
		    org.springframework.boot
		    spring-boot-starter-web
		
		
		     org.springframework.boot
		     spring-boot-starter-actuator
		 
		 
		 
		     org.springframework.boot
		     spring-boot-starter-security
		 

bootstrap(你们是不是要说怎么看着和admin server差不多?因为admin server那个我也做监控,如果不需要监控自己,可以去掉一些配置。还有如果谁的client端配置了context-path,那就把admin server 里的metadata这个家上)

server:
  port: 9080
spring:
  application:
    name: admin-client
  cloud:
    nacos:
      discovery:
        username: nacos账号
        password: nacos密码
        server-addr: 你们nacos地址
        namespace: 命名控件
  boot:
    admin:
      client:
        username: admin
        password: admin
        # url: http://localhost:8090
        instance:
          prefer-ip: true

logging:
  file:
    name: ./logs/error.log

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

安全类

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;

@EnableWebSecurity
public class RegisterWebSecurityConfigure extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/**")
                .and()
                .authorizeRequests()
                .antMatchers("/actuator/**","/test/**")
                .permitAll();
        super.configure(http);
    }
}

最后启动类(主要是服务发现注解)

@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {

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

}

好了大功告成,还是没有图片,自己脑补。

如果有看了我文章还不行的,请留言,咱们研究研究。

你可能感兴趣的:(springcloud,java,spring,springcloud,spring,boot)