Spring Cloud

微服务理论

SpringCloud

1.概述

  • 基于SpringBoot的微服务架构开发工具
为微服务架构中涉及的服务治理、断路器、负载均衡、配置管理、控制总线和集群状态管理等操作提供了一种简单的开发方式
  • 与SpringBoot的区别
    在这里插入图片描述
  • Rest风格
请求:http://localhost:8080/product/get/1
原来写法:http://localhost:8080/product?pid=1

2.生态简介

  • 外部或者内部的非SpringCloud项目都统一通过API网关(Zuul)来访问内部服务
  • 网关接收到请求后,从注册中心(Eureka)获取可用服务
  • Ribbon均衡负载后,分发到后端的具体实例
  • 微服务之间通过Feign进行通信处理业务
  • Hystrix负责处理服务超时熔断
  • Turbine监控服务间的调用和熔断相关指标

HelloSpringCloud

实现通信

1.接受请求(rest风格)

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductBiz productBiz;

	//1.映射地址传入参数
    @RequestMapping("/get/{pid}")
    public JsonModel get(@PathVariable("pid") Integer pid, JsonModel jsonModel) {
        Product product = this.productBiz.findById(pid);
        jsonModel.setCode(1);
        jsonModel.setData(product);
        return jsonModel;
    }

	//2.input表单接收参数
    @RequestMapping("/add")
    public JsonModel add(@RequestBody Product product, JsonModel jsonModel) {
        this.productBiz.add(product);
        jsonModel.setCode(1);
        return jsonModel;
    }

	//3.不接收参数
    @RequestMapping("/list")
    public JsonModel list(JsonModel jsonModel) {
        List<Product> list = this.productBiz.findByList();
        jsonModel.setCode(1);
        jsonModel.setData(list);
        return jsonModel;
    }
}

2.发送请求(消费端)到另一个地址获取响应

  • JDK原生的URLConnection
private final String BASE_URL_GET = "http://localhost:8081/product/get/";

@RequestMapping("/product/get/{pid}")
public JsonModel get(@PathVariable("pid") Integer pid, JsonModel jsonModel) throws IOException {
    String url = BASE_URL_GET + pid;
    jsonModel = sendRequest(url);
    return jsonModel;
}

private JsonModel sendRequest(String urlstr) throws IOException {
 JsonModel jsonModel = new JsonModel();
    String result = "";
    StringBuffer sb = new StringBuffer();
    URL url = new URL(urlstr);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    InputStream iis = con.getInputStream();
    byte[] bs = new byte[100 * 1024];
    int length = -1;
    while ((length = iis.read(bs, 0, bs.length)) != -1) {
        result = new String(bs, 0, length);
        sb.append(result);
    }
    Gson g = new Gson();
    jsonModel = g.fromJson(sb.toString(), JsonModel.class);
    iis.close();
    return jsonModel;
}
  • Spring封装好的RestTemplate对象(最易)
//配置类
@Configuration
public class WebConfigs {
    @Bean
    public RestTemplate  restTemplate(){
        return new RestTemplate();
    }
}
------------------------------------------------
private final String BASE_URL_GET = "http://localhost:8081/product/get/";

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/product/get/{pid}")
public JsonModel get(@PathVariable("pid") Integer pid, JsonModel jsonModel) throws IOException {
    String url = BASE_URL_GET + pid;

    //(请求路径,响应数据类型)
    jsonModel = this.restTemplate.getForObject(url, JsonModel.class);
    //postForObject、exchange、delete
    return jsonModel;
}
  • Apache的Http Client
  • Netty的异步Http Client
  • Feign(最优)

Eureka

Ribbon

Feign

Hystrix+Turbine

Zuul

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值