spring cloud spring-hystrix

Posted zfzf1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud spring-hystrix相关的知识,希望对你有一定的参考价值。

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR3</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<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-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		
	</dependencies>

  

@Service
@DefaultProperties(defaultFallback = "getMemberFallback")
public class MemberService {

	@Autowired
	private RestTemplate restTpl;

	@HystrixCommand(fallbackMethod = "getMemberFallback", groupKey = "MemberGroup", commandKey = "MemberCommandKey",
			commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
	}, threadPoolProperties = {
			@HystrixProperty(name = "coreSize", value = "2")
	})
	public Member getMember(Integer id) {
		try {
			Thread.sleep(2000);
		} catch (Exception e) {
			
		}
		
		Member member = restTpl.getForObject(
				"http://spring-hy-member/member/{id}", Member.class, id);
		return member;
	}

	public Member getMemberFallback(Integer id) {
		Member m = new Member();
		m.setId(1);
		m.setName("error member");
		return m;
	}
}

  

@RestController
public class TestController {

	@Autowired
	private MemberService memberService;

	@RequestMapping(value = "/router", method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Member router() {
		return memberService.getMember(1);
	}

}

  

以上是关于spring cloud spring-hystrix的主要内容,如果未能解决你的问题,请参考以下文章

spring-cloud-config-server

Spring Cloud实战Spring Cloud GateWay服务网关

spring-cloud-config——Quick Start

Spring Cloud入门 - Spring Cloud保护机制

spring cloud 与 spring boot 和 spring cloud alibab 版本号对应

整合spring cloud云服务架构 - Spring Cloud简介