• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

02-微服务基础

武飞扬头像
Black_Me_Bo
帮助1

服务架构演变

单体架构

**概念:**将业务所有功能集中在一个项目中开发,打成一个包部署

优点:

  • 架构简单
  • 部署成本低

缺点:

  • 耦合度高

分布式架构

**概念:**根据业务功能对系统惊醒拆分,每个业务模块作为独立项目开发,称为一个服务

优点:

  • 降低服务耦合
  • 有利于服务升级拓展

微服务

微服务是一种经过良好架构设计的分布式架构方案,微服务架构特征:

  • 单一职责:微服务拆分粒度更小,每一个服务都对应唯一的业务能力,做到单一职责,避免重复业务开发
  • 面向服务:微服务对外暴露业务接口
  • 自治:团队独立、技术独立、数据独立、部署独立
  • 隔离性强:服务调用做好隔离、容错、降级,避免出现级联问题

总结

单体架构特点?

  • 简单方便,高度耦合,扩展性差,适合小型项目。例如:学生管理系统

分布式架构特点?

  • 松耦合,扩展性好,但架构复杂,难度大。适合大型互联网项目,例如:京东、淘宝

微服务:一种良好的分布式架构方案

  • 优点:拆分粒度更小、服务更独立、耦合度更低

  • 缺点:架构非常复杂,运维、监控、部署难度提高

微服务技术对比

微服务这种方案需要技术框架来落地,全球的互联网公司都在积极尝试自己的微服务落地技术。在国内最知名的就是SpringCloud和阿里巴巴的Dubbo。

  Dubbo SpringCloud SpringCloud Alibaba
注册中心 zookeeper、Redis Eureka、Consul Nacos、Eureka
服务远程调用 Dubbo协议 Feign(http协议) Dubbo、Feign
配置中心 SpringCloudConfig SpringCloudConfig、Nacos
服务网关 SpringCloudGateway、Zuul SpringCloudGateway、Zuul
服务监控和保护 dubbo-admin,功能弱 Hystrix Sentinel

结合方式

SpringCloud Feign

  • 使用SpringCloud技术栈
  • 服务接口采用Restful风格
  • 服务调用采用Feign方式

SpringCloudAlibaba Feign

  • 使用SpringCloudAlibaba技术栈
  • 服务接口采用Restful风格
  • 服务调用采用Feign方式

SpringCloudAlibaba Dubbo

  • 使用SpringCloudAlibaba技术栈
  • 服务接口采用Dubbo协议标准
  • 服务调用采用Dubbo方式

Dubbo原始模式

  • 给予Dubbo老旧技术体系
  • 服务接口采用Dubbo协议标准
  • 服务调用采用Dubbo方式

SpringCloud简介

  • SpringCloud是目前国内使用最广泛的微服务框架。官网地址:https://spring.io/projects/spring-cloud。
  • SpringCloud集成了各种微服务功能组件,并基于SpringBoot实现了这些组件的自动装配,从而提供了良好的开箱即用体验
服务注册发现 服务远程调用 服务链路监控 统一配置管理 统一网关路由 流控、降级、保护
Eureka OpenFeign Zipkin SpringCloudConfig SpringCloudGateway Hystix
Nacos Dubbo Sleuth Nacos Zuul Sentinel
Consul          

服务拆分及远程调用

服务拆分注意事项

  1. 不同微服务,不要重复开发相同业务
  2. 微服务数据独立,不要访问其他微服务的数据库
  3. 微服务可以将自己的业务暴露为接口,供其他微服务调用

远程调用

  • 基于RestTemplate发起的http请求实现远程调用
  • http请求做远程调用是与语言无关的调用,只要知道对方的ip、端口、接口路径、请求参数即可。

注册RestTemplate

package cn.bttc.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@MapperScan("cn.bttc.order.mapper")
@SpringBootApplication
public class OrderApplication {

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

    /*
     * @Author blackme
     **/
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

服务远程调用RestTemplate

package cn.bttc.order.service;

import cn.bttc.order.pojo.Order;
import cn.bttc.order.pojo.User;
import cn.bttc.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author blackme
 */
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        Order order = orderMapper.findById(orderId);
        String url = "http://localhost:8081/user/"   order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjgiig
系列文章
更多 icon
同类精品
更多 icon
继续加载