美文网首页
Spring Cloud Hystrix 入门

Spring Cloud Hystrix 入门

作者: 歌哥居士 | 来源:发表于2019-03-30 12:34 被阅读0次

添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.baozi</groupId>
    <artifactId>cloud-hystrix</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
      <module>hystrix-registration</module>
      <module>hystrix-provider</module>
      <module>hystrix-consumer</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-hystrix</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>hystrix-registration</artifactId>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-hystrix</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>hystrix-provider</artifactId>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-hystrix</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>hystrix-consumer</artifactId>

</project>

注册中心

spring.application.name=hystrix-registration
server.port=9000

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class HystrixRegistrationApplication {

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

}

服务提供者

spring.application.name=hystrix-provider
server.port=10000

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class HystrixProviderApplication {

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

}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @GetMapping("/m1")
    public String m1() throws InterruptedException {
        // 模拟超时
        Thread.sleep(5000);
        return "m1";
    }

    @GetMapping("/m2")
    public String m2() throws InterruptedException {
        Thread.sleep(5000);
        return "m2";
    }

    @GetMapping("/m3/{id}")
    public String m3(@PathVariable String id) throws InterruptedException {
        Thread.sleep(5000);
        return "m3: " + id;
    }

}

服务消费者

spring.application.name=hystrix-consumer
server.port=11000

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringCloudApplication // 等于下面三个
//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker
public class HystrixConsumerApplication {

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

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

}
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;


    @HystrixCommand(fallbackMethod = "m1fallback")
    @GetMapping("/m1")
    public String m1() { // 方法修饰符必须是public,否则会抛出null
        return restTemplate.getForObject("http://hystrix-provider/m1", String.class);
    }


    @HystrixCommand(
            fallbackMethod = "m2fallback",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000"),
                    // 指定隔离策略
                    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")},
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"),
                    @HystrixProperty(name = "maxQueueSize", value = "10")}
    )
    @GetMapping("/m2")
    public String m2() {
        return restTemplate.getForObject("http://hystrix-provider/m2", String.class);
    }


    @HystrixCommand(fallbackMethod = "m3fallback")
    @GetMapping("/m3/{id}")
    public String m3(@PathVariable String id) {
        return restTemplate.getForObject("http://hystrix-provider/m3/{id}", String.class, id);
    }


    // ~服务降级。这些方法可以是private
    // ------------------------------------------

    private String m1fallback() {
        return "m1fallback";
    }

    private String m2fallback() {
        return "m2fallback";
    }

    // 参数要一致,否则找不到
    private String m3fallback(String id) {
        return "m3fallback: " + id;
    }
}

相关文章

网友评论

      本文标题:Spring Cloud Hystrix 入门

      本文链接:https://www.haomeiwen.com/subject/kbthvqtx.html