java服务熔断_Feign + Hystrix 服务熔断和服务降级

Feign + Hystrix 服务熔断和服务降级

三丰 soft张三丰

3ff1c6f43578cf29546aa728dea211da.png

本机IP为 192.168.1.102

1.新建 Maven 项目 feign

2.. pom.xml

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">

4.0.0

com.java

feign

1.0.0-SNAPSHOT

${project.artifactId}

1.8

2.0.0.RELEASE

org.springframework.boot

spring-boot-starter-parent

2.0.5.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

${spring.cloud.version}

org.springframework.cloud

spring-cloud-starter-openfeign

${spring.cloud.version}

com.alibaba

fastjson

1.2.49

org.projectlombok

lombok

provided

org.springframework

springloaded

1.2.8.RELEASE

org.springframework.boot

spring-boot-devtools

${project.artifactId}

org.apache.maven.plugins

maven-compiler-plugin

${jdk.version}

${jdk.version}

UTF-8

org.springframework.boot

spring-boot-maven-plugin

repackage

3.. application.yml

server:

port: 80

feign:

hystrix:

enabled: true

eureka:

client:

register-with-eureka: false

service-url:

defaultZone: http://192.168.1.102:8080/eureka/

#defaultZone: http://s0.com:8080/eureka/,http://s1.com:8080/eureka/,http://s2.com:8080/eureka/

4.. HostService.java

package com.java.feign.service;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import com.alibaba.fastjson.JSONObject;

@FeignClient(value = "MICROSERVICE", fallbackFactory = HostServiceFallbackFactory.class)

public interface HostService {

@GetMapping("/getHostMessage/{id}")

public JSONObject getHostMessage(@PathVariable(value = "id") String id);

}

5.HostServiceFallbackFactory.java

package com.java.feign.service;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import feign.hystrix.FallbackFactory;

@Component

public class HostServiceFallbackFactory implements FallbackFactory {

@Override

public HostService create(Throwable cause) {

return new HostService() {

@Override

public JSONObject getHostMessage(String id) {

JSONObject json = new JSONObject();

json.put("id", id);

json.put("description", "服务异常演习专用!");

json.put("msg", cause.getMessage());

return json;

}

};

}

}

6.. HostController.java

package com.java.feign.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.alibaba.fastjson.JSONObject;

import com.java.feign.service.HostService;

@RestController

public class HostController {

@Autowired

private HostService hostService;

@GetMapping("/getHostMessage/{id}")

public JSONObject getHostMessage(@PathVariable String id) {

return hostService.getHostMessage(id);

}

}

7.. FeignStarter.java

package com.java.feign;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.openfeign.EnableFeignClients;

import org.springframework.context.annotation.ComponentScan;

@EnableEurekaClient

@SpringBootApplication

@EnableFeignClients(basePackages = { "com.java.feign.service" })

@ComponentScan(basePackages = { "com.java.feign" })

public class FeignStarter extends SpringBootServletInitializer {

public static void main(String[] args) {

SpringApplication.run(FeignStarter.class, args);

}

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

return builder.sources(FeignStarter.class);

}

}

运行测试

启动 Feign服务,运行 FeignStarter.java

浏览器输入URL

返回数据如下:

{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}

截图如下:

4e64a50a0e4630ddb1370637b8813919.png

搭建成功,程序正常运行。

下面开始测试异常

关掉微服务提供者 microservice 服务器

浏览器输入URL

返回数据如下:

{"msg":"com.netflix.client.ClientException: Load balancer does not have available server for client: MICROSERVICE","description":"服务异常演习专用!","id":"hello"}

截图如下:

9d83b583fd511344ce6a1bd2ef7c9202.png

服务异常生效,符合预期结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值