
2.Spring Cloud Hystrix 简介
2.application.yml配置文件
- server:
- port: 8080
- spring:
- application:
- name: register-service
- eureka:
- client:
- service-url:
- defaultZone: http://192.168.174.133:8761/eureka
- instance:
- hostname: localhost
- instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
3.创建一个controller类
- package com.it.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class RegisterController {
-
- @GetMapping("register")
- public String register(){
- return "注册成功";
- }
-
- }
4.启动主函数类
- package com.it;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- @SpringBootApplication
- @EnableEurekaClient
- public class RegisterService02Application {
-
- public static void main(String[] args) {
- SpringApplication.run(RegisterService02Application.class, args);
- }
-
- }
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
- dependency>
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.3.12.RELEASEversion>
- <relativePath/>
- parent>
- <groupId>com.itgroupId>
- <artifactId>customer-service-1artifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>customer-service-1name>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- <spring-cloud.version>Hoxton.SR12spring-cloud.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-dependenciesartifactId>
- <version>${spring-cloud.version}version>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
2.application.yml
这里不同的配置是需要开启hystrix
- server:
- port: 8081
- spring:
- application:
- name: customer-service
- eureka:
- client:
- service-url:
- defaultZone: http://192.168.174.133:8761/eureka
- instance:
- hostname: localhost
- instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
- feign:
- hystrix:
- enabled: true #在springcloud的F版本以前是默认开启的
3.创建CustomerRegisterFeign接口
@FeignClient需要指定访问失败的路径,即熔断路径
- package com.it.feign;
-
- import com.it.feign.hystrix.CustomerRegisterFeignHystrix;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
-
- /**
- * 这里需要指定熔断类
- */
- @FeignClient(value = "register-service",fallback = CustomerRegisterFeignHystrix.class)
- public interface CustomerRegisterFeign {
-
- @GetMapping("register")
- public String register();
-
- }
4.CustomerRegisterFeignHystrix熔断类,实现CustomerRegisterFeign接口
- package com.it.feign.hystrix;
-
- import com.it.feign.CustomerRegisterFeign;
- import org.springframework.stereotype.Component;
- //这里需要加入ioc容器的注解
- @Component
- public class CustomerRegisterFeignHystrix implements CustomerRegisterFeign {
- @Override
- public String register() {
- return "我是备选方案!";
- }
- }
5.CustomerController类
进行远程调用
- package com.it.controller;
-
- import com.it.feign.CustomerRegisterFeign;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.annotation.Resource;
-
- @RestController
- public class CustomerController {
-
- @Resource
- private CustomerRegisterFeign customerRegisterFeign;
-
- @GetMapping("customer")
- public String customer(){
- System.out.println("用户进行账号注册");
- //RPC
- String register = customerRegisterFeign.register();
- System.out.println(register);
- return register;
- }
-
- }
6.主函数启动类
- package com.it;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.cloud.openfeign.EnableFeignClients;
-
- @SpringBootApplication
- @EnableEurekaClient
- @EnableFeignClients
- public class CustomerService1Application {
-
- public static void main(String[] args) {
- SpringApplication.run(CustomerService1Application.class, args);
- }
-
- }
7.测试
7.1关闭register-service,只启动该项目查看是否会发生熔断,进入熔断类中的方法

结论:成功进入
7.2打开register-service,查看该项目是否会正常的发起远程调用

结论:正常发起远程调用