springbootadmin-上手
配置服务端
-
引入依赖
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.0</version> </dependency>
-
修改配置文件
spring: application: name: admin-service server: port: 8000
-
启动类添加注解
@EnableAdminServer
-
访问
http://localhost:8000
即可访问服务端页面
安全
-
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
-
增加配置类
@Configuration public class AdminSecurityConfig extends WebSecurityConfigurerAdapter { private String adminContextPath; public AdminSecurityConfig(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(); } }
-
修改配置文件
spring: security: user: name: dqn password: dqn
-
此时访问服务端页面,或客户端向服务端注册就需要输入密码
配置客户端
-
导入依赖
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
修改配置文件
server: port: 8081 spring: application: name: test-server #spirngbootadmin配置 boot: admin: client: username: dqn #账号 password: dqn #密码 url: http://127.0.0.1:8000 #地址 # actuator配置 management: server: port: 9090 #使用端口 endpoints: web: exposure: include: "*" #开启全部的监控终端 base-path: /actuator #访问子路径
-
这样项目就可以被服务端健康