Hystrix基本用法 |
新建一个统一管理工程 |
对应pom依赖:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yb</groupId>
<artifactId>microservicedemo</artifactId>
<version>1.0</version>
</parent>
<artifactId>consumer-order-feign-custom</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
eureka(注册中心)项目相关代码:
pom依赖:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yb</groupId>
<artifactId>microservicedemo</artifactId>
<version>1.0</version>
</parent>
<groupId>com.eureka.yb</groupId>
<artifactId>eureka</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- eurekaserver 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<!-- <version>1.4.3.RELEASE</version> -->
</dependency>
<!-- 安全依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
EurekaApp类代码:
package com.eureka.yb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
}) //排除安全配置,否则登录需要密码
@EnableEurekaServer // 声明这是一个Eureka Server
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class);
}
}
application配置:
server:
port: 10000 #注册服务中心的端口号
eureka:
instance:
hostname: localhost
# prefer-ip-address: true
client:
register-with-eureka: false # 是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设为false
fetch-registry: false # 表示是否从Eureka Sever获取注册信息,默认为true。因为这一个单节点就是Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
# defaultZone: http://user:123@${eureka.instance.hostname}:${server.port}/eureka #curl 风格 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
#security:
# basic:
# enabled: true #开启安全配置,也就是需要密码,如果不需要密码设置为false即可,注意这个参数必须放在application.yml中,不允许放在bootstrap.yml
# user:
# name: user
# password: 123 #在配置了用户名密码后,我们可以修改地址的访问风格为 curl 风格
provider-user(服务提供者)工程:
pom:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yb</groupId>
<artifactId>microservicedemo</artifactId>
<version>1.0</version>
</parent>
<artifactId>provider-user</artifactId>
<!-- <version>1.0</version> -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
application配置:
eureka:
client:
service-url:
#http://localhost:8761/eureka/(即注册到的服务地址)
defaultZone: http://localhost:10000/eureka
# defaultZone: http://localhost:8762/eureka
# instance:
# # 将自己的IP注册到Eureka Server。若不配置或设置为false,表示注册微服务所在操作系统的hostname到Eureka Server
# prefer-ip-address: true
server:
port: 7900 #程序启动后的端口,也就是tomcat的端口,我们可以自己定义
spring:
application:
# 指定注册到Eureka Server上的应用名称
name: provider-user
Provider类:
package com.yb.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient//启用 eureka 客户端
public class Provider {
public static void main(String[] args) {
SpringApplication.run(Provider.class);
}
}
UserController类:
package com.yb.provider.springcloud.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.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.yb.provider.springcloud.pojo.User;
@RestController
public class UserController {
@Autowired
private EurekaClient eurekaClient;
@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) {
return new User(id);
}
@GetMapping("/info")
public String info() {
InstanceInfo instance = eurekaClient.getNextServerFromEureka("PROVIDER-USER", false);
return instance.getHomePageUrl();
}
@GetMapping("/get-user")
public User getUser(User user) {
return user; //相当于传递一个复杂参数会被封装成user对象,然后将封装对象返回
}
}
User类:
package com.yb.provider.springcloud.pojo;
import java.util.Date;
public class User {
private Long id;
private Date date;
public User(Long id) {
this.id = id;
this.date = new Date();
}
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
consumer-order-hystrix(消费者,熔断)项目相关代码:
pom依赖:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yb</groupId>
<artifactId>microservicedemo</artifactId>
<version>1.0</version>
</parent>
<artifactId>consumer-order-hystrix</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
application配置:
eureka:
client:
service-url:
#http://localhost:8761/eureka/(即注册到的服务地址)
defaultZone: http://localhost:10000/eureka
# instance:
# 将自己的IP注册到Eureka Server。若不配置或设置为false,表示注册微服务所在操作系统的hostname到Eureka Server,true则为ip
# prefer-ip-address: false
server:
port: 8900
spring:
application:
name: consumer-order-hystrix
user:
url: http://localhost:7900/user/
OrderConsumer(启动熔断类):
package com.yb.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient//启用 eureka 客户端
@EnableCircuitBreaker//启用熔断
public class OrderConsumer {
public static void main(String[] args) {
SpringApplication.run(OrderConsumer.class);
}
@Bean // 把rest模板注册到bean容器,相当于xml中bean标签,主要用于调用当前方法获取到指定对象
// @LoadBalanced//支持负载均衡
RestTemplate restTemplate() {
return new RestTemplate();
}
}
OrderController:
package com.yb.consumer.springcloud;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yb.consumer.springcloud.pojo.User;
@RestController
public class OrderController {
@Autowired
private EurekaClient eurekaClient;
@Autowired
private RestTemplate restTemplate;//spring 提供的一个用于访问 rest 接口的模板对象
@Value("${user.url}")
private String url;
// private String url = "http://localhost:7900/user/";
@GetMapping("/order/{id}")
@HystrixCommand(fallbackMethod = "fail")//请求失败的回调方法
// @HystrixCommand(fallbackMethod = "fail",
// commandProperties = {
// @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
// })//请求失败的回调方法,其中execution.isolation.strategy 默认不建议修改,如果遇到问题再修改,否则不建议修改
public User getOrder(@PathVariable Long id) {
InstanceInfo instance = eurekaClient.getNextServerFromEureka("PROVIDER-USER", false);
String homePageUrl = instance.getHomePageUrl();//从注册中心获取对应服务地址
//访问提供者获取数据
return restTemplate.getForObject(homePageUrl+"/user/"+id,User.class);//通过访问rest获取到json数据,然后转换为user对象
}
/**
* 失败后执行回调
* @param id
* @return
*/
public User fail(Long id) {
User user = new User();
user.setDate(new Date());
user.setId(-100L);
return user;
}
}
User类:
package com.yb.provider.springcloud.pojo;
import java.util.Date;
public class User {
private Long id;
private Date date;
public User(Long id) {
this.id = id;
this.date = new Date();
}
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}