微服务概述:
- 单一职责:微服务中每一个服务都对应唯一的业务能力,做到单一职责。
- 微:微服务的服务拆分力度很小,例如一个用户管理就可以作为一个服务,每个服务虽然小但是五脏俱全。
- 面向服务:面向服务是说每个服务都要对外暴露Rest风格的服务接口API。并不关系服务的技术实现,做到与平台和语言无关,也不限定用什么技术实现,只需要提供Rest接口即可。
- 自治:自治说的是服务之间相互独立,互不干扰。
- 团队独立:每一个服务都是独立的开发团队。
- 技术独立:因为是面向服务,提供Rest接口,服务使用什么技术实现没有别人干涉。
- 前后端分离:采用前后端分离开发,提供同一的Rest接口,后端不需要再为PC、移动端开发不同的接口。
- 数据库分离:每个服务使用自己的数据库。
- 部署独立:服务间虽然有调用,但要做到服务重启不影响其他的服务,有利于持续集成和持续交付。每个服务都有独立的组件,可复用、可替换、降低耦合、易维护。
一、服务调用方式
RPC和HTTP:
无论是微服务还是SOA架构,都面临着服务之间的远程调用,常见的远程调用方式有以下两种:
- RPC:远程过程调用,类似的还有RMI。自定义数据格式,基于原生TCP通信,速度快、效率高。早期的webService,还有阿里系的分布式框架Dubbo,都是RPC的典型代表。
- Http:http其实是一种网络传输协议,基于TCP,规定了数据传输的格式。现在客户端浏览器与服务端通信基本都是采用Http协议,也可以用来进行远程服务调用。缺点是消息封装臃肿,优势是对服务的提供和调用方法没有任何的技术限定,自由灵活,更符合微服务理念。SpringCloud在进行远程服务调用就是基于http协议来完成的。
二、SpringCloud简介
SpringCloud是Spring旗下的项目之一,Spring最擅长的就是集成,把世界上最好的框架拿过来集成到自己的项目之中。
SpringCloud也是一样,SpringCloud就是一系列组件的集合,它将现在一些非常流行的技术整合到一起,实现了诸如:配置管理、服务发现、智能路由、负载均衡、熔断器、控制总线、集群状态等等功能。其中主要涉及的组件包括:
- Eureka:服务治理组件,包含服务注册中心,服务注册与发现机制的实现。
- Zuul:网关组件,提供智能路由、访问过滤功能。
- Ribbon:客户端负载均衡的服务调用组件。
- Feign:声明式远程服务调用,给与Ribbon和Hystrix的声明式服务调用组件。
- Hystrix:容错管理组件,实现断路器模式,帮助服务依赖中出现的延迟和为故障提供强大的容错能力。(熔断、断路器、容错)
SpringCloud版本简介:
因为SpringCloud不同于其他独立项目,它是拥有很多子项目的大项目,所以它的版本是版本名+版本号(如Hoxton SR3)。
版本名:是伦敦的地铁站名;
版本号:SR(Service Releases)是固定的,意思是稳定版本,后面会有一个递增的数字。
所以Hoxton SR3就是Hoxton的第三个稳定版本。
微服务入门案例搭建:
1、搭建服务提供方,集成mybatis操作数据库,提供Rest接口:
package com.xsh.producer.controller;
import com.xsh.producer.pojo.User;
import com.xsh.producer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findByid/{uid}")
public User findByid(@PathVariable Integer uid){
return userService.findByid(uid);
}
}
2、搭建服务消费方,获取服务提供方的数据
在此只是简单实现服务提供方和服务消费方的远程调用,采用Spring提供的RestTemplate进行远程调用。
服务消费方只需要配置web启动器,用于调用服务提供方的Rest接口并向外暴露Rest接口即可。
①在引导类中初始化注入RestTemplate
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
②编写Controller通过RestTemolate远程调用服务提供方的接口
package com.xsh.consumer.controller;
import com.xsh.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/consumer/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/{uid}")
public User findByid(@PathVariable Integer uid){
return restTemplate.getForObject("http://localhost:8081/user/findByid/"+uid,User.class);
}
}
至此,我们就实现了服务提供方与服务消费放的远程调用。