微服务架构介绍

Spring Cloud Alibaba推荐的微服务生态架构基于分层架构实现如下:

  • 接入层:最外层为LVS+Keepalived,可承受几十万级高并发流量洪峰,然后再通过内层的nginx集群将客户端请求通过负载均衡策略转发至基于JAVA后端技术栈的Spring Cloud Gateway集群;

  • 业务中台层:Spring Cloud Gateway微服务通过Nacos获取路由配置信息和路由后端微服务提供者的发现,通过OAuth2做统一登录授权,并集成整合Sentinel针对请求做限流、熔断、降低,基于dubbo协议的高性能RPC进行微服务调用或者服务聚合调用,而后端微服务之间调用也是采用dubbo协议的rpc,对于需要分布式事务服务端则通过Seata实现。

  • 技术中台层:数据存储层包括内存、数据库、全文检索搜索引擎存储层;基础服务层提供分布式系统常见基础组件功能;日志采集层采用ELK

  • 系统监控层:分布式链路追踪、基于容器化的监控和告警

微服务生态涉及技术点如下

Maven整合工程入门案例

shopping-demo源码地址

https://gitee.com/yongzhebuju/shopping

功能简介


本示例主要对微服务使用Nacos实现配置中心读取、服务注册和服务发现,微服务网关实现路由策略并整合sentinel实现限流,微服务之间使用Dubbo高性能RPC进行调用。

本案例主要包含一下几个demo模块

commons:公共服务模块,存放公共pojo实体类和微服务接口模块,比如Dubbo服务提供者接口定义、基于Open Feign远程调用服务提供者接口定义等,公共模块pom可以配置一些公共引用依赖如spring-cloud-starter-alibaba-nacos-config和spring-cloud-starter-alibaba-nacos-discovery等,这样其他微服务只需依赖公共模块即可

gateway:微服务入口网关模块,负责微服务路由、授权认证、微服务聚合等功能处理

users:用户模块,提供获取用户接口

good:商品模块,提供商品接口,需要调用用户接口

核心源码和配置

工程父pom文件主要包含Spring Boot、Spring Cloud、Spring Cloud Alibaba的父依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itxs</groupId>
<artifactId>shopping</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent> <modules>
<module>shopping_commons</module>
<module>shopping_goods</module>
<module>shopping_users</module>
<module>shopping_gateway</module>
</modules> <properties>
<java.verson>1.8</java.verson>
<spring.cloud.verison>Hoxton.SR12</spring.cloud.verison>
<spring.cloud.alibaba.verison>2.2.1.RELEASE</spring.cloud.alibaba.verison>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.verison}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.verison}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> </project>

微服务yml配置文件,微服务的配置都放在Nacos配置中心,每个微服务本地配置文件只需配置服务名称、激活的环境以及配置中心地址、配置文件扩展名、命名空间和组即可。下面为网关配置文件,其他模块配置文件与此类似

spring:
profiles:
active: dev
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
group: shopping
application:
name: gateway

commons 实体类和暴露获取用户接口服务

package com.itxs.entity;

import java.io.Serializable;

