上一个例程我们配置如何在Feign中使用Hystrix,如果不使用Feign如何使用Hystrix,需要使用注解@HystrixCommand
参考下面例程:
POM.xml
4.0.0
com.test
eureka-server
war
0.0.1-SNAPSHOT
eureka-server Maven Webapp
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8110
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8110/eureka/
security.user.name=chenqx
security.user.password=123
启动类
package com.test.eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EureKaServer
{
public static void main( String[] args )
{
SpringApplication.run(EureKaServer.class, args);
}
}
POM.xml
4.0.0
com.test
eureka-service-order
war
0.0.1-SNAPSHOT
eureka-service-order Maven Webapp
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=9100
spring.application.name=service-order
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
服务实现类
package com.test.eureka_service_order;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderServiceApiImpl{
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
public String hello(@RequestParam("name") String name) {
System.out.println("name="+name);
return "hello " + name;
}
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
public OrderInfo hello(@RequestParam("name") String name,
@RequestParam("prdtName") String prdtName) {
System.out.println("hello2 name="+name+",prdtName="+prdtName);
try {
name= URLDecoder.decode(name,"UTF-8");
prdtName= URLDecoder.decode(prdtName,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new OrderInfo(name, prdtName,1,1,null,null);
}
@RequestMapping(value = "/hello3", method = RequestMethod.GET)
public String hello(@RequestBody OrderInfo order) {
if (order == null) {
return "未知";
}
return order.toString();
}
}
实体类
package com.test.eureka_service_order;
import java.io.Serializable;
import java.sql.Date;
public class OrderInfo implements Serializable {
private static final long serialVersionUID = -7233238826463139634L;
private Long id;
private String name;
private String prdtName;
private Integer count;
private Integer amount;
private Date createDt = new Date(System.currentTimeMillis());
private String userId = null;
public OrderInfo() {
}
public OrderInfo(String name,String prdtName,Integer count,Integer amount,Date createDt,String userId) {
this.name = name;
this.prdtName = prdtName;
this.count = count;
this.amount = amount;
this.createDt = createDt;
this.userId = userId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrdtName() {
return prdtName;
}
public void setPrdtName(String prdtName) {
this.prdtName = prdtName;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
",prdtName=" + prdtName +
",count=" + count +
",amount=" + amount +
'}';
}
}
启动类
package com.test.eureka_service_order;
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;
@EnableDiscoveryClient
@SpringBootApplication
public class OrderServer
{
public static void main( String[] args )
{
SpringApplication.run(OrderServer.class, args);
}
}
POM.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
com.test
eureka-service-web
0.0.1-SNAPSHOT
jar
eureka-service-web
http://maven.apache.org
UTF-8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8887
spring.application.name=order-service-web
eureka.instance.hostname=localhost
eureka.instance.server.port=8110
eureka.client.serviceUrl.defaultZone=http://chenqx:123@${eureka.instance.hostname}:${eureka.instance.server.port}/eureka/
eureka.client.register-with-eureka=true
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
feign.hystrix.enabled=true
Controller类
package com.test.eureka_service_web;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class OrderController {
@Autowired
@Qualifier(value="restTemplate")
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod="hellofallback")
@RequestMapping(value="/hello2",method=RequestMethod.GET)
public String hello6(String name,String prdtName)
{
String url = "http://SERVICE-ORDER/hello2?name="+name+"&prdtName="+prdtName;
return restTemplate.getForEntity(url, String.class).getBody();
}
public String hellofallback(String name,String prdtName)
{
return "FallBack invoke...name="+name+",prdtName="+prdtName;
}
}
package com.test.eureka_service_web;
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.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import feign.Logger;
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderClient
{
@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderClient.class, args);
}
}
依次启动注册中心,服务端程序,客户端程序
在浏览器中输入地址http://localhost:8887/hello2?name=1&prdtName=Java
关闭服务端程序,再次刷新页面
在客户端程序的POM.xml中添加依赖
org.springframework.boot
spring-boot-starter-actuator
重新启动客户端程序
通过浏览器输入http://localhost:8887/hystrix.stream
添加Dashboard依赖
在客户端程序的POM.xml中添加依赖
org.springframework.cloud
spring-cloud-starter-hystrix-dashboard
重新启动客户端程序
通过浏览器输入http://localhost:8887/hystrix
在输入框中输入http://localhost:8887/hystrix.stream