目前成熟互联网架构图:应用服务化拆分+消息中间件
可以看一下社区活跃度
https://github.com/dubbo
https://github.com/springcloud
Dubbo | springcloud | |
---|---|---|
服务注册中心 | zookeeper | springcloud netfix Eureka |
服务调用方式 | RPC | Rest API |
服务监控 | Dubbo-monitor | springboot admin |
断路器 | 不完善 | springcloud netfix Hystrix |
服务网关 | 无 | springcloud netfix zuul |
分布式配置 | 无 | springcloud netfix config |
服务跟踪 | 无 | springcloud netfix sleuth |
消息总线 | 无 | springcloud netfix bus |
数据流 | 无 | springcloud netfix stream |
批量任务 | 无 | springcloud netfix task |
最大区别:springcloud抛弃了dubbo的RPC通信,采用的是基于http的rest方式
严格来说,这两种方式各有优劣,虽然从一定程度上来说,后者牺牲了服务调用的性能,但也避免了上面提到原生RPC带来的问题。
而rest比rpc更为灵活,服务提供和调用方的依赖只依靠一纸契约,不存在代码级别的强依赖,这种强调快速演化下的微服务环境下,显得更合适
品牌机与组装机的区别
很明显 springcloud的功能比dubbo更强大,涵盖面更广,而且作为spring的拳头项目,它也能够与springFramework springboot springdata spring batch 等其他spring项目完美融合,
这些对于微服务而言至关重要。使用dubbo构建的微服务就像组装电脑,各个环节高度自由,但是最终可能某一个小问题导致开启失败,总让人不怎么放心,如果你是一名高手,那么这问题都不是存在;
而springcloud就像品牌机,在spring source的整合下,做了大量的兼容性测试,保证了机器拥有更高稳定性,但是如果要在非原装组件外的东西,就需要对其基础有足够的了解。
社区支持与更新力度
重要的是dubbo停止了5年左右的更新,在2017.7重启了,对于技术发展的新需求,需要有开发者自行拓展升级,这对很多采用微服务的中小软件组织不太合适,中小公司没有那么强大的技术去修改dubbo源码+周边一套解决方案。
总结
解决的问题域不一样:Dubbo的定位是一款RPC框架,springcloud的目标是微服务架构下的一站式解决方案
springcloud 没有采用数字编号的方式命名版本号,而是采用了伦敦地铁站的名称,同时根据字母表的顺序来对应版本时间顺序
自学参考书
一个简单的maven模块结构:
-- app-parent:一个父项目(app-parent)聚合了很多子项目(app-util\app-dao\app-web)
| --pom.xml
|
| --app-core
|| ----pom.xml
|
| --app-web
|| ----pom.xml
....
一个父工程带着多个Moudule子模块
SpringCloudDemo父工程(Project)下带着3个子模块(Module)
springcloud-api 【封装的整体entity/接口/公共配置等】
springcloud-consumer-dept-80 【服务提供者】
springcloud-provider-dept-8001 【服务消费者】
重要版本说明
springboot | springcloud | 关系 |
---|---|---|
1.2.x | Angel版本(天使) | 兼容springboot1.2.x |
1.3.x | Brixton版本(布里克斯顿) | 兼容SpringBoot1.3.x,也兼容SpringBoot1.4.x |
1.4.x | Camden版本(卡姆登) | 兼容SpringBoot1.4.x,也兼容SpringBoot1.5.x |
1.5.x | Dalston版本(多尔斯顿) | 兼容SpringBoot1.5.x,不兼容SpringBoot2.0.x |
1.5.x | Edgware版本(埃奇韦尔) | 兼容SpringBoot1.5.x,不兼容SpringBoot2.0.x |
2.0.x | Finchley版本(芬奇利) | 兼容SpringBoot2.0.x,不兼容SpringBoot1.5.x |
2.1.x | Greenwich版本(格林威治) |
实际开发版本关系
spring-boot-start-parent | 发布日期 | spring-cloud-dependencles | 发布日期 |
---|---|---|---|
1.5.2.RELEASE | 2017-03 | Dalston.RC1 | 2017-x |
1.5.9.RELEASE | 2017-11 | Edgware.RELEASE | 2017-11 |
1.5.16.RELEASE | 2018-04 | Edgware.SR5 | 2018-10 |
1.5.20.RELEASE | 2018-09 | Edgware.SR5 | 2018-10 |
2.0.2.RELEASE | 2018-05 | Fomchiey.BULD-SNAPSHOT | 2018-x |
2.0.6.RELEASE | 2018-10 | Fomchiey-SR2 | 2018-10 |
2.1.4.RELEASE | 2019-04 | Greenwich.SR1 | 2019-03 |
创建一个demoparent模块,再在里面创建api、生产者及消费者模块(选中部分为eureka注册中心,暂时可以不需要,后面会介绍)
项目大致结构如下
创建一个对应的数据库
<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.0modelVersion>
<groupId>springcloudgroupId>
<artifactId>demo-parentartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>apimodule>
<module>provider-dept-8001module>
<module>consumer-dept-8080module>
<module>eureka-8000module>
modules>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<lombok.version>1.18.16lombok.version>
<log4j.version>1.2.17log4j.version>
<logback-core.version>1.2.3logback-core.version>
<mysql-connector-java.version>8.0.21mysql-connector-java.version>
<druid.version>1.1.23druid.version>
<mybatis-spring-boot-starter.version>2.1.4mybatis-spring-boot-starter.version>
<spring-boot-dependencies.version>2.3.8.RELEASEspring-boot-dependencies.version>
<spring-cloud-dependencies.version>Hoxton.SR9spring-cloud-dependencies.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud-dependencies.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot-dependencies.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql-connector-java.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis-spring-boot-starter.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>${logback-core.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
build>
project>
api将实体拆分为一个单独工程,方便后期在其他工程中添加依赖使用
<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">
<parent>
<artifactId>demo-parentartifactId>
<groupId>springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>apiartifactId>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class Dept implements Serializable {
private static final long serialVersionUID = -1L;
private int id;
private String deptName;
private String deptSource;
}
server:
port: 8001
mybatis:
mapper-locations: classspath*:mapper/*.xml
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true&allowMultiQueries=true
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
<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">
<parent>
<artifactId>demo-parentartifactId>
<groupId>springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>provider-dept-8001artifactId>
<dependencies>
<dependency>
<groupId>springcloudgroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jettyartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
dependencies>
project>
@Mapper
@Repository
public interface DeptMapper {
@Select("select id,dept_name deptName, dept_source deptSource from dept")
List<Dept> findAllDept();
@Select("select id,dept_name deptName, dept_source deptSource from dept where id = #{id}")
Dept findById(int id);
@Insert("insert into dept(dept_name) values (#{deptName})")
int addDept(Dept dept);
}
接口
public interface DeptService {
List<Dept> findAllDept();
Dept findById(int id);
int addDept(Dept dept);
}
实现类
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
DeptMapper deptMapper;
@Override
public List<Dept> findAllDept() {
return deptMapper.findAllDept();
}
@Override
public Dept findById(int id) {
return deptMapper.findById(id);
}
@Override
public int addDept(Dept dept) {
return deptMapper.addDept(dept);
}
}
@RestController
@RequestMapping(value = "/dept")
public class DeptController {
@Autowired
DeptService deptService;
@GetMapping("/findAllDept")
public List<Dept> findAllDept() {
return deptService.findAllDept();
}
@GetMapping("/findById/{id}")
public Dept findById(@PathVariable("id") int id) {
return deptService.findById(id);
}
@PostMapping("/addDept")
public int addDept(@RequestBody Dept dept) {
return deptService.addDept(dept);
}
}
@SpringBootApplication
public class ProviderApplication8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication8001.class, args);
}
}
<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">
<parent>
<artifactId>demo-parentartifactId>
<groupId>springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>consumer-dept-8080artifactId>
<dependencies>
<dependency>
<artifactId>demo-parentartifactId>
<groupId>springcloudgroupId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>springcloudgroupId>
<artifactId>apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
dependencies>
project>
server:
port: 8080
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
@RestController
@RequestMapping("/dept")
public class DeptController {
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:8001/dept/";
@GetMapping("/findAll")
public List<Dept> findAll() {
return restTemplate.getForObject(REST_URL_PREFIX + "findAllDept", List.class);
}
@GetMapping("/findById/{id}")
public Dept findById(@PathVariable("id") int id) {
return restTemplate.getForObject(REST_URL_PREFIX + "findById/" + id, Dept.class);
}
@PostMapping("/addDept")
public int addDept(@RequestBody Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "addDept", dept, Integer.class);
}
}
@SpringBootApplication
public class ConsumerDept8080 {
public static void main(String[] args) {
SpringApplication.run(ConsumerDept8080.class, args);
}
}