增加 spring-cloud-hystrix用例

This commit is contained in:
罗祥
2019-01-15 17:57:56 +08:00
parent 78f9e43988
commit 7c3e42909c
29 changed files with 813 additions and 0 deletions

View File

@ -0,0 +1,65 @@
<?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>
<parent>
<groupId>com.heibaiying.hystrix</groupId>
<artifactId>spring-cloud-hystrx</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>consumer</artifactId>
<dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--ribbon 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--hystrix 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--hystrix 监控依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--健康检查依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入对公共模块的依赖-->
<dependency>
<groupId>com.heibaiying.hystrix</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,33 @@
package com.heibaiying.consumer;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ConsumerApplication {
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

View File

@ -0,0 +1,21 @@
package com.heibaiying.consumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author : heibaiying
*/
@Configuration
public class RibbonConfig {
@LoadBalanced // 配置客户端负载均衡
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@ -0,0 +1,45 @@
package com.heibaiying.consumer.controller;
import com.heibaiying.common.api.IProductService;
import com.heibaiying.common.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
/**
* @author : heibaiying
*/
@Controller
@RequestMapping("sell")
public class SellController {
@Autowired
private IProductService productService;
@GetMapping("products")
public String productList(Model model) {
List<Product> products = productService.queryAllProducts();
model.addAttribute("products", products);
return "products";
}
@GetMapping("product/{id}")
public String productDetail(@PathVariable int id, Model model) {
Product product = productService.queryProductById(id);
model.addAttribute("product", product);
return "product";
}
@PostMapping("product")
public String save(@RequestParam String productName) {
long id = Math.round(Math.random() * 100);
Product product = new Product(id, productName, false, new Date(), 88);
productService.saveProduct(product);
return "redirect:products";
}
}

View File

@ -0,0 +1,46 @@
package com.heibaiying.consumer.service;
import com.heibaiying.common.api.IProductService;
import com.heibaiying.common.bean.Product;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* @author : heibaiying
* @description : 产品提供接口实现类
*/
@Service
public class ProductService implements IProductService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "queryProductsFail")
public List<Product> queryAllProducts() {
ResponseEntity<List> responseEntity = restTemplate.getForEntity("http://producer/products", List.class);
List<Product> productList = responseEntity.getBody();
return productList;
}
public Product queryProductById(int id) {
ResponseEntity<Product> responseEntity = restTemplate.getForEntity("http://producer/product/{1}", Product.class, id);
return responseEntity.getBody();
}
public void saveProduct(Product product) {
restTemplate.postForObject("http://producer/product", product, Void.class);
}
public List<Product> queryProductsFail() {
return new ArrayList<>();
}
}

View File

@ -0,0 +1,12 @@
server:
port: 8080
# 指定服务命名
spring:
application:
name: consumer
# 指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/

View File

@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<title>产品详情</title>
</head>
<body>
<ul>
<li>产品名称:${product.name}</li>
<li>产品序列号:${product.id}</li>
<li>是否贵重品:${product.isPrecious?string('是','否')}</li>
<li>生产日期: ${product.dateInProduced?string("yyyy-MM-dd HH:mm:ss")}</li>
<li>产品价格:${product.price}</li>
</ul>
</body>
</html>

View File

@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<title>产品列表</title>
</head>
<body>
<h3>产品列表:点击查看详情</h3>
<form action="/sell/product" method="post">
<input type="text" name="productName">
<input type="submit" value="新增产品">
</form>
<ul>
<#if (products?size>0) >
<#list products as product>
<li>
<a href="/sell/product/${product.id}">${product.name}</a>
</li>
</#list>
<#else>
<h4 style="color: red">当前排队人数过多,请之后再购买!</h4>
</#if>
</ul>
</body>
</html>

View File

@ -0,0 +1,17 @@
package com.heibaiying.consumer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerApplicationTests {
@Test
public void contextLoads() {
}
}