整合Hystrix/基本整合与配置(一)

  • 基本整合与配置
  • 新建Maven项目:atm_eureka_hystrix_server
  • 新建Maven项目:atm_eureka_hystrix_provider
  • 新建Maven项目:atm_eureka_hystrix_invoker
  • 可回顾之前Spring Cloud_4_Eureka集群搭建

1、调用者整合Hystrix

1.1、引入依赖

<!-- Hystrix依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

1.2、PersonService

package com.atm.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class PersonService {

    @Autowired
    RestTemplate restTemplate;

    //使用Hystrix的注解,当服务提供者不启动时,直接直接回退方法
    @HystrixCommand(fallbackMethod = "findPersonFallback")
    public Person findPerson(Integer personId) {
        Person person = restTemplate.getForObject(
                "http://atm-eureka-hystrix-provider/person/{personId}",
                Person.class, personId);
        return person;
    }

    // fallBack方法需要和findPerson的参数一致
    public Person findPersonFallback(Integer personId) {
        Person p = new Person();

        p.setId(10);
        p.setAge(personId);
        p.setName("i am fallback!");

        return p;
    }
}

1.3、MyRouterController

package com.atm.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Configuration
public class MyRouterController {

    @Autowired
    private PersonService personService;

    @RequestMapping(value = "/router", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person router() {
        return personService.findPerson(2);
    }

}

1.4、HystrixInvokerApp

package com.atm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//打开断路器
public class HystrixInvokerApp {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

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

1.5、测试

  • 启动atm_eureka_hystrix_server(8761端口),其中的代码与之前小节一致
  • 不启动 atm_eureka_hystrix_provider(8080端口),其中的代码与之前小节一致
  • 启动atm_eureka_hystrix_invoker(9000端口),部分修改代码上面已经提供

2、命令配置

  • 一般配置(针对单个方法进行配置)
  • 默认配置(针对整个类进行配置)

2.1、一般配置

/**
*  commandKey:之前用作缓存 , 
*  groupKey:用于执行线程,
*  重要的是超时时间的配置, 
*  线程池的配置
*/
@HystrixCommand(
    fallbackMethod = "findPersonFallback", 
    groupKey = "PersonGroupKey", 
    commandKey = "PersonCommandKey", 
    commandProperties = { 
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") 
    }, 
    threadPoolProperties = { 
        @HystrixProperty(name = "coreSize", value = "2") 
    })
public Person findPerson(Integer personId) {
    Person person = restTemplate.getForObject(
        "http://atm-eureka-hystrix-provider/person/{personId}",
        Person.class, personId);
    return person;
}

2.2、全局配置

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;

@Service
@DefaultProperties(defaultFallback = "findPersonFallback")
public class PersonService {
}

// 默认的回退方法,是没有参数的
public Person findPersonFallback() {
    Person p = new Person();

    p.setId(10);
    p.setAge(1);
    p.setName("i am fallback!");

    return p;
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