spring cloud springcloud是一个生态,它是分布式微服务架构下的一站式解决方案。它基于springboot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件
springcloud将各家公司开发比较成熟,经得起实际考验的服务框架组合起来,通过Springboot的风格进行再封装,屏蔽掉了复杂的配置和实现原理,最终给开发者留下了一套简单易懂,易部署和维护的分布式系统开发工具包
Spring Cloud 版本命名方式采用了伦敦地铁站的名称
软件架构 单体架构 全部功能集中在一个项目内(All in one)
垂直架构 按照业务进行切割,形成小的单体项目
分布式架构 将服务拆分,每一个service代表一个单独的功能模块
SOA架构 把业务逻辑抽象成可复用的服务,通过服务的编排实现业务的快速再生,目的:把原 先固有的业务功能转变为通用的业务服务,实现业务逻辑的快速复用
微服务架构 将系统服务层完全独立出来,抽取为一个一个的微服务
服务注册与发现 Eureka Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。
Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。
Eureka-server
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 <properties > <maven.compiler.source > 11</maven.compiler.source > <maven.compiler.target > 11</maven.compiler.target > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <project.reporting.outputEncoding > UTF-8</project.reporting.outputEncoding > <java.version > 11</java.version > <spring-cloud.version > Hoxton.SR12</spring-cloud.version > <lombok.version > 1.18.22</lombok.version > </properties > <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 > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > ${lombok.version}</version > </dependency > </dependencies > </dependencyManagement >
1 2 3 4 5 6 7 8 9 10 11 12 <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-server</artifactId > </dependency > </dependencies >
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 server: port: 8761 eureka: instance: hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka register-with-eureka: false fetch-registry: false dashboard: enabled: true path: / server: enable-self-preservation: false eviction-interval-timer-in-ms: 3000 spring: application: name: eureka-server
1 2 3 4 5 6 7 8 @SpringBootApplication @EnableEurekaServer public class eurekaApplication { public static void main (String[] args) { SpringApplication.run(eurekaApplication.class, args); } }
Eureka-client eureka-provider
1 2 3 4 5 6 7 8 9 10 11 <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 > </dependencies >
1 2 3 4 5 6 7 8 @SpringBootApplication @EnableEurekaClient public class ProviderApplication { public static void main (String[] args) { SpringApplication.run(ProviderApplication.class,args); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 server: port: 8080 eureka: instance: hostname: localhost prefer-ip-address: true ip-address: 127.0 .0 .1 instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} lease-renewal-interval-in-seconds: 3 lease-expiration-duration-in-seconds: 9 client: service-url: defaultZone: http://localhost:8761/eureka spring: application: name: eureka-provider
eureka-consumer
1 2 3 4 5 6 7 8 9 <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 >
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 server: port: 8082 spring: application: name: eureka-consumer eureka: instance: hostname: localhost client: service-url: defaultZone: http://localhost:8761/eureka eureka-provider: ribbon: NFLoadBalancerRuleClassName: com.dream.xiaobo EUREKA-PROVIDER: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule ribbon: ConnectTimeout: 1000 ReadTimeout: 10000 logging: level: com.dream.xiaobo: debug feign: hystrix: enabled: true
1 2 3 4 5 6 7 8 9 @SpringBootApplication @EnableEurekaClient public class ConsumerApplication { public static void main (String[] args) { SpringApplication.run(ConsumerApplication.class,args); } }
高可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 server: port: 8761 eureka: instance: hostname: eureka-server1 client: service-url: defaultZone: http://eureka-server2:8762/eureka register-with-eureka: true fetch-registry: true spring: application: name: eureka-server-ha
1 2 3 4 5 6 7 8 9 10 11 12 13 14 server: port: 8762 eureka: instance: hostname: eureka-server2 client: service-url: defaultZone: http://eureka-server1:8761/eureka register-with-eureka: true fetch-registry: true spring: application: name: eureka-server-ha
Zookeeper zookeeper是一个分布式协调工具,可以实现注册中心功能。dubbo,大数据组件hadoop,hive,kafka。
Zookeeper服务启动
官网下载Zookeeper
修改配置文件,改成zoo.cfg
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 # The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=D:/Apache/apache-zookeeper-3.8.0-bin/data dataLogDir=D:/Apache/apache-zookeeper-3.8.0-bin/logs # the port at which the clients will connect clientPort=2181 #admin.serverPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpHost=0.0.0.0 #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=true
Zookeeper使用 Zookeeper-provider
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 <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-zookeeper-discovery</artifactId > <exclusions > <exclusion > <groupId > org.apache.zookeeper</groupId > <artifactId > zookeeper</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.apache.zookeeper</groupId > <artifactId > zookeeper</artifactId > <version > 3.8.0</version > <exclusions > <exclusion > <groupId > org.slf4j</groupId > <artifactId > slf4j-log4j12</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > </dependency > </dependencies >
1 2 3 4 5 6 7 8 9 server: port: 8004 spring: application: name: zookeeper-provider cloud: zookeeper: connect-string: 127.0 .0 .1 :2181
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RestController @RequestMapping("/goods") public class GoodsController { @Autowired private GoodsService goodsService; @GetMapping("/findById/{id}") public Goods findById (@PathVariable("id") Integer id) { return goodsService.findById(id); } }
1 2 3 4 5 6 7 8 9 @SpringBootApplication @EnableDiscoveryClient public class ZookProviderApp { public static void main (String[] args) { SpringApplication.run(ZookProviderApp.class, args); } }
Zookeeper-consumer
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 <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-zookeeper-discovery</artifactId > <exclusions > <exclusion > <groupId > org.apache.zookeeper</groupId > <artifactId > zookeeper</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.apache.zookeeper</groupId > <artifactId > zookeeper</artifactId > <version > 3.8.0</version > <exclusions > <exclusion > <groupId > org.slf4j</groupId > <artifactId > slf4j-log4j12</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > </dependency > </dependencies >
1 2 3 4 5 6 7 8 9 server: port: 8005 spring: application: name: zookeeper-consumer cloud: zookeeper: connect-string: 127.0 .0 .1 :2181
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 @RestController @RequestMapping("/order") public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/add/{id}") public Goods addOrder (@PathVariable("id") Integer id) { List<ServiceInstance> instances = discoveryClient.getInstances("zookeeper-provider" ); ServiceInstance instance = instances.get(0 ); String host = instance.getHost(); int port = instance.getPort(); String url = "http://" + host +":" + port +"/goods/findById/" + id; Goods goods = restTemplate.getForObject(url, Goods.class); return goods; } }
1 2 3 4 5 6 7 8 9 @Configuration public class RestTemplateConfiguration { @Bean public RestTemplate restTemplate () { return new RestTemplate(); } }
1 2 3 4 5 6 7 8 @SpringBootApplication @EnableDiscoveryClient public class ZookConsumerApp { public static void main (String[] args) { SpringApplication.run(ZookConsumerApp.class,args); } }
Consul Consul 是由 HashiCorp 基于 GoLanguage 语言开发的,支持多数据中心,分布式高可用的服务发布和注册服务软件
Consul服务启动
Consul使用 consul-provider
1 2 3 4 5 6 7 8 9 10 11 12 <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-consul-discovery</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > </dependencies >
1 2 3 4 5 6 7 8 9 10 11 12 server: port: 8006 spring: application: name: consul-provider cloud: consul: host: 127.0 .0 .1 port: 8500 discovery: service-name: ${spring.application.name}
1 2 3 4 5 6 7 8 9 @SpringBootApplication @EnableDiscoveryClient public class ConsulProviderApplication { public static void main (String[] args) { SpringApplication.run(ConsulProviderApplication.class, args); } }
consul-consumer
1 2 3 4 5 6 7 8 9 10 11 <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-consul-discovery</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > </dependencies >
1 2 3 4 5 6 7 8 9 10 11 12 server: port: 8007 spring: application: name: consul-consumer cloud: consul: host: 127.0 .0 .1 port: 8500 discovery: service-name: ${spring.application.name}
1 2 3 4 5 6 7 8 9 @SpringBootApplication @EnableDiscoveryClient public class ConsulConsumerApplication { public static void main (String[] args) { SpringApplication.run(ConsulConsumerApplication.class, args); } }
三个注册中心的异同
组件
语言
cap
健康检查
暴露接口
cloud集成
eureka
java
ap
支持
http
已经集成
zookeeper
java
cp
支持
tcp
已经集成
consul
go
cp
支持
http
已经集成
cap c
: consustency 强一致性
a
: avalibility 可用性
p
: partition tolerance 分区容忍性
正确的开始,微小的长进,然后持续,嘿,我是小博,带你一起看我目之所及的世界……