1. 程式人生 > >Spring Cloud_19_整合Hystrix/基本整合與配置(一)

Spring Cloud_19_整合Hystrix/基本整合與配置(一)

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

  • 基本整合與配置
  • 新建Maven專案:atm_eureka_hystrix_server
  • 新建Maven專案:atm_eureka_hystrix_provider
  • 新建Maven專案:atm_eureka_hystrix_invoker

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;
}