git add .
This commit is contained in:
parent
7c3e42909c
commit
c19c349fd5
@ -2,13 +2,14 @@ package com.heibaiying.consumer.feign;
|
||||
|
||||
import com.heibaiying.common.feign.ProductFeign;
|
||||
import com.heibaiying.consumer.config.FeignConfig;
|
||||
import com.heibaiying.consumer.feign.impl.CProductFeignImpl;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 声明式接口调用
|
||||
*/
|
||||
@FeignClient(value = "producer",configuration = FeignConfig.class)
|
||||
@FeignClient(value = "producer",configuration = FeignConfig.class,fallback = CProductFeignImpl.class)
|
||||
public interface CProductFeign extends ProductFeign {
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.heibaiying.consumer.feign.impl;
|
||||
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import com.heibaiying.consumer.feign.CProductFeign;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 定义发生错误时候的熔断处理。除了继承自CProductFeign,还需要用@Component声明为spring的组件
|
||||
*/
|
||||
@Component
|
||||
public class CProductFeignImpl implements CProductFeign {
|
||||
|
||||
@Override
|
||||
public List<Product> productList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Product productDetail(int id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Product product) {
|
||||
|
||||
}
|
||||
}
|
@ -9,3 +9,7 @@ eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
feign:
|
||||
hystrix:
|
||||
# 如果为true,则OpenFign客户端将使用Hystrix断路器进行封装 默认为false
|
||||
enabled: true
|
||||
|
@ -10,11 +10,15 @@
|
||||
<input type="submit" value="新增产品">
|
||||
</form>
|
||||
<ul>
|
||||
<#list products as product>
|
||||
<li>
|
||||
<a href="/sell/product/${product.id}">${product.name}</a>
|
||||
</li>
|
||||
</#list>
|
||||
<#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>
|
||||
|
@ -9,6 +9,8 @@ import org.springframework.stereotype.Service;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
@ -20,16 +22,18 @@ public class ProductService implements IProductService, ApplicationListener<WebS
|
||||
private static List<Product> productList = new ArrayList<>();
|
||||
|
||||
public Product queryProductById(int id) {
|
||||
for (Product product : productList) {
|
||||
if (product.getId() == id) {
|
||||
return product;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return productList.stream().filter(p->p.getId()==id).collect(Collectors.toList()).get(0);
|
||||
}
|
||||
|
||||
|
||||
public List<Product> queryAllProducts() {
|
||||
// 用于测试 hystrix 超时熔断
|
||||
try {
|
||||
int i = new Random().nextInt(2500);
|
||||
Thread.sleep(i);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return productList;
|
||||
}
|
||||
|
||||
|
@ -9,3 +9,4 @@ eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
|
||||
|
3
spring-cloud/spring-cloud-hystrix/README.md
Normal file
3
spring-cloud/spring-cloud-hystrix/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
http://localhost:8040/turbine.stream
|
||||
|
||||
http://localhost:8020/actuator/hystrix.stream
|
@ -15,7 +15,17 @@ import org.springframework.context.annotation.Bean;
|
||||
@EnableHystrixDashboard
|
||||
public class ConsumerApplication {
|
||||
|
||||
@Bean
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 这种方式对servlet进行注册和在配置文件中开启端点是等效的
|
||||
* 可以在启动的监控日志中看到端点的注册信息:
|
||||
* o.s.b.a.e.web.ServletEndpointRegistrar : Registered '/actuator/hystrix.stream' to hystrix.stream-actuator-endpoint
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean getServlet() {
|
||||
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
|
||||
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
|
||||
@ -23,11 +33,7 @@ public class ConsumerApplication {
|
||||
registrationBean.addUrlMappings("/actuator/hystrix.stream");
|
||||
registrationBean.setName("HystrixMetricsStreamServlet");
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConsumerApplication.class, args);
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.consumer.controller;
|
||||
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@RestController
|
||||
public class CustomController {
|
||||
|
||||
@GetMapping("consumers")
|
||||
public String queryCustoms() {
|
||||
return "用户产品偏好列表生成中,请月底再获取";
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8080
|
||||
port: 8030
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
@ -9,4 +9,10 @@ eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
# 需要开启hystrix.stream端点的暴露 这样才能获取到监控信息 * 代表开启可监控端点
|
||||
include: "*"
|
||||
|
||||
|
@ -27,6 +27,26 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</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>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
@ -3,11 +3,24 @@ package com.heibaiying.producer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
|
||||
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableHystrix
|
||||
@EnableHystrixDashboard
|
||||
public class ProducerApplication {
|
||||
|
||||
@LoadBalanced // 配置客户端负载均衡
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProducerApplication.class, args);
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package com.heibaiying.producer.controller;
|
||||
|
||||
import com.heibaiying.producer.service.api.ICustomService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 用来测试turbine聚合监控功能的接口 调用了custom的服务
|
||||
*/
|
||||
@RestController
|
||||
public class CustomController {
|
||||
|
||||
@Autowired
|
||||
private ICustomService customService;
|
||||
|
||||
@GetMapping("consumers")
|
||||
public String queryCustoms() {
|
||||
return customService.queryCustoms();
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.heibaiying.producer.controller;
|
||||
|
||||
import com.heibaiying.common.api.IProductService;
|
||||
import com.heibaiying.common.bean.Product;
|
||||
import com.heibaiying.producer.service.api.ICustomService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@ -17,6 +18,7 @@ public class ProducerController {
|
||||
@Autowired
|
||||
private IProductService productService;
|
||||
|
||||
|
||||
@GetMapping("products")
|
||||
public List<Product> productList() {
|
||||
return productService.queryAllProducts();
|
||||
@ -31,4 +33,5 @@ public class ProducerController {
|
||||
public void save(@RequestBody Product product) {
|
||||
productService.saveProduct(product);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.heibaiying.producer.service;
|
||||
|
||||
import com.heibaiying.producer.service.api.ICustomService;
|
||||
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Service
|
||||
public class CustomService implements ICustomService {
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@Override
|
||||
@HystrixCommand(fallbackMethod = "queryCustomsFail")
|
||||
public String queryCustoms() {
|
||||
return restTemplate.getForObject("http://consumer/consumers", String.class);
|
||||
}
|
||||
|
||||
public String queryCustomsFail() {
|
||||
return "获取用户列表失败";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.heibaiying.producer.service.api;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 用户接口
|
||||
*/
|
||||
public interface ICustomService {
|
||||
|
||||
String queryCustoms();
|
||||
|
||||
}
|
@ -9,3 +9,9 @@ eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
# 需要开启hystrix.stream端点的暴露 这样才能获取到监控信息 * 代表开启可监控端点
|
||||
include: "*"
|
||||
|
@ -1,14 +1,11 @@
|
||||
package com.heibaiying.turbine;
|
||||
|
||||
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.cloud.netflix.turbine.EnableTurbine;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@ -17,17 +14,6 @@ import org.springframework.context.annotation.Bean;
|
||||
@EnableTurbine
|
||||
public class TurbineApplication {
|
||||
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean getServlet() {
|
||||
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
|
||||
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
|
||||
registrationBean.setLoadOnStartup(1);
|
||||
registrationBean.addUrlMappings("/hystrix.stream");
|
||||
registrationBean.setName("HystrixMetricsStreamServlet");
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TurbineApplication.class, args);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 8090
|
||||
port: 8040
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
@ -13,9 +13,9 @@ eureka:
|
||||
turbine:
|
||||
aggregator:
|
||||
cluster-config: default
|
||||
app-config: consumer
|
||||
clusterNameExpression: new String("default")
|
||||
instanceUrlSuffix:
|
||||
default: actuator/hystrix.stream
|
||||
combine-host-port: true
|
||||
app-config: consumer,producer
|
||||
clusterNameExpression: "'default'"
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user