关于Feign和Hystrix断路器的概念知识,有兴趣的小伙伴们可以去https://blog.csdn.net/weixin_44364444/article/details/105969229看看。 pom.xml application.yml MicroserviceConsumerEmp80FeignApplication FeignClientEmpService.java ErrorFallback.java EmpController.java 上面使用到的是 fallback pom.xml application.yml MicroserviceConsumerEmp80FeignHystrixApplication IFeignClientEmpService.java EmpServiceFallBackFactory.java EmpController.java 这里使用的是 fallbackFactory 其他模块可以去https://blog.csdn.net/weixin_44364444/article/details/105955997看看 程序运行,可以看的负载均衡的这个服务器已经Hystrix 当刷新浏览器,服务器切换到其他,数据又会重新出来 控制会打印被 Hystrix的信息 关于Spring Cloud之Feign结合Hystrix断路器的两种方式就到这里了,有问题的小伙伴可以去前面的博客看,或者留言!!!
microservice-consumer-emp-80-feign
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.newer.demo</groupId> <artifactId>microservice-consumer-emp-80-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>microservice-consumer-emp-80-feign</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR4</spring-cloud.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-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
server: port: 80 #设置Eureka请求地址 eureka: client: service-url: defaultZone: https://eureka7001.com:7001/eureka/,https://eureka7002.com:7002/eureka/,https://eureka7003.com:7003/eureka/ #调整显示ip instance: instance-id: microservice-consumer-emp-80-feign prefer-ip-address: true #注册服务名称 spring: application: name: microservice-consumer-feign #启用hystrix feign: hystrix: enabled: true
package com.newer.demo.microservice.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class MicroserviceConsumerEmp80FeignApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceConsumerEmp80FeignApplication.class, args); } }
package com.newer.demo.microservice.consumer.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; /** * 定义Feign客户端接口 */ //指定服务名称 // fallback ,fallbackFactory 二选一 @FeignClient(value ="EmpService" ,fallback = ErrorFallback.class) public interface FeignClientEmpService { // 服务提供方映射地址 @GetMapping("/api/emps") public ResponseEntity<?> emps(); }
package com.newer.demo.microservice.consumer.service; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; /** * 1.编写类实现FeignClientEmpService接口 * 2.将该类装配成Bean */ @Component public class ErrorFallback implements FeignClientEmpService{ @Override public ResponseEntity<?> emps() { return new ResponseEntity<>("服务出现异常,将进行熔断", HttpStatus.OK); } }
package com.newer.demo.microservice.consumer.controller; import com.newer.demo.microservice.consumer.service.FeignClientEmpService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/api") public class EmpController { // 注入 @Resource private FeignClientEmpService feignClientEmpService; @GetMapping("/emps") public ResponseEntity<?> emps(){ return feignClientEmpService.emps(); } }
microservice-consumer-emp-80-feign-hystrix
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.newer.demo</groupId> <artifactId>microservice-consumer-emp-80-feign-hystrix</artifactId> <version>0.0.1-SNAPSHOT</version> <name>microservice-consumer-emp-80-feign-hystrix</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR4</spring-cloud.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-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
server: port: 80 #设置Eureka请求地址 eureka: client: service-url: defaultZone: https://eureka7001.com:7001/eureka/,https://eureka7002.com:7002/eureka/,https://eureka7003.com:7003/eureka/ #调整显示ip instance: instance-id: microservice-consumer-emp-80-feign-hystrix prefer-ip-address: true #注册服务名称 spring: application: name: microservice-consumer-feign #启用hystrix feign: hystrix: enabled: true
package com.newer.demo.microservice.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker @EnableFeignClients public class MicroserviceConsumerEmp80FeignHystrixApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceConsumerEmp80FeignHystrixApplication.class, args); } }
package com.newer.demo.microservice.consumer.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; /** * * */ @FeignClient(value = "EmpService",fallbackFactory =EmpServiceFallBackFactory.class ) public interface IFeignClientEmpService { @GetMapping("/api/emps") public ResponseEntity<?> emps(); }
package com.newer.demo.microservice.consumer.service; import feign.hystrix.FallbackFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @Component public class EmpServiceFallBackFactory implements FallbackFactory<IFeignClientEmpService> { //创建日志记录器实例 private static final Logger log= LoggerFactory.getLogger(EmpServiceFallBackFactory.class); @Override public IFeignClientEmpService create(Throwable throwable) { // 可以通过throwable得到异常信息 return new IFeignClientEmpService() { @Override public ResponseEntity<?> emps() { log.error("大事不好了,他跑路了",throwable); return new ResponseEntity<>("服务结束", HttpStatus.INTERNAL_SERVER_ERROR); } }; } }
package com.newer.demo.microservice.consumer.controller; import com.newer.demo.microservice.consumer.service.IFeignClientEmpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * */ @RestController @RequestMapping("/api") public class EmpController { @Autowired private IFeignClientEmpService feignClientEmpService; @GetMapping("/emps") public ResponseEntity<?> emps(){ return feignClientEmpService.emps(); } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算