springCloud简单Demo(二)

一、Hystrix断路器

原理:
在这里插入图片描述

package cn.tedu.controller;

import cn.tedu.feign.HelloFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//不做业务 去调用服务的提供者
public class HelloController {
@Autowired
private HelloFeign helloFeign;
@RequestMapping("/hello/{name}")
@HystrixCommand(fallbackMethod = "fllbackHello")
public String hello(@PathVariable String name){
return helloFeign.hello(name);
}



//断路器方法,返回值要和方法一致 参数也一致
public String fllbackHello(String name){
return "tony";//当发生异常时,直接返回默认值 这种形式成为 降级
}
}

其他配置和消费者工程相同

二、网关zuul

*1、zuul实现API网关

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

</dependencies>
@SpringBootApplication
@EnableZuulProxy
public class RunAppZuul {
public static void main(String[] args) {
SpringApplication.run(RunAppZuul.class,args);
}
}
application.yml
zuul:
routes:
app-a:
path:/user/**
serviceId:consumer-hystrix
*

2.zuul实现断路器

实现服务合并、转发、验证、 异构协议

一、zuul实现API网关


org.springframework.cloud
spring-cloud-starter-eureka

org.springframework.cloud spring-cloud-starter-zuul

@SpringBootApplication
@EnableZuulProxy
public class RunAppZuul {
public static void main(String[] args) {
SpringApplication.run(RunAppZuul.class,args);
}
}

application.yml
zuul:
routes:
app-a:
path:/user/**
serviceId:consumer-hystrix

二、zuul实现断路器

package cn.tedu.fallback;

import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Component //zuul环境中启动它作为一个断路器
public class HelloFallback implements ZuulFallbackProvider {

@Override
public String getRoute() {
return "*";
}

@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse() {
@Override//状态码
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST;
}

@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST.value();
}

@Override //状态文字描述
public String getStatusText() throws IOException {
return HttpStatus.BAD_REQUEST.getReasonPhrase();
}

@Override
public void close() {

}

@Override //返回内容
public InputStream getBody() throws IOException {
String msg = "tonyzuul"; //可以构建对象Json
return new ByteArrayInputStream(msg.getBytes());
}

@Override //头信息
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
};
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值