目录:
Hystrix集群、
Feign和Hystrix整合
Hystrix集群
导入pom依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.liuxia</groupId>
<artifactId>t226_microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.liuxia</groupId>
<artifactId>microservice-student-provider-hystrix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-student-provider-hystrix</name>
<description>Demo project for Spring Boot</description>
<properties>
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加注册中心Eureka相关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- actuator监控引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.liuxia</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- actuator监控引入 -->
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
导入application.yml文件
---
server:
port: 1004
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/t226_layui?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1004
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1004
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.liuxia.com:2001/eureka/,http://eureka2002.liuxia.com:2002/eureka/,http://eureka2003.liuxia.com:2003/eureka/
info:
groupId: com.liuxia.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://liuxia.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
ribbon:
ReadTimeout: 10000
ConnectTimeout: 9000
---
server:
port: 1005
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/t226_layui?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1005
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1005
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.liuxia.com:2001/eureka/,http://eureka2002.liuxia.com:2002/eureka/,http://eureka2003.liuxia.com:2003/eureka/
info:
groupId: com.liuxia.testSpringcloud
artifactId: microservice-student-provider-hystrix-1005
version: 1.0-SNAPSHOT
userName: http://liuxia.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
---
server:
port: 1006
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/t226_layui?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1006
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1006
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.liuxia.com:2001/eureka/,http://eureka2002.liuxia.com:2002/eureka/,http://eureka2003.liuxia.com:2003/eureka/
info:
groupId: com.liuxia.testSpringcloud
artifactId: microservice-student-provider-hystrix-1006
version: 1.0-SNAPSHOT
userName: http://liuxia.com
phone: 123456
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
这个里面需要注意的就是我们设置了hystrix的默认请求超时时间,因为我们在后面还要设置集群后的Ribbon的超时时间所以在这里讲清楚
而我们是利用Ribbon来连接注册中心和消费者、Hystrix来设置生产者请求注册中心的熔断,但是呢我们Ribbon的操作时间就包括了Hystrix的请求时间,所以我们设置Ribbon的默认超时时间一定要比Hystrix长
项目启动类
package com.liuxia.microservicestudentproviderhystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableCircuitBreaker
@EntityScan("com.liuxia.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
}
}
这样我们的hystrix的集群就搭建好了
然后我们创建
microservice-student-consumer-hystrix-turbine-91
来监控这个集群
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
application.yml
server:
port: 91
context-path: /
eureka:
client:
service-url:
defaultZone: http://eureka2001.liuxia.com:2001/eureka/,http://eureka2002.liuxia.com:2002/eureka/,http://eureka2003.liuxia.com:2003/eureka/
turbine:
app-config: microservice-student
clusterNameExpression: "'default'"
spring:
application:
name: turbine
配置启动类
package com.liuxia.microservicestudentconsumerhystrixturbine91;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerHystrixTurbine91Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerHystrixTurbine91Application.class, args);
}
}
然后我们先把1004启动、然后把1005、1006带hystrix启动,然后启动80消费者,就能测试了
这样的话 http://localhost/student/hystrix 就能调用服务集群
http://localhost:91/turbine.stream 我们输入看是否能出现ping的字,能就是配置成功了,我们就能进入监控平台了,然后就在Hystrix中输入地址
同时我们在http://localhost/student/hystrix不断请求就能看到监控数据了
Feign和Hystrix整合
在你的公共项目
microservice-common
StudentClientFallbackFactory
package com.liuxia.microservicecommon.service;
import com.liuxia.microservicecommon.entity.Student;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> {
@Override
public StudentClientService create(Throwable cause) {
return new StudentClientService() {
@Override
public boolean save(Student student) {
return false;
}
@Override
public List<Student> list() {
return null;
}
@Override
public Map<String, Object> hystrix() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", 500);
map.put("info", "系统繁忙,稍后重试");
return map;
}
@Override
public Student get(Integer id) {
return null;
}
@Override
public boolean delete(Integer id) {
return false;
}
@Override
public String ribbon() {
return null;
}
};
}
}
在
StudentClientService
添加注解和方法
/**
* Student Feign接口客户端
* @author Administrator
*
*/
@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)
public interface StudentClientService {
/**
* 服务熔断降级
* @return
*/
@GetMapping(value="/student/hystrix")
public Map<String,Object> hystrix();
}
然后再外面集群的项目microservice-student-provider-hystrix
在service定义方法
/**
* 测试Hystrix服务降级
* @return
*/
public Map<String,Object> hystrix();
StudentServiceImpl
package com.liuxia.microservicestudentproviderhystrix.service.impl;
import com.liuxia.microservicecommon.entity.Student;
import com.liuxia.microservicestudentproviderhystrix.repository.StudentRepository;
import com.liuxia.microservicestudentproviderhystrix.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public void save(Student student) {
studentRepository.save(student);
}
@Override
public Student findById(Integer id) {
return studentRepository.findOne(id);
}
@Override
public List<Student> list() {
return studentRepository.findAll();
}
@Override
public void delete(Integer id) {
studentRepository.delete(id);
}
@Value("${server.port}")
private String port;
@Override
public Map<String, Object> hystrix() {
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 200);
map.put("info","工号【"+port+"】正在为您服务");
return map;
}
}
在controller调用方法
/**
* 测试Hystrix服务降级
* @return
* @throws InterruptedException
*/
@ResponseBody
@GetMapping(value="/hystrix")
@HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
Thread.sleep(2000);
return studentService.hystrix();
}
microservice-student-consumer-feign-80修改 支持Hystrix
/**
* 测试Hystrix服务降级
* @return
*/
@GetMapping(value="/hystrix")
@ResponseBody
public Map<String,Object> hystrix(){
return studentClientService.hystrix();
}
microservice-student-consumer-feign-80的application.yml加上hystrix支持
feign:
hystrix:
enabled: true
microservice-student-consumer-feign-80上加个 ribbon超时时间设置,我们设置的Hystrix默认时长是1500,所以我们只需要比1500大就Ok了
以前hystrix的设置
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
现在添加的Ribbon的设置
ribbon:
ReadTimeout: 10000
ConnectTimeout: 9000
当我们设置了,1005和1006都能访问了,并且一样能进行错误处理