美文网首页
Hystrix应用

Hystrix应用

作者: 长孙俊明 | 来源:发表于2019-10-08 21:51 被阅读0次
    ###服务启动端口号
    server:
       port: 8001
    ###服务名称(服务注册到eureka名称)
    spring:
       application:
           name: test-configClient #如果配置了Config,该名称必须跟Git上的配置文件前缀一致。如:test-configClient-prd.properties
       cloud:
         config:
           # 读取版本环境,也就是配置文件的后缀名。如:test-configClient-prd.properties
           profile: prd
           discovery:
             # 就是读取configserver服务,写成configserver的spring.application.name
             service-id: config-server
             enabled: true
    ###服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8100/eureka
    ribbon:
      ReadTimeout: 6000
      ConnectTimeout: 6000
    
    # 禁止服务超时时间
    hystrix:
      command:
        default:
          execution:
            timeout:
              enabled: true
    
    # feign开启hystrix配置
    feign:
      hystrix:
        enabled: true
    
    package com.springcloudtest.order.api.fallback;
    
    import com.springcloudtest.order.api.service.MemberService;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DefaultMemberService implements MemberService {
        @Override
        public String getMember() {
            return "以类的方式,连接超时。";
        }
    
    }
    
    package com.springcloudtest.order.api.service;
    
    import com.springcloudtest.order.api.fallback.DefaultMemberService;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @FeignClient(name="app-member", fallback=DefaultMemberService.class)
    public interface MemberService {
        @RequestMapping("/geteMember")
        public String getMember();
    }
    
    
    package com.springcloudtest.order.api;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.springcloudtest.order.api.service.MemberService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RefreshScope
    public class OrderController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @Autowired
        private MemberService memberService;
    
        // 读取ConfigServer服务上的值
        @Value("${dburl}")
        private String dburl;
    
        @RequestMapping("/getOrder")
        @HystrixCommand(fallbackMethod = "getErrorTip")
        public String getOrder() {
            System.out.println("orderToMemberUserInfoHystrix:" + "线程池名称:" + Thread.currentThread().getName());
           // return restTemplate.getForObject("http://APP-MEMBER/geteMember", String.class);
            return memberService.getMember();
        }
    
        // Hystrix以类方式提示超时
        @RequestMapping("/getOrderWithClass")
        public String getOrderWithClass() {
            System.out.println("getOrderWithClass:" + "线程池名称:" + Thread.currentThread().getName());
            // return restTemplate.getForObject("http://APP-MEMBER/geteMember", String.class);
            return memberService.getMember();
        }
    
        @RequestMapping("/getDburl")
        public String getDburl() {
            return dburl;
        }
    
        public String getErrorTip() {
            return "访问超时,请稍后重试。";
        }
    }
    
    <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-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                <version>2.1.3.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-client</artifactId>
            </dependency>
            <!-- actuator 监控中心 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    

    相关文章

      网友评论

          本文标题:Hystrix应用

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