Spring Cloud 服务监控 – Hystrix、Eureka admin 和 Spring boot admin

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(853)   2023-03-28 11:29:14

项目源码下载:(访问密码:9987)
spring-cloud-dashboards.zip

在交付基于微服务的应用程序时,广泛使用 Spring Boot 和 Spring Cloud。最终,基于运行在不同主机上的 Spring boot 应用程序来监控微服务已成为一种必要。有许多工具可用于监控这些微服务的各种健康状态。

在这个spring 云教程中,我们将学习使用三个这样的监控工具,即Hystrix 仪表板Eureka 管理仪表板Spring boot 管理仪表板
 

1. 概述

在这个演示中,我们将创建三个应用程序
  1. 员工服务——这个微服务应用程序负责获取员工的数据。
  2. Api-Gateway - 此应用程序是在访问不同的微服务时提供通用网关。在以下示例中,它将充当上述员工服务的网关。
  3. Eureka Server – 该微服务应用程序将提供上述微服务的服务发现和注册。
这个演示是围绕Netflix Eureka创建的,用于集中管理和监控注册的应用程序。您可能已经知道 Netflix Eureka 服务器用于构建服务注册服务器和关联的 Eureka 客户端,它们将注册自己以查找其他服务并通过 REST api 进行通信。

2. 技术栈

  • Java 1.8
  • Spring tool suite
  • Spring cloud
  • Spring boot
  • Spring Rest
  • Maven

3. 员工服务

  • 从Spring boot 初始化程序/ Spring 工具套件创建一个 Spring boot 项目,依赖Eureka DiscoveryActuatorWebRest 存储库项目构建
  • EmployeeServiceApplication启动 Spring boot 应用程序的主应用程序类。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     
    @SpringBootApplication
    @EnableEurekaClient
    public class EmployeeServiceApplication {
     
      public static void main(String[] args) 
      {
        SpringApplication.run(EmployeeServiceApplication.class, args);
      }
    }
        @EnableEurekaClient – 此注解将此服务注册为下面创建的Eureka 服务器应用程序中的 Eureka 客户端。
  • 创建一个 Rest 控制器类 [ EmployeeServiceController ] 来公开 Employee 数据
     
    import java.util.HashMap;
    import java.util.Map;
     
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
     
    import com.howtodoinjava.example.employee.beans.Employee;
     
    @RestController
    public class EmployeeServiceController {
         
         
        private static final Map<Integer, Employee> employeeData = new HashMap<Integer,Employee() {
     
            private static final long serialVersionUID = -3970206781360313502L;
            {
                put(111,new Employee(111,"Employee1"));
                put(222,new Employee(222,"Employee2"));
            }
        };
      
        @RequestMapping(value = "/findEmployeeDetails/{employeeId}", method = RequestMethod.GET)
        public Employee getEmployeeDetails(@PathVariable int employeeId) {
            System.out.println("Getting Employee details for " + employeeId);
      
            Employee employee = employeeData.get(employeeId);
            if (employee == null) {
                 
                employee = new Employee(0, "N/A");
            }
            return employee;
        }
    }
       相关的EmployeeBean 类如下
       
public class Employee {
 
    private String name;
    private int id;
 
    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + "]";
    }
}
application.ymlsrc/main/resources目录中创建。
server:
  port: 8011   
  
eureka:         
  instance:
    leaseRenewalIntervalInSeconds: 5
    leaseExpirationDurationInSeconds: 2
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    healthcheck:
      enabled: true
    lease:
      duration: 5
  
spring:    
  application:
    name: employee-service   
      
management:
  security:
    enabled: false 
  
logging:
  level:
    com.self.sprintboot.learning.employee: DEBUG
启动此应用程序可达 http://localhost:8011/findEmployeeDetails/111

