Kaynağa Gözat

新增断路器

chenweida 7 yıl önce
ebeveyn
işleme
af56d3f353

+ 4 - 4
web-gateway/pom.xml

@ -67,9 +67,9 @@
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-zuul</artifactId>-->
        <!--</dependency>-->
    </dependencies>
</project>

+ 1 - 1
web-gateway/src/main/java/com/yihu/jw/WebGateWayAppliaction.java

@ -23,7 +23,7 @@ import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient//服务注册到发现服务
@EnableHystrix //启动断路器
@EnableHystrixDashboard //启动Hystrix仪表盘(监控数据)
@EnableZuulProxy //启动zuul代理 路由
//@EnableZuulProxy //启动zuul代理 路由
@EnableFeignClients //声名式的客户端
@EnableCircuitBreaker
public class WebGateWayAppliaction {

+ 11 - 1
web-gateway/src/main/java/com/yihu/jw/controller/PatientController.java

@ -6,6 +6,8 @@ import com.yihu.jw.fegin.PatientFegin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -19,6 +21,7 @@ import org.springframework.web.context.annotation.SessionScope;
@RequestMapping("/rest/patient")
@Api(description = "患者")
public class PatientController {
    private Logger logger= LoggerFactory.getLogger(PatientController.class);
    @Autowired
    private PatientFegin patientFegin;
@ -27,9 +30,16 @@ public class PatientController {
    //配置HystrixProperty 则调用的方法和fallback是同一个线程 否则就不是
    //@HystrixCommand(fallbackMethod = "findByCodeFallback",commandProperties = @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"))
//   @HystrixCommand(fallbackMethod = "findByCodeFallback" )
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "-1"),//超时时间
            @HystrixProperty(name = "execution.timeout.enabled", value = "false") })
    public String findByCode(
            @ApiParam(name = "code", value = "患者code", required = true) @RequestParam(value = "code", required = true) String code) {
        return patientFegin.findByCode(code);
        logger.info("start");
        String text1 =patientFegin.findByCode(code);
        logger.info("text");
        String text2 =patientFegin.findByCode(code);
        return text1+text2;
    }
//    /**

+ 2 - 2
web-gateway/src/main/java/com/yihu/jw/fegin/fallback/PatientFeginFallback.java

@ -10,13 +10,13 @@ import org.springframework.web.bind.annotation.RequestParam;
/**
 * Created by chenweida on 2017/5/13.
 */
//@Component
@Component
public class PatientFeginFallback implements PatientFegin {
    private Logger logger = LoggerFactory.getLogger(PatientFeginFallback.class);
    @Override
    public String findByCode(String code) {
    public String findByCode( @RequestParam(value = "code", required = true) String code) {
        logger.info("进入断路器");
        return "断路器启动";
    }

+ 4 - 5
web-gateway/src/main/resources/application.yml

@ -3,12 +3,11 @@ spring:
  application:
    name:  web-gateway  ##注册到发现服务的id 如果id一样 eurika会自动做负载
##开启feign断路器
feign:
  hystrix:
    enabled: true
##断路器
#hystrix:
#  metrics:
#    enabled: true
#    polling-interval-ms: 20000 ##熔断间隔时间 默认是2秒
#zuul 默认会代理所有的微服务  路径 /{appliction.name}/**