org.springframework.cloud
spring-cloud-starter-hystrix
1.4.6.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka
1.4.6.RELEASE
org.springframework.boot
spring-boot-starter-actuator
com.tian
springcloud-api
1.0-SNAPSHOT
junit
junit
mysql
mysql-connector-java
com.alibaba
druid
ch.qos.logback
logback-core
org.mybatis.spring.boot
mybatis-spring-boot-starter
org.springframework.boot
spring-boot-test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-devtools
第三步、controller
package com.tian.springcloud.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.tian.springcloud.pojo.Dept;
import com.tian.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//提供restful服务!
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping(“dept/get/{id}”)
@HystrixCommand(fallbackMethod = “hystrixGet”)
public Dept get(@PathVariable(“id”) Long id){
Dept dept = deptService.queryById(id);
if (dept==null){
throw new RuntimeException(“id==+”+id+“不存在”);
}
return dept;
}
//备选方案
public Dept hystrixGet(@PathVariable(“id”) Long id){
return new Dept().setDeptno(id).setDname(“id==+”+id+“不存在,null. null @hystrix”)
.setDb_source(“no this database in MySql”);
}
}
第四步、主启动类
//启动类
@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到eureka中
@EnableDiscoveryClient
@EnableCircuitBreaker //添加熔断的支持
public class DeptProviderHystirx_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProviderHystirx_8001.class,args);
}
}
扩展 application.xml
eureka的配置 服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept-hystrix-8001 #修改eureka上的默认描述信息!
prefer-ip-address: true #true 可以显示服务的IP地址~
测试 7001 7002 8001 consumer80 访问
http://localhost/consumer/dept/get/1 正常
http://localhost/consumer/dept/get/6 返回自定义的信息
自己话理解就是 服务很多,访问的人也多,但是有些服务访问的人少,所以就先关闭,为了给访问的人多提供性能,让他更好的运行
编写个实现类 DeptClientServiceFallBackFactory
package com.tian.springcloud.service;
import com.tian.springcloud.pojo.Dept;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.List;
//降级
@Component
public class DeptClientServiceFallBackFactory implements FallbackFactory {
public DeptClientService create(Throwable throwable) {
return new DeptClientService() {
public Dept queryById(Long id) {
return new Dept().setDeptno(id)
.setDname(“id=”+id+“没有对应的信息,客户端提供了降级的信息,这个服务现在已经关闭了”)
.setDb_source(“没有数据”);
}
public List queryALL() {
return null;
}
public boolean addDept(Dept dept) {
return false;
}
};
}
}
Deptservice加上支持注解
@FeignClient(value = “SPRINGCLOUD-PROVIDER-DEPT”,
fallbackFactory = DeptClientServiceFallBackFactory.class)
springcloud-consumer-dept-feign application.xml 开启降级
开启降级feign.hystrix
feign:
hystrix:
enabled: true
服务熔断和服务降级比较
第一步、new module 加依赖
- springcloud-consumer-hystirx-dashboard
org.springframework.cloud
spring-cloud-starter-hystrix
1.4.6.RELEASE
org.springframework.cloud
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。