public class User implements Serializable {
private String name;
private Integer age; public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
package com.itxs.service;

import com.itxs.entity.User;

public interface UserService {
User getUser(String userId);
}

users微服务获取用户接口实现

package com.itxs.service;

import com.itxs.entity.User;
import org.apache.dubbo.config.annotation.Service; @Service
public class UserServiceImpl implements UserService{
@Override
public User getUser(String userId) {
System.out.println("userId:"+userId);
return new User("zhangsan",16);
}
}

users controller实现类,在这里也提供http协议调用方式

package com.itxs.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController { @RequestMapping("/users/1")
public String getUser(){
return "hello users";
}
}

goods controller

package com.itxs.controller;

import com.itxs.entity.User;
import com.itxs.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class GoodsController { @Reference
UserService userService; @RequestMapping("/goods/1")
public String getGoods(){
return "hello goods";
} @RequestMapping("/goods/user")
public String getUserInfo(){
User user = userService.getUser("a1001");
return user.toString();
}
}

Nacos 配置中心

启动本地Nacos server端,访问本地nacos管理界面http://localhost:8848/nacos,默认端口是8848,默认用户密码nacos/nacos,在dev命名空间下有网关、用户、商品微服务配置文件,都使用shoopping组

users-dev.yaml

server:
port: 8081
spring:
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: 192.168.3.3:8848
namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
group: shopping
sentinel:
enabled: true
transport:
dashboard: localhost:8888
port: 8719
application:
name: users

goods-dev.yaml

server:
port: 8082
spring:
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: 192.168.3.3:8848
namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
group: shopping
sentinel:
enabled: true
transport:
dashboard: localhost:8888
port: 8729
application:
name: goods

gateway-dev.yaml

server:
port: 8083
spring:
profiles:
active: dev
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
server-addr: 192.168.3.3:8848
namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
group: shopping
sentinel:
enabled: true
transport:
dashboard: localhost:8080
port: 8719
gateway:
discovery:
locator:
lowerCaseServiceId: true
enabled: true
routes:
- id: users_route
uri: lb://users
predicates:
- Path=/users/** - id: goods_route
uri: lb://goods
predicates:
- Path=/goods/**
application:
name: gateway
management:
endpoints:
web:
exposure:
include: "*"

Sentinel控制台

通过Sentinel源码项目启动Sentinel控制台,是一个Spring Boot项目

访问本地Sentinel控制台界面http://localhost:8080/,默认端口是8080,默认用户密码sentinel/sentinel,由于暂时没有做持久化功能,所以刚进来是内容是空的

微服务启动

启动网关、用户、商品三个微服务,用户微服务端口为8081,商品微服务端口为8082,网关微服务端口为8083

先不通过网关直接访问goods微服务http://localhost:8082/goods/1,走http方式调用接口

通过网关路由配置我们访问用户服务http://localhost:8083/users/users/1 ,访问结果正确

继续访问商品接口服务http://localhost:8083/goods/goods/1 ,访问结果正确

访问商品服务调用用户服务 http://localhost:8083/goods/goods/user ,访问结果正确

Sentinel控制台设置下限流规则,针对goods/goods/user 这个触点链路进行流控设置

当我们每秒访问在两次内还是会访问正常,我们连续快速按F5刷新则会出现Blocked by Sentinel: FlowException,这个是默认Sentinel返回限流提供,我们也可以实现自定义限流提示

**本人博客网站 **IT小神 www.itxiaoshen.com

Spring Cloud Alibaba微服务架构入门最容易理解篇的更多相关文章

  1. Spring Cloud Alibaba | 微服务分布式事务之Seata

    Spring Cloud Alibaba | 微服务分布式事务之Seata 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud:Green ...

  2. Spring Cloud构建微服务架构

    Dalston版本 由于Brixton和Camden版本的教程已经停止更新,所以笔者计划在2017年上半年完成Dalston版本的教程编写(原计划完成Camden版本教程,但由于写了两篇Dalston ...

  3. Spring Cloud构建微服务架构(一)服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  4. Spring Cloud构建微服务架构(二)服务消费者

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...

  5. Spring Cloud构建微服务架构:服务网关(路由配置)【Dalston版】

    转载:http://blog.didispace.com/spring-cloud-starter-dalston-6-2/ 原创  2017-08-26  翟永超  Spring Cloud 被围观 ...

  6. Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台

    Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台: https://gitee.com/leecho/cola-cloud

  7. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 我们使用Spring Cloud Netflix中的Eureka实现了服务 ...

  8. Spring Cloud构建微服务架构 - 服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: alt 我们使用Spring Cloud Netflix中的Eureka实 ...

  9. Spring Cloud搭建微服务架构----文章汇总

    Spring Cloud搭建微服务架构----前言 原文地址:https://my.oschina.net/u/1000241/blog/882929 Spring Cloud搭建微服务架构----使 ...

随机推荐

  1. Java 获取PDF数字签名证书信息

    PDF文档中可添加数字签名,在添加签名前,需要准备可信任签名证书.对文档中已有的签名,可验证书签是否有效.也可通过一定方法来获取数字签名或者签名证书信息.下面以Java代码示例展示如何读取签名的证书信 ...

  2. 探究 Go 源码中 panic & recover 有哪些坑?

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/627 本文使用的go的源码1.17.3 前言 写这一篇文章的原因是 ...

  3. Python3的运算符

    比较运算符: == != < > <= >= 逻辑运算符 或与非 or and not   x or y : x为true,则不计算y的值,直接返回ture x为false,则 ...

  4. c语言printf输出最前端字符不显示

    原因:语法错误,和其它语言语法混用. printf("链表长度 : %d \n",length); printf("length is : %d \n",len ...

  5. 洛谷 P7078 - [CSP-S2020] 贪吃蛇(贪心)

    题面传送门 题意: 有 \(n\) 条蛇,每条蛇有个实力 \(a_i\) 我们称编号为 \(x\) 的蛇比编号为 \(y\) 的蛇强,当且仅当 \(a_x>a_y\) 或 \(a_x=a_y\) ...

  6. Codeforces 1290F - Making Shapes(数位 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 数位 dp 好题. 首先,由于是凸包,一但向量集合确定,凸包的形态肯定就已经确定了.考虑什么样的向量集合能够组成符合条件的凸包,我们假设第 ...

  7. Codeforces 889E - Mod Mod Mod(dp+状态设计)

    Codeforces 题目传送门 & 洛谷题目传送门 题目名称 hopping 我们记 \(x_i=X\bmod a_1\bmod a_2\bmod\dots\bmod a_i\),也就是 \ ...

  8. 【基因组预测】braker2基因结构注释要点记录

    目录 流程使用 问题 记录下braker2的使用要点,以备忘记. 流程使用 braker2有很多流程,根据你的数据:组装的基因组.转录组.蛋白(同源,包括近缘或远缘)选择不同流程,官网有说明: htt ...

  9. R shiny 小工具Windows本地打包部署

    目录 服务器部署简介 windows打包部署 1. 部署基本框架 2.安装shiny脚本需要的依赖包 3.创建运行shiny的程序 [报错解决]无法定位程序输入点EXTPTE_PTR于动态链接库 将小 ...

  10. shell批量创建用户

    #!/bin/bash cat << EOF ************************************************************ 批量添加用户并随机生 ...