美文网首页
Spring Cloud Admin 与eureka集成

Spring Cloud Admin 与eureka集成

作者: 舍是境界 | 来源:发表于2019-11-21 17:20 被阅读0次

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

    环境

    • Spring Boot 2.0以上
    • Spring Cloud Greenwich.SR1

    注册中心以及Admin端

    • 注册中心使用eureka,其他的服务发现请参考官网
    • admin和注册中心集成在一个服务
    • 集成spring security,做基本的权限控制

    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客户端

    • 客户端只需要能被eureka发现即可,这里举一个简单的例子

    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版本以上,两者需怎么兼顾?

    具体情况及配置

    • spring boot admin:2.1.7
    配置略
    
    • admin client1: 2.1.7
      yml配置部分配置:
    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"
    
    • admin client2: 1.5.6
      yml部分配置配置:
    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>
    

    总结

    • 可以将admin和eureka分离部署,也可以集成在一起
    • 需要保证监控服务的health检测正常,然后控制监控暴露的endpoint
    • admin的客户端注册借助eureka非常简单,没有额外的配置工作
    • 有特殊配置要求时,注意eureka.instance.metadata-map参数的控制,详情可以查看官方文档
    • 1.5.x版本与2.x以上版本有不兼容差异,1.5.x版本功能不全,建议都升级到2.x以上
      如果对你有帮助或有什么问题,欢迎一起留言讨论

    相关文章

      网友评论

          本文标题:Spring Cloud Admin 与eureka集成

          本文链接:https://www.haomeiwen.com/subject/lamtwctx.html