參考:http://blog.csdn.net/forezp/article/details/69934399
1.ribbon斷路器
pom.xml加入依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
改造ribbon項目。
改造Ribbon.java:
package ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class Ribbon {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Ribbon.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
改造RibbonService.java:
package ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "Service0Error")
public String Service0(String name) {
return restTemplate.getForObject("http://SERVICE0/service0/" + name, String.class);
}
public String Service0Error(String name) {
return "Service0Error:"+name+",error!";
}
}
啓動discovery,service0,ribbon,訪問以下地址http://127.0.0.1:8085/service0/qq:
關閉service0,刷新瀏覽器:
添加監控儀表盤,改造Ribbon.java:
package ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class Ribbon {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Ribbon.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
訪問地址:http://127.0.0.1:8085/hystrix,效果如下:
2.Feign斷路器
改造service1項目。
改造Server0Client.java:
package service1.client;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value="service0",fallback = Server0ClientHystric.class) //service0的application中的name屬性一致,採用FeignClient會自動負載均衡
public interface Server0Client {
@RequestMapping(method=RequestMethod.GET,path="/service0/{value}")
public String service0(@PathVariable("value") String value);//與service0controller的方法一致
}
添加類Server0ClientHystric.java:
package service1.client;
import org.springframework.stereotype.Component;
@Component
public class Server0ClientHystric implements Server0Client{
@Override
public String service0(String value) {
// TODO Auto-generated method stub
return "sorry "+value;
}
}
啓動discovery,service1,訪問http://127.0.0.1:8082/service1/qq,這個時候service0還沒有啓動,所以service1調用不到,會出錯,提示如下:
說明斷路器工作正常!
啓動service0,訪問http://127.0.0.1:8082/service1/qq,正常訪問如下: