Hystrix使用

Hystrix项目使用

配置pom.xml依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>

配置RestTemplate对象

@Configuration
public class RestTemplateConfiguration {
	@Bean
	public RestTemplate create() {
		return new RestTemplate();
	}
}

添加测试类

@RestController
public class HystrixController {
	@Autowired
	private RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "failCallHello")
	@GetMapping("/hello")
	public String callHello() {
	    String result = restTemplate.getForObject("http://192.168.1.55:8081/hello", String.class);
	    return result;
	}
	
	public String failCallHello() {
		return "Fail";
	}
	
	@HystrixCommand(fallbackMethod = "failCallHello", commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") })
	@GetMapping("/hello2")
	public String callHello2() {
		String result = "Ok";
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return result;
	}
}
  • /hello测试程序异常
  • /hello2测试超时异常

激活Hystrix,启动类添加注解@EnableHystrix

@EnableHystrix
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

准备

启动Eureka服务,并注册两个应用作为测试集群机器,如图所示;测试接口/hello
在这里插入图片描述

Feign项目使用

再添加配置pom.xml依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

封装Feign接口,eureka-provider-app为注册到Eureka中的服务名称

@FeignClient(value = "eureka-provider-app", fallback = TestClientFailBack.class)
public interface TestClient {
	@GetMapping("/hello")
	String hello();
}

TestClientFailBack失败处理策略

@Component
public class TestClientFailBack implements TestClient{
	@Override
	public String hello() {
		return "FAIL_fail";
	}
}

添加测试类

@RestController
public class TestController {
	@Autowired
	private TestClient client;

	@GetMapping("/test")
	public String test() {
		String result = client.hello();
		return result;
	}
}

激活Feign配置

@EnableFeignClients(basePackageClasses = {TestClient.class})
@EnableHystrix
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

配置文件application.properties添加Eureka注册配置,启动feign与hystrix整合

eureka.client.serviceUrl.defaultZone=http://192.168.1.55:8761/eureka/
feign.hystrix.enabled=true```

添加测试类

```java
@RestController
public class TestController {
	@Autowired
	private TestClient client;

	@GetMapping("/test")
	public String test() {
		String result = client.hello();
		return result;
	}
}

测试,成功测试

在这里插入图片描述
停掉应用,测试
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值