作用:
- 服務降級:
程序運行異常、超時、服務熔斷觸發服務降級、線程池或信號量打滿也會導致服務降級 - 服務熔斷:
類比保險絲達到最大服務訪問後,直接拒絕訪問,拉閘限電,然後調用服務降級的方法並返回友好提示
服務的降級->進而熔斷->恢復調用鏈路 - 服務限流:秒殺高併發等操作,嚴禁一窩蜂的過來擁擠,大家排隊,一秒鐘N個,有序進行
接近實時的監控
提供者hystrix-provider-payment8005
1.新建hystrix-provider-payment8005
2.pom
<dependencies>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--公共模塊:通用工具包-->
<dependency>
<groupId>cn.chen.demo</groupId>
<artifactId>api-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 熱部署依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.yml
server:
port: 8005 # 端口號
spring:
# 應用名稱
application:
name: hystrix-payment-service # 服務名稱
# 數據源
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 數據庫操作類型
driver-class-name: org.gjt.mm.mysql.Driver # 數據庫驅動
#url: jdbc:mysql://192.168.221.129:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
url: jdbc:mysql://localhost:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# 是否支持熱部署
devtools:
restart:
enabled: true
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka # 單機版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集羣版
instance:
instance-id: payment8005
prefer-ip-address: true
# eureka客戶端想服務端發動心跳的時間間隔,單位爲秒(默認是30秒)。開發的時候可以設置小一些,以保證服務關閉後註冊中心及時剔除服務
lease-renewal-interval-in-seconds: 1
# eureka服務端在收到最後一次心跳後等待時間上限,單位爲秒(默認是90秒)。開發時候設置小一些
lease-expiration-duration-in-seconds: 2
mybatis:
mapperLocations: classpath:mapper/*.xml #mapper文件
type-aliases-package: cn.chen.cloud.entity #所有entity別名所在包
4.主啓動
@SpringBootApplication
@EnableEurekaClient
public class HystrixPaymentApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixPaymentApplication.class,args);
}
}
5.業務類
1)service
@Service
public class PaymentService {
/**
* @Description 模擬訪問成功情況
**/
public String IsOk(){
return "線程池:"+Thread.currentThread().getName()+",IsOk。";
}
/**
* @Description 模擬訪問失敗情況
**/
public String IsTimeOut(int timeNumber){
try {
TimeUnit.SECONDS.sleep(timeNumber);
}catch (Exception e) {
e.printStackTrace();
}
return "線程池:"+Thread.currentThread().getName()+",IsTimeOut,耗時(秒)"+timeNumber;
}
}
2)controller
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
@Resource
private PaymentService paymentService;
@GetMapping("/hystrix/ok")
public String ok(){
String result = paymentService.IsOk();
log.info("*******result:"+result);
return result;
}
@GetMapping("/hystrix/timeout")
public String timeOut(){
String result = paymentService.IsTimeOut(3);
log.info("*******result:"+result);
return result;
}
}
6.啓動7001(單機版)
啓動8005(Hystrix)
消費者hystrix-consumer-order80
1.新建hystrix-consumer-order80
2.pom依賴(參考consumer-order80)
<dependencies>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--公共模塊-->
<dependency>
<groupId>cn.chen.demo</groupId>
<artifactId>api-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.yml配置(參考consumer-order80)
server:
port: 80
spring:
application:
name: hystrix-order-consumer
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka # 單機版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集羣版
instance:
instance-id: order80
prefer-ip-address: true
payment:
#url: http://localhost:8001
server:
name: http://payment-service
4.主啓動類
@SpringBootApplication
@EnableFeignClients
public class HystrixOrderApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixOrderApplication.class,args);
}
}