外贸网站建设公司案例,深圳网站建设易佰讯,珠宝营销型网站设计,阿里巴巴做网站的Spring Cloud 基础入门级学习 超详细#xff08;简单通俗易懂#xff09; 一、SpringCloud核心组件第一代#xff1a;SpringCloud Netflix组件第二代#xff1a;SpringCloud Alibaba组件SpringCloud原生组件 二、SpringCloud体系架构图三、理解分布式与集群分布式集群 四、… Spring Cloud 基础入门级学习 超详细简单通俗易懂 一、SpringCloud核心组件第一代SpringCloud Netflix组件第二代SpringCloud Alibaba组件SpringCloud原生组件 二、SpringCloud体系架构图三、理解分布式与集群分布式集群 四、最简单的样例实现微服务调用 一、SpringCloud核心组件 SpringCloud是在SpringBoot的基础上增加了很多微服务相关的规范。目前SpringCloud规范已经由Spring官方、SpringCloud Netflix、SpringCloud Alibaba等实现。通过组件化的方式SpringCloud将这些实现整合起来构成全家桶式的微服务技术栈。 第一代SpringCloud Netflix组件
目前SpringCloud Netflix已经停止维护
组件名称作用Eureka服务注册与发现组件Ribbon负载均衡组件Feign声明式服务调用Hystrx熔断器组件ZuulAPI网关组件
第二代SpringCloud Alibaba组件
Spring Cloud Alibaba是阿里开源的一套Sping Cloud规范的实现配置比 NetFlix 更简单易用。
组件名称作用nacos服务注册与发现 配置中心 流量控制 (Eureka config)Sentinel容错保护组件Dubbo服务治理 负载均衡与容错 远程调用 服务降级RecketMQ消息中间件Seata分布式事务
SpringCloud原生组件
组件名称作用Consul服务注册Zookeeper服务发现和配置管理Config分布式配置中心Sleuth/Zipkin分布式链路追踪GatewayAPI服务网关新一代API网关提供路由负载均衡过滤等OpenFeign声明式服务调用Stream消息驱动 简化消息的发送和接收Bus消息总线LoadBalancer客户端负载均衡 目前市场主流选择
SpringCloud AlibabaSpringCloud原生网关Spring Cloud GateWay服务注册与发现、配置中心SpringCloud Alibaba Nacos服务间调用Spring Cloud OpenFeign负载均衡Spring Cloud LoadBalance客户端容错保护:SpringCloud Alibaba Sentinel消息中间件:RabbitMQ
二、SpringCloud体系架构图 三、理解分布式与集群
分布式
分布式把一个大业务拆分成多个子业务每个子业务都是一套独立的系统子业务之间相互协作最终完成整体的大业务
比如 小明是一家烧烤店的烧烤师傅每天不仅要准备食材准备配料烧烤可以看作一个单体架构。 后来小明烤的太好吃了客人也多了又专门雇了两位师傅小红李华小红专门准备食材李华专门准备配料小明专门烧烤。小明小红李华这三者的关系就是分布式。 分工完成一件事就是分布式
集群
集群在几个服务器上部署相同的应用程序来分担客户端的请求。
比如 小明不雇用小红和李华了找到了自己的好朋友烧烤师傅 王刚来帮忙王刚每天工作和小明一样要准备食材准备配料烧烤。此时王刚和小明的关系就是集群 各自做同一件事情就是集群
四、最简单的样例实现微服务调用
创建shop父模块 和 shop-provider(服务提供者) 和 shop-consumer(服务消费者)三个maven模块 父依赖pom.xml (锁定Springboot和SpringCloud版本 和 写一些通用的依赖)
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdcloud-shop/artifactIdpackagingpom/packagingversion1.0-SNAPSHOT/versionmodulesmodulecloud-shop-provider/modulemodulecloud-shop-consumer/module/modules!-- 统一管理jar包版本 --propertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.targetjunit.version4.12/junit.versionlog4j.version1.2.17/log4j.versionlombok.version1.18.22/lombok.versionmysql.version8.0.24/mysql.versiondruid.version1.2.8/druid.versionmybatis-plus.version3.0.7.1/mybatis-plus.version/propertiesdependencies!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- https://mvnrepository.com/artifact/com.alibaba/druid --dependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.8/version/dependency!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId/dependency!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion${mybatis-plus.version}/versionoptionaltrue/optionalexclusionsexclusiongroupIdcom.baomidou/groupIdartifactIdmybatis-plus-generator/artifactId/exclusion/exclusions/dependency/dependencies!-- 子模块继承之后提供作用锁定版本 子 modlue 不用写 groupId和version --dependencyManagementdependencies!--spring boot 2.2.2--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.2.2.RELEASE/versiontypepom/typescopeimport/scope/dependency!--spring cloud Hoxton.SR1--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionHoxton.SR1/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationaddResourcestrue/addResources/configuration/plugin/plugins/build/project准备一张数据表任意数据都行 对服务提供者进行操作
application.yml文件
server:port: 8001 #服务端口号
spring:application:name: cloud-shop-provider #服务名称datasource:type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包url: jdbc:mysql://127.0.0.1:3306/csdn?characterEncodingutf8serverTimezoneAsia/Shanghaiusername: rootpassword: 1234
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpltype-aliases-package: com.chq.csdn.entitymapper-locations: classpath:mapping/*xmlentity
Data
AllArgsConstructor
NoArgsConstructor
public class Payment extends ModelPayment {private Long id;private String serial;
}
PaymentDao
Mapper
public interface PaymentDao extends BaseMapperPayment {
}PaymentService
public interface PaymentService extends IServicePayment {}PaymentServiceImpl
Service
public class PaymentServiceImpl extends ServiceImplPaymentDao, Payment implements PaymentService {}PaymentController
RestController
RequestMapping(/payment)
public class PaymentController extends ApiController {/*** 通过主键查询单条数据* param id 主键* return 单条数据*/GetMapping(/getPayment/{id})public R selectOne(PathVariable Serializable id) {return success(this.paymentService.getById(id));}/*** 新增数据* param payment 实体对象* return 新增结果*/PostMapping(/insertPayment)public R insert(RequestBody Payment payment) {return success(this.paymentService.save(payment));}启动类
SpringBootApplication
MapperScan(com.chq.csdn.dao)
public class ShopProviderApplication {public static void main(String[] args) {SpringApplication.run(ShopProviderApplication.class,args);}
}
启动消费者项目
访问localhost:8001/payment/insertPayment 数据库数据
访问localhost:7001/payment/getPayment
在对服务消费者进行操作
再次编写一次entity这里就有点重复操作了实际开发时我们可以将实体类通通放入一个模块进行调用这里方便理解就再写一遍
Data
AllArgsConstructor
NoArgsConstructor
public class Payment extends ModelPayment {private Long id;private String serial;
}进行服务调用这里我们使用RestTemplate准备好RestTemplate配置类
Configuration
public class ApplicationContextConfig {Beanpublic RestTemplate restTemplate(){return new RestTemplate();}
}ConsumerController
RestController
RequestMapping(/consumer)
public class ConsumerController extends ApiController {//服务端口号这里建议使用127.0.0.1不建议使用localhostprivate static final String HOST http://127.0.0.1:8001;Autowiredprivate RestTemplate restTemplate;//还是刚才的方法 获取 和 添加GetMapping(/payment/get/{id})public R get(PathVariable(id) Long id){//拼接结果 http://127.0.0.1:8001/payment/getPayment/id//R.class是返回类型return restTemplate.getForObject(HOST /payment/getPayment/ id,R.class);}PostMapping(/payment/insert)public R insert(RequestBody Payment payment){//payment是请求参数return restTemplate.postForObject(HOST /payment/insertPayment ,payment,R.class);}
}application.yml文件 注意这里的配置文件端口号改变并且没有配置数据源无法对数据库直接操作
server:port: 8002 #服务端口号
spring:application:name: cloud-shop-consumer #服务名称启动类 //因为我们添加了数据库的依赖却没有配置数据源 加上
//(exclude {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
//可以忽略数据源启动
SpringBootApplication(exclude {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
public class ShopConsumerApplication {public static void main(String[] args) {SpringApplication.run(ShopConsumerApplication.class,args);}
}启动成功后
访问localhost:8002/consumer/payment/insert
查看数据表 访问localhost:8002/consumer/payment/get/id
此时服务调用就结束了我们可以发现消费者consumer没有配置数据源却通过RestTemplate访问消费者服务从而间接访问数据库这就是服务的调用。 服务注册中心扮演者一个仓储功能服务提供者提供服务注册到注册中心服务消费者通过http消息或组件到注册中心而非RestTemplate去找到所需的服务这也就是服务中心服务消费者和服务提供者三者关系