109天
行百里者半九十,想要在一个行业里成为顶尖人才,一定满足一万小时定律,要想学好JAVA,需要持之以恒不断地努力,每天都要勤思考+善于询问+解决问题!
知识温故而知新>>>>>>
今天学到的知识>>>>>>
1. 解决灾难性雪崩效应-隔离机制-信号量隔离-信号量隔离处理
- 什么是信号量隔离?
- 信号量隔离参数的作用是什么?
2. 线程池隔离和信号量隔离的区别
- 线程池隔离与信号量隔离有什么区别?
- 什么情况下使用线程池隔离?什么情况下使用信号量隔离?
7. 创建Turbine聚合服务器
- Turbine的作用是什么?
10. 采用RabbitMQ收集数据监控-设计原理与服务搭建
- 使用RabbitMQ收集数据的原理是什么?并绘制图形
- 描述RabbitMQ收集监控数控的详细步骤
习题的情况总结>>>>>>
Feign 的服务降级处理
1.2修改配置文件开启对 hystrix 的支持
spring.application.name=springcloud-eureka-consumer-feign-f allback
server.port=9020
#设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://user:123456@eur eka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/
#Feign 默认是不开启 Hystrix 的。默认为:false
feign.hystrix.enabled=true
1.3修改 ProductConsumerService
@FeignClient(name="ego-product-provider",fallback=ProductSe rviceFallback.class)
public interface ProductConsumerService {
//查询所有商品
@RequestMapping(value="/product/findAll",method=RequestMeth od.GET)
public List<Product> findAll();
}
1.4添加 ProductServiceFallback 类
@Component
public class ProductServiceFallback implements ProductConsumerService {
//能够返回托底数据的 fallback方法
@Override
public List<Product> findAll() {
List<Product> list = new ArrayList<>();
list.add(new Product(-1, "我是托底数据"));
return list;
}
}
1.5修改 ProductController
/**
* Product Consumer 服务
* @author Administrator
*
*/
@RestController
public class ProductController {
@Autowired
ProductConsumerService consumerservice;
/**
* Consumer中的查询所有商品的方法
* @return
*/
@RequestMapping(value="/list",method=RequestMethod.GET) public List<Product> getAll(){
return this.consumerservice.findAll();
}
}
降级后的异常记录
2.2添加 ProductServiceFallbackFactory 类
@Component
public class ProductServiceFallbackFactory implements FallbackFactory<ProductConsumerService> {
Logger logger =
LoggerFactory.getLogger(ProductServiceFallbackFactory.class); @Override
public ProductConsumerService create(final Throwable arg0) {
return new ProductConsumerService() {
//能够返回托底数据的 fallback方法
@Override
public List<Product> findAll() {
logger.warn("Fallback Exception: ",arg0);
List<Product> list = new ArrayList<>();
list.add(new Product(-1, "我是托底数据")); return list;
}
};
}
}
2.3修改 ProductConsumerService
@FeignClient(name="ego-product-provider",fallbackFactory=Pr oductServiceFallbackFactory.class)
public interface ProductConsumerService {
//查询所有商品
@RequestMapping(value="/product/findAll",method=RequestMeth od.GET)
public List<Product> findAll();
}
可视化的数据监控 Hystrix-dashboard
2 修改 pom 文件添加 Hystrix-dashboard 坐标
<?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.bjsxt</groupId>
<artifactId>springcloud-eureka-consumer-ribbon-dashboard </artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-eureka-consumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository --> </parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEn coding>
<project.reporting.outputEncoding>UTF-8</project.reporting. outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<project.build.sourceEncoding>UTF-8</project.build.sourceEn coding>
<project.reporting.outputEncoding>UTF-8</project.reporting. outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
3 修改启动类
@EnableCircuitBreaker //开启熔断器 断路器
@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class ConsumerApplication {
public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,
args);
}
}
Hystrix-dashboard 监控中心
修改 pom 文件添加 Hystrix-dashboard 坐标
<?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.bjsxt</groupId>
<artifactId>springcloud-eureka-consumer-ribbon-dashboard </artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-eureka-consumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository --> </parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEn coding>
<project.reporting.outputEncoding>UTF-8</project.reporting. outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId >
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改启动类
@EnableCircuitBreaker //开启熔断器 断路器
@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class ConsumerApplication {
public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,
args);
}
}
网友评论