第二十章 Spring cloud+turbine+context-path(项目路径配置) 监测集群各项指标
2018-01-21 17:52
597 查看
Spring cloud+turbine+context-path(项目路径配置) 监测集群各项指标
项目结构如下:
EurekaApplication类
application.yml配置
pom.xml配置
OrderController类
User类
SpringCloudOrderApplication类
application.yml配置
pom.xml配置
项目启动顺序:
spring-cloud-eureka-->spring-cloud-user-->spring-cloud-order-ribbon-hystrix2-->spring-cloud-hystrix-turbine2-->spring-cloud-hystrixDashboard
项目结构如下:
EurekaApplication类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.turbine.EnableTurbine; @EnableTurbine @SpringBootApplication //开启启动程序入口类 public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
application.yml配置
server: port: 8031 spring: application: name: spring-cloud-hystrix-turbine2 eureka: client: serviceUrl: defaultZone: http://user:123456@localhost:8761/eureka instance: prefer-ip-address: true turbine: aggregator: clusterConfig: SPRING-CLOUD-ORDER-RIBBON-HYSTRIX2 appConfig: spring-cloud-order-ribbon-hystrix2 instanceUrlSuffix: SPRING-CLOUD-ORDER-RIBBON-HYSTRIX2: /ribbon/hystrix.stream #若不加此配置,到时候访问http:localhost:port/ribbon/hystrix.stream不了,报404 #此日志配置的是打印访问的URL logging: level: root: INFO com.netflix.turbine.monitor: DEBUG
pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.demo</groupId> <artifactId>spring-cloud-hystrix-turbine2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring-cloud-hystrix-turbine2</name> <description>spring-cloud-hystrix-turbine2</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <!-- 文件拷贝时的编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 编译时的编码 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <!-- jdk版本 --> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> <!-- 用于热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- 版本依赖管理,故之后添加依赖无需指定version --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 用以为integration-test提供支持。 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
OrderController类
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.example.demo.entity.User; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/order/{id}") @HystrixCommand(fallbackMethod="findByIdFallback")//熔断机制 public User findById(@PathVariable Long id) { // http://localhost:7900/user/ // VIP virtual IP(http://localhost:7900/user/表示虚拟ip) // HAProxy Heartbeat return this.restTemplate.getForObject("http://spring-cloud-user/user/" + id, User.class);//直接写服务提供者的spring.application.name,这样方便于动态IP } /** * 若是出现调用findById方法时出现错误或超时时,则调用此方法 * @param id * @return */ public User findByIdFallback(Long id){ User user = new User(); user.setId(0L); return user; } }
User类
package com.example.demo.entity; import java.math.BigDecimal; public class User { private Long id; private String username; private String name; private Short age; private BigDecimal balance; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Short getAge() { return this.age; } public void setAge(Short age) { this.age = age; } public BigDecimal getBalance() { return this.balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } }
SpringCloudOrderApplication类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker //熔断机制注解 public class SpringCloudOrderApplication { @Bean //必须new 一个RestTemplate并放入spring容器当中,否则启动时报错 @LoadBalanced //此注解整合了Ribbon,使客户端负载均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpringCloudOrderApplication.class, args); } }
application.yml配置
spring: application: name: spring-cloud-order-ribbon-hystrix2 #微服务的名称 server: port: 7902 #微服务端口号 context-path: /ribbon #Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application.yml文件中加入server.context-path = /你的path,比如:spring-boot,那么访问地址就是http://ip:port/spring-boot 路径。 eureka: client: healthcheck: enabled: true # 开启健康检查 serviceUrl: defaultZone: http://user:123456@localhost:8761/eureka #服务eureka的URL地址 instance: prefer-ip-address: true home-page-url-path: /ribbon #因为默认是/ #超时所设定的时间 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.demo</groupId> <artifactId>spring-cloud-order-ribbon-hystrix2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring-cloud-order-ribbon-hystrix2</name> <description>spring-cloud-order-ribbon-hystrix2</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <!-- 文件拷贝时的编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 编译时的编码 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <!-- jdk版本 --> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 注册中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 这个库让我们可以访问应用的很多信息,包括:/env、/info、/metrics、/health等 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 添加hystrix依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!-- 用于热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- 版本依赖管理,故之后添加依赖无需指定version --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 用以为integration-test提供支持。 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
项目启动顺序:
spring-cloud-eureka-->spring-cloud-user-->spring-cloud-order-ribbon-hystrix2-->spring-cloud-hystrix-turbine2-->spring-cloud-hystrixDashboard
相关文章推荐
- 项目路径配置[pageContext.request.contextPath]和c标签
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring Boot配置ContextPath【从零开始学Spring Boot】
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- Spring的ClassPathXmlApplicationContext在Eclipse中的路径问题
- 在js中获取request.contextPath项目路径
- Spring 的ClassPathXmlApplicationContext()路径读取错误
- spring中applicationContext.xml在web.xml中的配置路径说明
- 通过环境变量设置WEB项目数据源(Spring)及其项目配置文件路径,动态更新数据源及项目配置
- 万物生长 项目jar包越少越好,不然会导致eclipse进入断点延迟高,很慢. Spring+quartz 实现定时任务job集群配置
- Spring中用context:property-placeholder给项目装载配置文件
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring Boot 学习18--配置ContextPath
- Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决
- springboot、springcloud之静态资源路径的配置
- springboot配置ContextPath(配置全局过滤URl)
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- Spring-applicationContext.xml路径配置
- Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
- spring cloud turbine 分组配置