zoukankan      html  css  js  c++  java
  • springcloud8----feign-with-hystrix

    Feign也可以使用Hystrix

    package com.itmuch.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    @EnableCircuitBreaker
    public class ConsumerMovieFeignApplication {
      public static void main(String[] args) {
        SpringApplication.run(ConsumerMovieFeignApplication.class, args);
      }
    }
    package com.itmuch.cloud.controller;
    
    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 com.itmuch.cloud.entity.User;
    import com.itmuch.cloud.feign.UserFeignClient;
    
    @RestController
    public class MovieController {
    
      @Autowired
      private UserFeignClient userFeignClient;
    
      @GetMapping("/movie/{id}")
      public User findById(@PathVariable Long id) {
        return this.userFeignClient.findById(id);
      }
    }
    package com.itmuch.cloud.entity;
    
    import java.math.BigDecimal;
    
    public class User {
      private Long id;
    
      private String username;
    
      private String name;
    
      private Short age;
    
      private BigDecimal balance;
    
      public Long getId() {
        return this.id;
      }
    
      public void setId(Long id) {
        this.id = id;
      }
    
      public String getUsername() {
        return this.username;
      }
    
      public void setUsername(String username) {
        this.username = username;
      }
    
      public String getName() {
        return this.name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public Short getAge() {
        return this.age;
      }
    
      public void setAge(Short age) {
        this.age = age;
      }
    
      public BigDecimal getBalance() {
        return this.balance;
      }
    
      public void setBalance(BigDecimal balance) {
        this.balance = balance;
      }
    
    }
    package com.itmuch.cloud.feign;
    
    import org.springframework.stereotype.Component;
    
    import com.itmuch.cloud.entity.User;
    
    @Component
    public class HystrixClientFallback implements UserFeignClient {
    
      @Override
      public User findById(Long id) {
        User user = new User();
        user.setId(0L);
        return user;
      }
    }
    package com.itmuch.cloud.feign;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.itmuch.cloud.entity.User;
    
    @FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)
    public interface UserFeignClient {
      @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
      public User findById(@PathVariable("id") Long id);
    }
    spring:
      application:
        name: microservice-consumer-movie-feign-with-hystrix
    server:
      port: 7901
    eureka:
      client:
        healthcheck:
          enabled: true
        serviceUrl:
          defaultZone: http://user:password123@localhost:8761/eureka
      instance:
        prefer-ip-address: true
    <?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>
    
        <artifactId>microservice-consumer-movie-feign-with-hystrix</artifactId>
        <packaging>jar</packaging>
    
        <parent>
            <groupId>com.itmuch.cloud</groupId>
            <artifactId>microservice-spring-cloud</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.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-eureka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
        </dependencies>
    </project>
  • 相关阅读:
    POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
    HDU 3037 Saving Beans(Lucas定理模板题)
    HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
    Java进阶(二)当我们说线程安全时,到底在说什么
    UML建模一般流程
    Spring事务管理(详解+实例)
    Spring 的 BeanPostProcessor接口实现
    Spring中ApplicationContext和beanfactory区别
    使用Maven Release插件发布项目
    tair与redis比较总结
  • 原文地址:https://www.cnblogs.com/yaowen/p/9142213.html
Copyright © 2011-2022 走看看