一、SpringCloud简介
微服务
微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底地去解耦合,每一个微服务提供单个业务功能也服务,一个服务做一件事,从技术角度看就是一种小而独立的处理过程,类似进程概念,能够自行单独启动或销毀,拥有自己独立的数据库。
SpringCloud与Dubbo对比
| Dubbo | SpringCloud | |
|---|---|---|
| 服务注册中心 | Zookeeper | Eureka |
| 服务调用方式 | RPC | RestAPI |
| 服务监控 | Dubbo-monitor | Spring Boot Admin |
| 断路器 | 不完善 | Hystrix |
| 服务网关 | 无 | Zuul |
| 分布式配置 | 无 | Spring Cloud Config |
| 服务跟踪 | 无 | Spring Cloud Sleuth |
| 消息总线 | 无 | Spring Cloud Bus |
| 数据流 | 无 | Spring Cloud Stream |
| 批量任务 | 无 | Spring Cloud Task |
最大区别:SpringCloud抛弃了Dubbo的RPC通信,采用的是基于HTTP的REST方式。 严格来说,这两种方式各有优劣,虽然从一定程度上来说,后者牺牲了服务调用的性能,但也避免了上面提到的原生RPC带来的问题。而且REST相比RPC更为灵活,服务提供方和调用方的依赖只依靠一纸契约,不存在代码级别的强依赖,这在强调快速演化的微服务环境下,显得更加合适。
一、Rest微服务构建
项目结构
microservicecloud // 父项目
|- microservicecloud-api // 存放公共接口、实体类。。
|- microservicecloud-provider-dept-8081 // 部门服务提供者
|- microservicecloud-consumer-dept-80 // 部门服务消费者
1、创建maven父项目
书写pom.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>cn.x5456</groupId> 8 <artifactId>microservicecloud</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 <modules> 12 <module>microservicecloud-api</module> 13 </modules> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <maven.compiler.source>1.8</maven.compiler.source> 18 <maven.compiler.target>1.8</maven.compiler.target> 19 <junit.version>4.12</junit.version> 20 <log4j.version>1.2.17</log4j.version> 21 <lombok.version>1.16.18</lombok.version> 22 </properties> 23 24 <dependencyManagement> 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-dependencies</artifactId> 29 <version>Dalston.SR1</version> 30 <type>pom</type> 31 <scope>import</scope> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-dependencies</artifactId> 36 <version>1.5.9.RELEASE</version> 37 <type>pom</type> 38 <scope>import</scope> 39 </dependency> 40 <dependency> 41 <groupId>mysql</groupId> 42 <artifactId>mysql-connector-java</artifactId> 43 <version>5.0.4</version> 44 </dependency> 45 <dependency> 46 <groupId>com.alibaba</groupId> 47 <artifactId>druid</artifactId> 48 <version>1.0.31</version> 49 </dependency> 50 <dependency> 51 <groupId>org.mybatis.spring.boot</groupId> 52 <artifactId>mybatis-spring-boot-starter</artifactId> 53 <version>1.3.0</version> 54 </dependency> 55 <dependency> 56 <groupId>ch.qos.logback</groupId> 57 <artifactId>logback-core</artifactId> 58 <version>1.2.3</version> 59 </dependency> 60 <dependency> 61 <groupId>junit</groupId> 62 <artifactId>junit</artifactId> 63 <version>${junit.version}</version> 64 <scope>test</scope> 65 </dependency> 66 <dependency> 67 <groupId>log4j</groupId> 68 <artifactId>log4j</artifactId> 69 <version>${log4j.version}</version> 70 </dependency> 71 </dependencies> 72 </dependencyManagement> 73 74 <build> 75 <finalName>microservicecloud</finalName> 76 <resources> 77 <resource> 78 <directory>src/main/resources</directory> 79 <filtering>true</filtering> 80 </resource> 81 </resources> 82 <plugins> 83 <plugin> 84 <groupId>org.apache.maven.plugins</groupId> 85 <artifactId>maven-resources-plugin</artifactId> 86 <configuration> 87 <delimiters> 88 <delimit>$</delimit> 89 </delimiters> 90 </configuration> 91 </plugin> 92 </plugins> 93 </build> 94 95 </project>