在交付基于微服务的应用程序时,广泛使用 Spring Boot 和 Spring Cloud。最终,基于运行在不同主机上的 Spring boot 应用程序来监控微服务已成为一种必要。有许多工具可用于监控这些微服务的各种健康状态。项目源码下载:(访问密码:9987)
spring-cloud-dashboards.zip
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);
}
}
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;
}
}
Employee
Bean 类如下public class Employee {
private String name;
private int id;
@Override
public String toString() {
return "Employee [name=" + name + ", id=" + id + "]";
}
}
application.yml
在src/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
Eureka Discovery
、Actuator
、Web
、Hystrix
、Hystrix Dashboard
、Rest 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 流的仪表板视图。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.yml
在src/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
http://localhost:8010/hystrix
。http://localhost:8010/hystrix.stream
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 – 此注释将使此应用程序充当微服务注册中心和发现服务器。application.yml
并bootstrap.yml
在src/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
。http://localhost:8761/admin
。项目源码下载:(访问密码:9987)
spring-cloud-dashboards.zip
https://www.leftso.com/article/855.html