SpringCloud--01、微服务简介

版权声明:转载请注明原始链接 https://blog.csdn.net/sswqzx/article/details/84727056

一、微服务

1、集中式架构

当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,
用于简化增删改查工作量的数据访问框架(ORM)是影响项目开发的关键

存在的问题:

- 代码耦合,开发维护困难
- 无法针对不同模块进行针对性优化
- 无法水平扩展
- 单点容错率低,并发能力差

2、垂直拆分

访问量大、为了提高更高的并发和业务需求、根据业务功能对系统进行拆分

优点:

- 系统拆分实现了流量分担,解决了并发问题
- 可以针对不同模块进行优化
- 方便水平扩展,负载均衡,容错率提高

缺点:

系统间相互独立,会有很多重复开发工作,影响开发效率

3、分布式服务

抽取核心业务、作为独立服务、此时,用于提高业务复用及整合的分布式调用是关键。

优点:

将基础服务进行了抽取,系统间相互调用,提高了代码复用和开发效率

缺点:

系统间耦合度变高,调用关系错综复杂,难以维护

4、服务治理(SOA)

SOA:面向服务的架构

服务越来越多,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。
此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键。

缺点:

- 服务间会有依赖关系,一旦某个环节出错会影响较大
- 服务关系复杂,运维、测试部署困难,不符合DevOps思想

5、微服务

概述

微服务架构,就是将单一程序开发成多个微服务,每个微服务运行处理唯一的业务,
并使用轻量级机制通信,通常是HTTP RPC。这些服务围绕业务能力来划分构建的,并通过完全自动化部署机制来
独立部署。这些服务可以使用不同的编程语言,以及不同数据存储技术,以保证最低限度的集中式管理

 二、远程调用方式(通信方式)

无论是微服务还是SOA,都面临着服务间的远程调用。那么服务间的通信方式:RPC 、HTTP

1、RPC

RPC:Remote Procedure Call远程过程调用,类似的还有RMI。
自定义数据格式,基于原生TCP通信,速度快,效率高。早期的webservice,现在热门的dubbo,都是RPC的典型

2、Http

http其实是一种网络传输协议,基于TCP,规定了数据传输的格式。
现在客户端浏览器与服务端通信基本都是采用Http协议。也可以用来进行远程服务调用。缺点是消息封装臃肿。
现在热门的Rest风格,就可以通过http协议来实现。
- 优点:RPC方式更加透明,对用户更方便。Http方式更灵活,没有规定API和语言,跨语言、跨平台
- 缺点:RPC方式需要在API层面进行封装,限制了开发的语言环境。

三、HTTP客户端工具

HttpClient、OKHttp、URLConnection

1、HttpClient

HttpClient是Apache公司的产品,是Http Components下的一个组件。

特点:

- 基于标准、纯净的Java语言。实现了Http1.0和Http1.1
- 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)
- 支持HTTPS协议。
- 通过Http代理建立透明的连接。
- 自动处理Set-Cookie中的Cookie。

Rest风格:

- 查询:GET,/user/12
- 新增:POST, /user
- 修改:PUT, /user
- 删除:DELTE, /user/12

发起get请求

  @Test
    public void testGet() throws IOException {
        HttpGet request = new HttpGet("http://www.baidu.com");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

发起Post请求:

@Test
public void testPost() throws IOException {
    HttpPost request = new HttpPost("http://www.baidu.com/");
    request.setHeader("User-Agent",
                      "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
    String response = this.httpClient.execute(request, new BasicResponseHandler());
    System.out.println(response);
}

也可以访问http://localhost/user/3

UserController.java

package com.baidus.user.contrlooer;

import com.baidus.user.pojo.User;
import com.baidus.user.service.UserService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 15:54 2018/12/1
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }
}

测试

    @Test
    public void testGetPojo() throws IOException {
        HttpGet request = new HttpGet("http://localhost:8081/user/1");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

2、Spring的RestTemplate <低层为URLConnection>new RestTemplate(Okhttp)

Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,
并且实现了对象与json的序列化和反序列化,非常方便。RestTemplate并没有限定Http的客户端类型,
而是进行了抽象,目前常用的3种都有支持:

- HttpClient
- OkHttp
- JDK原生的URLConnection(默认的)

在项目中注入RestTemplate

@SpringBootApplication
public class HttpDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(HttpDemoApplication.class, args);
	}

	@Bean
	public RestTemplate restTemplate() {
        // 默认的RestTemplate,底层是走JDK的URLConnection方式。
        //return new RestTemplate(new OkHttp3ClientHttpRequestFactory())
        //写了OkHttp就是OkHttp
		return new RestTemplate();
	}
}
通过RestTemplate的getForObject()方法,传递url地址及实体类的字节码,
RestTemplate会自动发起请求,接收响应,并且帮我们对响应结果进行反序列化。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HttpDemoApplication.class)
public class HttpDemoApplicationTests {

	@Autowired
	private RestTemplate restTemplate;

	@Test
	public void httpGet() {
		User user = this.restTemplate.getForObject("http://localhost/hello/2", User.class);
		System.out.println(user);
	}
}

猜你喜欢

转载自blog.csdn.net/sswqzx/article/details/84727056
今日推荐