spring cloud 一

上岸

spring cloud

springcloud是一个生态,它是分布式微服务架构下的一站式解决方案。它基于springboot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件

springcloud将各家公司开发比较成熟,经得起实际考验的服务框架组合起来,通过Springboot的风格进行再封装,屏蔽掉了复杂的配置和实现原理,最终给开发者留下了一套简单易懂,易部署和维护的分布式系统开发工具包

Spring Cloud 版本命名方式采用了伦敦地铁站的名称

软件架构

单体架构

单体架构
全部功能集中在一个项目内(All in one)

垂直架构

垂直架构
按照业务进行切割,形成小的单体项目

分布式架构

分布式架构
将服务拆分,每一个service代表一个单独的功能模块

SOA架构

SOA架构
把业务逻辑抽象成可复用的服务,通过服务的编排实现业务的快速再生,目的:把原
先固有的业务功能转变为通用的业务服务,实现业务逻辑的快速复用

微服务架构

微服务架构
将系统服务层完全独立出来,抽取为一个一个的微服务

服务注册与发现

Eureka

Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。

Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。

Eureka-server

  • 父工程pom
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>
  • eureka-server
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>
  • application.yml
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 配置
# eureka 一共有4部分 配置
# 1. dashboard:eureka的web控制台配置
# 2. server:eureka的服务端配置
# 3. client:eureka的客户端配置
# 4. instance:eureka的实例配置

eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
register-with-eureka: false # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: false # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
dashboard:
enabled: true # 是否开启图形化页面监控
path: / # 图形化监控路径
server:
enable-self-preservation: false # 关闭自我保护机制
eviction-interval-timer-in-ms: 3000 # 检查服务的时间间隔
spring:
application:
name: eureka-server
  • app
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

  • pom
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>
  • app
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
  • application.yml
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注册到eureka server 中。默认是false 注册主机名
ip-address: 127.0.0.1 # 设置当前实例的ip
instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # 设置web控制台显示的 实例id
lease-renewal-interval-in-seconds: 3 # 每隔3 秒发一次心跳包
lease-expiration-duration-in-seconds: 9 # 如果9秒没有发心跳包,服务器呀,你把我干掉吧~
client:
service-url:
# defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
defaultZone: http://localhost:8761/eureka
spring:
application:
name: eureka-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径

eureka-consumer

  • pom
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>
  • application.yml
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://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka
defaultZone: http://localhost:8761/eureka

eureka-provider:
ribbon:
NFLoadBalancerRuleClassName: com.dream.xiaobo

EUREKA-PROVIDER: #远程服务名
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #策略


# 设置Ribbon的超时时间
ribbon:
ConnectTimeout: 1000 # 连接超时时间 默认1s
ReadTimeout: 10000 # 逻辑处理的超时时间 默认1s

# 设置当前的日志级别 debug,feign只支持记录debug级别的日志
logging:
level:
com.dream.xiaobo: debug

#开启feign对hystrix支持
feign:
hystrix:
enabled: true


  • app
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableEurekaClient //开启Eureka客户端
//@RibbonClient(name = "eureka-provider",configuration = MyRule.class)
public class ConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}

高可用

  • eureka-server1
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 # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: true # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要

spring:
application:
name: eureka-server-ha
  • eureka-server2
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 # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: true # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
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
  • 进入bin目录下cmd启动zkServer.cmd

  • 服务启动成功

Zookeeper使用

Zookeeper-provider

  • pom
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>
<!--springcloud 整合 zookeeper 组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--zk发现-->
<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>
  • application.yml
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 # zk地址
  • controller
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);
}

}
  • app
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

  • pom
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>
<!--springcloud 整合 zookeeper 组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--zk发现-->
<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>
  • application.yml
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 # zk地址
  • controller
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;

// discoveryClient可以根据服务名从注册中心中拿到ip和端口号
@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;
}

}
  • RestTemplateConfig
1
2
3
4
5
6
7
8
9
@Configuration
public class RestTemplateConfiguration {

@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}

}
  • app
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.exe agent -dev启动

Consul使用

consul-provider

  • pom
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>
  • application.yml
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}
  • app
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

  • pom
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>
  • application.yml
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}
  • app
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 分区容忍性

正确的开始,微小的长进,然后持续,嘿,我是小博,带你一起看我目之所及的世界……

-------------本文结束 感谢您的阅读-------------

本文标题:spring cloud 一

文章作者:小博

发布时间:2022年10月08日 - 17:11

最后更新:2022年10月08日 - 17:12

原始链接:https://codexiaobo.github.io/posts/243992453/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。