一、微服务基本介绍
微服务 - 也被称为微服务架构 - 是一种架构风格,它是使一个整合了业务功能的程序成为一个松耦合服务的架构。微服务架构使大型的,复杂的应用程序,可以连续快速的实现和部署。另外,它也使得企业可以发展自己的技术栈体系。
微服务不是银弹
微服务架构不是银弹,它有几个缺点。此外,采用这种架构的时候,你必须解决许多问题。微服务架构模式语言是一系列实现微服务架构模式的集合。 它有两个目标:
1,模式语言使您能够决定微服务是否一个非常适合你的应用程序。
2,模式语言使您能够成功使用微服务架构。
二、微服务组成部分
服务注册与发现:nacos、eureka、consul、zookeeper
配置中心:nacos 、config
路由网关:gateway、zuul
链路追踪:zipkin 、Sleuth
服务调用:ribbon、feign
分布式事务:seata
服务总线:bus、nacos
熔断与限流:hystrix、Sentinel、resilience4j
日志收集:ELK
工作流:Flowable、activiti
任务调度:xxl-job、power-job
三、父工程搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| <properties> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> <knife4j.version>2.0.4</knife4j.version> <spring-boot.version>2.3.5.RELEASE</spring-boot.version> <java.version>1.8</java.version> <mysql.version>8.0.18</mysql.version> <druid.version>1.1.22</druid.version> <guava.version>30.0-jre</guava.version> <seata.version>1.3.0</seata.version> <lombok.version>1.18.4</lombok.version> <easypoi.version>4.2.0</easypoi.version> <mybatis-plus.version>3.3.1</mybatis-plus.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <!--core-service--> <dependency> <groupId>com.lnsoft</groupId> <artifactId>core-service</artifactId> <version>${project.version}</version> </dependency> <!--gateway--> <dependency> <groupId>com.lnsoft</groupId> <artifactId>gateway</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>${seata.version}</version> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>${seata.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!--Spring cloud alibaba 2.2.1.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>${easypoi.version}</version> </dependency> <!--swagger--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus.version}</version> </dependency>
<!--google--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <!--springcloud--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency>
</dependencies> </dependencyManagement>
|
四、nacos注册中心
1.引入pom依赖
1 2 3 4
| <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
|
主启动类添加注解:@EnableDiscoveryClient
2.yml文件添加配置
1 2 3 4 5 6 7
| spring: application: name: test cloud: nacos: discovery: server-addr: 10.0.0.1:8848
|
五、nacos配置中心
1.引入pom依赖
1 2 3 4
| <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
|
2.yml文件添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| spring: application: name: gateway cloud: nacos: config: server-addr: 10.0.0.1:8848 file-extension: yaml namespace: public group: DEFAULT_GROUP extension-configs: - data-id: qxgd-env.yaml refresh: true group: DEFAULT_GROUP
|
六、网关gateway
1.引入pom依赖
1 2 3 4 5
| <!-- 只有网关需要引入 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
|
2.yml文件添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| spring: cloud: gateway: routes: - id: gdfw-qxgd uri: lb://gdfw-qxgd predicates: - Path=/gdfw-qxgd/** filters: - StripPrefix=1 - SwaggerHeaderFilter discovery: locator: lower-case-service-id: true enabled: true
|