什么是微服务
最早在Martin Fowler的blog中提及:
https://martinfowler.com/articles/microservices.html
- 一种设计风格
- 将原本独立的系统拆分成多个小型服务,并且能独立运行
- 基于API接口通讯协作
同类型的框架
- 服务:
- 阿里-Dubbo
- 当当-DubboX
- Netflix-Eureka
- Apache-Consul
- 分布式管理
- 批量任务
- 跟踪
Spring Cloud模块介绍
- spring-boot-starter-web: 包含web, tomcat,的模块
- spring-boot-starter-test: 包含JUnit, 其他测试模块
- spring-boot-starter-actuator: 监控模块
构建项目
- web方式: https://start.spring.io/
- IDEA方式
选择web依赖
选择Maven构建工具完成后配置文件:pom.xml
1 |
|
配置详情 src/main/resources
- application.properties:
1
2
3//分别配置server和spring
server.port=8080
spring.application.name=hello- 也可以使用YAML来配置
命令行参数
1 | jar -jar demo.jar --server.port=8080 //同样可以指定端口 |
多环境配置
- 分别使用application-dev, -test, -prod来表示加载那个配置文件
- 在application.properties中设置spring.profiles.active=test来生效
创建单元测试test
1 | (SpringRunner.class) |