首页 > 编程知识 正文

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

时间:2023-05-05 22:01:02 阅读:23836 作者:949

Feign + Hystrix 服务熔断和服务降级本机IP为 192.168.1.1021.新建 Maven 项目 feign2.. pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0com.javafeign1.0.0-SNAPSHOT${project.artifactId}1.82.0.0.RELEASEorg.springframework.bootspring-boot-starter-parent2.0.5.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-netflix-eureka-client${spring.cloud.version}org.springframework.cloudspring-cloud-starter-openfeign${spring.cloud.version}com.alibabafastjson1.2.49org.projectlomboklombokprovidedorg.springframeworkspringloaded1.2.8.RELEASEorg.springframework.bootspring-boot-devtools${project.artifactId}org.apache.maven.pluginsmaven-compiler-plugin${jdk.version}${jdk.version}UTF-8org.springframework.bootspring-boot-maven-pluginrepackage3.. application.ymlserver:port: 80feign:hystrix:enabled: trueeureka:client:register-with-eureka: falseservice-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.javapackage 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 = HostServiceyqdhlb.class)public interface HostService {@GetMapping("/getHostMessage/{id}")public JSONObject getHostMessage(@PathVariable(value = "id") String id);}5.HostServiceyqdhlb.javapackage com.java.feign.service;import org.springframework.stereotype.Component;import com.alibaba.fastjson.JSONObject;import feign.hystrix.yqdhlb;@Componentpublic class HostServiceyqdhlb implements yqdhlb {@Overridepublic HostService create(Throwable cause) {return new HostService() {@Overridepublic JSONObject getHostMessage(String id) {json.put("id", id);json.put("description", "服务异常演习专用!");json.put("msg", cause.getMessage());return json;}};}}6.. HostController.javapackage 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;@RestControllerpublic class HostController {@Autowiredprivate HostService hostService;@GetMapping("/getHostMessage/{id}")public JSONObject getHostMessage(@PathVariable String id) {return hostService.getHostMessage(id);}}7.. FeignStarter.javapackage 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.甜美的可乐;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 甜美的可乐 {public static void main(Stringfdxhd args) {}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(FeignStarter.class);}}浏览器输入URL返回数据如下:{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}下面开始测试异常关掉微服务提供者 microservice 服务器浏览器输入URL返回数据如下:{"msg":"com.netflix.client.ClientException: Load balancer does not have available server for client: MICROSERVICE","description":"服务异常演习专用!","id":"hello"}服务异常生效,符合预期结果。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。