微服务架构介绍
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;@Servicepublic 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;@RestControllerpublic 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;@RestControllerpublic 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: 8081spring: 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: 8082spring: 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: 8083spring: 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: gatewaymanagement: 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返回限流提供,我们也可以实现自定义限流提示
http://www.tianya.cn/143521411/profile
http://www.tianya.cn/143518697/profile
http://www.tianya.cn/143521486/profile
http://www.tianya.cn/143521503/profile
http://www.tianya.cn/143521536/profile
http://www.tianya.cn/143521568/profile
http://www.tianya.cn/143521568/profile
http://www.tianya.cn/142585408/profile
http://www.tianya.cn/143521411/
http://www.tianya.cn/143518697/
http://www.tianya.cn/143521486/
http://www.tianya.cn/143521503/
http://www.tianya.cn/143521536/
http://www.tianya.cn/143521568/
http://www.tianya.cn/143521568/
http://www.tianya.cn/142585408/
http://www.tianya.cn/143521411
http://www.tianya.cn/143518697
http://www.tianya.cn/143521486
http://www.tianya.cn/143521503
http://www.tianya.cn/143521536
http://www.tianya.cn/143521568
http://www.tianya.cn/143521568
http://www.tianya.cn/142585408
http://www.tianya.cn/140397500/profile
https://www.tianya.cn/140397500/
12.16
http://www.tianya.cn/142776503/profile
http://www.tianya.cn/142776503 作者:啊实打实发r534 https://www.bilibili.com/read/cv14451158 出处:bilibili