4. 带有 Hystrix 的 API 网关

  • 从Spring boot 初始值设定项/ Spring 工具套件创建一个 Spring boot 项目,并具有依赖项Eureka DiscoveryActuatorWebHystrixHystrix DashboardRest repositories项目创建
  • ApiGatewayApplication启动 Spring boot 应用程序的主要应用程序类。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
     
    @SpringBootApplication
    @EnableEurekaClient
    @EnableHystrixDashboard
    @EnableCircuitBreaker
    public class ApiGatewayApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(ApiGatewayApplication.class, args);
        }
    }
    @EnableHystrixDashBoard – 提供 Hystrix 流的仪表板视图。
    @EnableCircuitBreaker – 启用断路器实现。
  • 创建 REST 控制器类 [ EmployeeController ] 以公开 Employee 数据。
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.ParameterizedTypeReference;
    import org.springframework.http.HttpMethod;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
     
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
     
    @RestController
    public class EmployeeController {
         
        @Autowired
        RestTemplate restTemplate;
      
        @RequestMapping(value = "/employeeDetails/{employeeid}", method = RequestMethod.GET)
        @HystrixCommand(fallbackMethod = "fallbackMethod")
        public String getStudents(@PathVariable int employeeid)
        {
            System.out.println("Getting Employee details for " + employeeid);
      
            String response = restTemplate.exchange("http://employee-service/findEmployeeDetails/{employeeid}",
                                    HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, employeeid).getBody();
      
            System.out.println("Response Body " + response);
      
            return "Employee Id -  " + employeeid + " [ Employee Details " + response+" ]";
        }
         
        public String  fallbackMethod(int employeeid){
             
            return "Fallback response:: No employee details available temporarily";
        }
      
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
  • application.ymlsrc/main/resources目录中创建。
    server:
      port: 8010    #port number
      
    eureka:
      instance:
        leaseRenewalIntervalInSeconds: 5
        leaseExpirationDurationInSeconds: 2
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
        healthcheck:
          enabled: true
        lease:
          duration: 5
      
    spring:    
      application:
        name: api-gateway   
          
    management:
      security:
        enabled: false 
      
    logging:
      level:
        com.self.sprintboot.learning.apigateway: DEBUG
  • 启动可访问的应用程序http://localhost:8010/employeeDetails/111访问结果

5. Hystrix 仪表盘视图

  • 通过 Hystrix 仪表板进行监控,请在 处打开 Hystrix 仪表板http://localhost:8010/hystrix仪表板
       这是需要放置事件流 URL 以进行监控的主页
  • 现在在仪表板中查看hystrix 流http://localhost:8010/hystrix.stream监控图
       这提供了所有 Hystrix 命令和线程池的实时信息。
 

6. Eureka 管理仪表板视图

现在让我们学习使用 Eureka 管理仪表板视图。
  • 使用这些依赖项Eureka ServerActuatorWebSpring Boot Admin Server从Spring boot 初始化程序/ Spring 工具套件创建一个 Spring boot 项目。服务创建
  • EurekaServerApplication启动 spring boot 应用程序的主要应用程序类。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
     
    import de.codecentric.boot.admin.config.EnableAdminServer;
     
    @SpringBootApplication
    @EnableEurekaServer
    @EnableAdminServer
     
    public class EurekaServerApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    @EnableEurekaServer – 此注释将使此应用程序充当微服务注册中心和发现服务器。
    @EnableAdminServer – 这个注解提供了 Spring Boot Admin 配置。
  • 创建application.ymlbootstrap.ymlsrc/main/resources目录中。
  • 添加application.yml给定的配置。请注意,对于 Spring boot 管理服务器,提供了不同的上下文路径/admin,以免与/eureka.
    server:
      port: ${PORT:8761}
      
    eureka:
      client:
        registryFetchIntervalSeconds: 5
        registerWithEureka: false
        serviceUrl:
          defaultZone: ${DISCOVERY_URL:http://localhost:8761}/eureka/
      instance:
        leaseRenewalIntervalInSeconds: 10
      
    management:
      security:
        enabled: false
    spring:
      boot:
        admin:
          context-path: /admin  #A different context path for Spring boot admin server has been provided avoiding conflict with eureka
    
  • 创建bootstrap.yml并提供此配置。
    $title(bootstrap.yml)
    spring:
      application:
        name: Eureka-Server
      cloud:
        config:
          uri: ${CONFIG_SERVER_URL:http://localhost:8888}
  • 启动应用程序。但在此之前,请确保上面提到的其余客户端应用程序都已启动,以便查看所有已注册的应用程序。此应用程序可通过 访问http://localhost:8761服务中心监控界面

7. Spring Boot 管理仪表板视图

  • 要通过 Spring Boot Admin 服务器进行监控,请调用在不同上下文路径中运行的此 URL- http://localhost:8761/adminadmin面包
  • 此管理界面提供应用程序概览、桌面通知、应用程序运行状况检查、日志文件浏览、JMX Bean、线程堆转储等。要查看单个应用程序运行状况并监控其指标,请单击详细信息按钮。它将带您进入单个应用程序的管理仪表板。admin仪表盘
  • 使用仪表板管理日志级别。日志监控
  • 使用仪表板管理运行时环境属性。admin环境模块
  • 您也可以使用它来查看 HTTP 跟踪。http跟踪
 
项目源码下载:(访问密码:9987)
spring-cloud-dashboards.zip
标签:
地址:https://www.leftso.com/article/855.html

相关阅读

项目源码下载:(访问密码:9987)spring-cloud-dashboards.zip在交付基于微服务的应用程序时,广泛使用 Spring Boot 和 Spring Cloud
演示项目源码下载:(访问密码:9987)Spring-Cloud-discovery-server.zip 了解如何创建微服务的基础上,Spring Cloud,对Netflix的Eureka注...
项目源码下载:(访问密码:9987)Spring-Cloud-Circuit-Breaker.zip学习在调用底层微服务的同时利用调用的Spring Cloud Netflix堆栈组件之一Hys...
随着Spring Cloud 的越来越流行,国内很多公司也在开始使用该框架了
Java编程之Spring Cloud Hystrix Circuit熔断/断路
演示项目源码下载:(访问密码:9987)Spring-Cloud-Consoul.zip了解如何创建微服务的基础上Spring cloud,对登记HashiCorp Consul注册服务器,以及...
在这个 Spring cloud 教程中,学习在 spring boot/cloud 项目中使用 Netflix Ribbon 使用客户端负载平衡
演示项目源码下载:(访问密码:9987)spring-cloud-zipkin.zipZipkin是非常有效的工具分布追踪在微服务生态系统
项目源码下载:(访问密码:9987)spring-cloud-apigateway_zuul.zip学习使用Netflix Zuul及其与Spring Cloud 的牢固结合来创建负载均衡器
在spring cloud项目中配置配置服务的地址spring.cloud.config.uri不生效的解决办法,spring cloud