Spring Cloud Admin 与eureka集成

2019-11-21  本文已影响0人  舍是境界

单体应用的admin这里就略过了,比较简单,这里主要说明spring cloud分布式的用法,单体请见官网:https://codecentric.github.io/spring-boot-admin/1.5.3/#set-up-admin-server
这里将eureka和admin集成在一个服务上,当然也可以分开,这里为了减少服务的个数,将这两个服务集成在一起。

环境

注册中心以及Admin端

pom.xml

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

        <!-- 添加basic认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.6</version>
        </dependency>

配置文件

server:
  port: 8761
spring:
  application:
    name: eureka
  security:
    user:
      name: eureka
      password: eureka_pass
  boot:
    admin:
      context-path: /admin #admin界面与eureka分离
eureka:
  client:
    healthcheck:
      enabled: true
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka/
    fetch-registry: true
    # 单实例情况不需要相互注册,这里因为admin端需要被发现所以注册上
    register-with-eureka: true
  instance:
    prefer-ip-address: true
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

启动类

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

Security配置

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic()
                .and().authorizeRequests()
                .antMatchers("/actuator/**").permitAll() #这里根据你的实际情况暴露对应需要监控的服务
                .anyRequest().authenticated()
                .and().csrf().disable();
    }
}

Admin客户端

pom.xml

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

配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka:eureka_pass@localhost:8761/eureka/
    healthcheck:
      enabled: true
  instance:
    prefer-ip-address: true
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

启动类

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

security配置

同上即可

效果展示:

image.png
image.png
image.png
image.png

番外配置介绍

这里以一个我解决的历史遗留问题为demo,介绍下当我们服务有自定义路由前缀时,为了兼顾spring boot 1.5.x和spring boot 2.0.0我们怎么配置这个监控。
问题描述:历史遗留系统的zuul会根据各个微服务之前定义好的前缀为路由到其服务中,并且路由前缀会带到下游服务中,同时部分系统是1.5.x,部分是2.x版本以上,两者需怎么兼顾?

具体情况及配置

配置略
management:
  security:
    enabled: false
  endpoints:
    web:
      base-path: /client1
      path-mapping:
        health: /health
      exposure:
        include: "*"
eureka:
  instance:
    prefer-ip-address: true
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /client1/health
    metadata-map:
      management.context-path: "/client1"
management:
  security:
    enabled: false
endpoints:
  health:
    enabled: true
    sensitive: true
    path: /client2/health
eureka:
  instance:
    prefer-ip-address: true
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /client2/health

开启actuator端点需引入依赖:

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

总结

上一篇下一篇

猜你喜欢

热点阅读