一.微服务架构
- SOA架构演变而来
- 解决了Webservice,ESB总线架构的性能差的问题
- 引入一个服务注册中心,(中心话服务注册中心)
- 不再使用xml传输数据,而是使用JSON来传输数据
- 将服务再进行一次精细化(细粒度服务组件),使用HTTP协议,使用RESTFul风格开发模式
⼆.微服务如何拆分的问题
2.1 原则
- 如果某⼀个模块不能再细分,职责很单⼀,那么就可以成为微
- 每⼀个服务运⾏在⼀个独⽴进
- 每⼀个服务有⾃⼰的⼀个数据库存储,缓存系统,消息队列..
2.2 微服务的原始⽂
https://www.cnblogs.com/liuning8023/p/4493156.html
三.SpringCloud微服务架构
3.1Spring Cloud概述
- Spring Boot基础之上开发的⼀套微服务框架。微服务完整解决⽅案。
- 服务治理、注册与发现、配置管理、跟踪管理、断路器,路由、负载均衡,控制总线,微代理...
- https://spring.io/projects/spring-cloud
- https://springcloud.cc/
- Spring Cloud⼏⼤组件(五⼤神兽)
- Spring Cloud Config:分布式配置中⼼
- Spring Cloud netflix
- Eureka:服务的发现与注册
- Hystrix:服务熔断,服务的保护
- Ribbon:负载均衡
- Feign:声明式服务调⽤组件
- Zuul:⽹关组件,提供智能路由
四.服务治理Eureka
4.1 服务的治理
- 服务之间的依赖关系,管理成本很⾼,需要有⼀个中间件(注册中⼼)来管理依赖关系
- 实现服务的发现与注册,服务调⽤,负载均衡,容错
4.2 服务的发现与注册
- 服务的提供⽅注册服务(注册中⼼)。当服务器启动时服务注册到注册中⼼(Eureka Server)。
- Eureka Server将服务地址进⾏维护。以别名的⽅式注册到注册中⼼
- 服务的消费⽅在注册中⼼上获取到服务列表
- 以RPC(HttpClient)调⽤服务
4.3 搭建注册中⼼
- 编写pom.xml
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.2.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 <groupId>com.zf.mo</groupId> 8 <artifactId>springcloud-eureka</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <name>springcloud-eureka</name> 11 <description>Demo project for Spring Boot</description> 12 13 <properties> 14 <java.version>1.8</java.version> 15 </properties> 16 17 <dependencies> 18 22 <dependency> 23 <groupId>org.springframework.cloud</groupId> 24 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-devtools</artifactId> 30 <scope>runtime</scope> 31 <optional>true</optional> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-test</artifactId> 36 <scope>test</scope> 37 </dependency> 38 </dependencies> 39 40 <dependencyManagement> 41 <dependencies> 42 <dependency> 43 <groupId>org.springframework.cloud</groupId> 44 <artifactId>spring-cloud-dependencies</artifactId> 45 <version>Finchley.SR2</version> 46 <type>pom</type> 47 <scope>import</scope> 48 </dependency> 49 </dependencies> 50 </dependencyManagement> 51 52 <build> 53 <plugins> 54 <plugin> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-maven-plugin</artifactId> 57 </plugin> 58 </plugins> 59 </build>
- 编写application.properties/application.yml
1 server.port=8080 2 #服务名称 3 spring.application.name=springcloud-eureka 4 eureka.instance.hostname=127.0.0.1 5 #服务注册中心地址 6 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ 7 #false 服务注册中心不注册自己 8 eureka.client.register-with-eureka=false 9 # false服务注册中心不检索自己 10 eureka.client.fetch-registry=false
- 运⾏启动类
1 @SpringBootApplication 2 @EnableEurekaServer 3 public class SpringcloudEurekaApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(SpringcloudEurekaApplication.class, args); 7 } 8 9 }
4.4服务提供者搭建
- pom文件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zf.mo</groupId> <artifactId>springcloud-producer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-producer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- 编写application.properties/application.yml
server.port=8180
spring.application.name=springcloud-producer
#服务注册中心地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8080/eureka/
#服务可注册
eureka.client.register-with-eureka=true
#服务可检索
eureka.client.fetch-registry=true
- 运⾏启动类
@SpringBootApplication @EnableDiscoveryClient public class SpringcloudProducerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudProducerApplication.class, args); } }