六、zuul路由网关
1、概述
Zuul包含了对请求的==路由和过滤==的主要功能:
路由功能
主要负责将外部请求转发到具体的微服务上,是实现外部访问入口的基础
过滤功能
负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础
Zuul与Eureka进行整合,将==Zuul自身注册为Eureka==服务治理下的应用,同事从Eureka中获得其他微服务的信息,也即以后的微服务访问都是通过Zuul跳转后获得。
==三大功能—-==代理、路由、过滤
2、路由基本配置
新建springcloud-study-zuul-gateway-9527模块
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
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gxs.springcloud</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>springcloud-study-zuul-gateway-9527</artifactId>
<dependencies>
<dependency>
<groupId>com.gxs.springcloud</groupId>
<artifactId>springcloud-study-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Hystrix容错-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>yml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21server:
port: 9527
spring:
application:
name: springcloud-study-zuul-gateway
eureka:
instance:
instance-id: gateway-9572
prefer-ip-address: true
client:
service-url:
# 单机版配置defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
info:
app.name: springcloud-study-zuul-gateway
company.name: www.gxs.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}host文件修改
127.0.0.1 myzuul.com
主启动类
1
2
3
4
5
6
7
public class ZuulGatewayApp {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApp.class,args);
}
}
3、测试
- 不启用路由:http://localhost:8001/dept/findAll
- 启用路由:http://myzuul.com:9527/STUDY-SPRINGCLOUD-DEPT/dept/findAll
4、路由访问映射规则
1、代理名称
修改yml,增加如下配置
配置后http://myzuul.com:9527/STUDY-SPRINGCLOUD-DEPT/dept/findAll
可变为http://myzuul.com:9527/mydept/dept/findAll
1
2
3
4zuul:
routes:
mydept.serviceId: STUDY-SPRINGCLOUD-DEPT
mydept.path: /mydept/*原真实服务名忽略
1
2
3
4
5
6zuul:
routes:
mydept.serviceId: STUDY-SPRINGCLOUD-DEPT
mydept.path: /mydept/*
#多个用"*" ignored-services: "*"
ignored-services: STUDY-SPRINGCLOUD-DEPT设置统一公共前缀
1
2
3
4
5
6
7
8zuul:
routes:
mydept.serviceId: STUDY-SPRINGCLOUD-DEPT
mydept.path: /mydept/*
#多个用"*" ignored-services: "*"
ignored-services: STUDY-SPRINGCLOUD-DEPT
#公共前缀
prefix: /springcloud
七、SpringCloud Config 分布式配置中心
1、概述
分布式系统面临问题,配置问题
集中式的、动态的配置管理必不可少
Spring colud Config为微服务架构中心的微服务提供==集中式的外部配置支持==,配置服务器==为各个不同的微服务应用==提供一个==中心化的外部配置==
Spring colud Config分为==服务端和客户端两部分==
服务端
也成为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并未客户端提供获取配置等访问接口
客户端
通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取配置,配置服务器默认采用git来存储配置信息。这样有助于对环境配置的版本管理,并且可以通过git客户端访问配置内容
2、功能
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署,dev/test/prod/beta/release
当配置发生变化是,服务不需要重启
运行期间动态调整配置
将配置信息以rest接口形式暴露
与github、svn整合
2、SpringCloud Config 服务端配置
1、在github上新建springcloud-study-config的repository
https://github.com/gongxings/springcloud-study-config.git
2、在本地硬盘中新建git仓库并clone
git clone https://github.com/gongxings/springcloud-study-config.git
3、在本地git仓库中新建一个application.yml
格式必须是UTF-8的形式保存,否则会有乱码
提交到github
4、新建module,springcloud-study-config-3344
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
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gxs.springcloud</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>springcloud-study-config-3344</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Hystrix容错-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>yml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14server:
port: 3344
spring:
application:
name: springcloud-study-config
cloud:
config:
server:
git:
# uri: git@github.com:gongxings/springcloud-study-config.git
uri: https://github.com/gongxings/springcloud-study-config.git
search-paths: config-repo
username: #账号
password: #密码主启动类
1
2
3
4
5
6
7
public class Config3344App {
public static void main(String[] args) {
SpringApplication.run(Config3344App.class,args);
}
}修改host文件增加映射
127.0.0.1 config3344.com
启动服务,访问yml
3、SpringCloud Config 客户端配置与测试
1、在本地git仓库中新建一个springcolud-study-config.yml
1 | server: |
4、新建springcloud-study-config-client-3355模块
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
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gxs.springcloud</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>springcloud-study-config-client-3355</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
</dependency>
<!--zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Hystrix容错-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>主启动类
1
2
3
4
5
6
public class ConfigClient3355App {
public static void main(String[] args) {
SpringApplication.run(ConfigClient3355App.class,args);
}
}yml文件
application.yml是用户级的资源配置项
bootstrap.xml是系统级的,优先级更高
Springcolud 会创建一个BootStrap Context,作为spring应用的Application Context的父上下文。初始化的时候,BootStrap Context负责从外部资源加载配置属性并解析配置。这两个上下文共享一个外部获取的Environment。BootStrap 属性有高优先级,默认情况下,他们不会被本地配置覆盖。BootStrap Context和Application Context有着不同的约定。
所以新增一个bootstrap.xml,保证BootStrap Context和Application Context的配置分离。
application.yml
1
2
3spring:
application:
name: springcloud-study-config-clientbootstrap.xml
1
2
3
4
5
6
7spring:
cloud:
config:
name: springcloud-study-config-client #需要从github上服务的资源名称,注意没有yml后缀
profile: dev #本次访问的配置项
label: master
uri: http://config3344.com:3344 #本次服务启动后,先去找3344服务,通过SpringCloudConfig获取github上的配置
host文件配置
127.0.0.1 config-client.com
新建rest类,验证是否能从Github上读取配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ConfigClientRest {
"${spring.application.name}") (
private String applicationName;
"${eureka.client.server-url.defaultZone}") (
private String eurekaServers;
"${server.port}") (
private String port;
"/config") (
public String getConfig(){
System.out.println("applicationName:"+this.applicationName
+"eurekaServers:"+this.eurekaServers
+"port:"+this.port);
return "applicationName:"+this.applicationName
+"eurekaServers:"+this.eurekaServers
+"port:"+this.port;
}
}测试
启动3344、3355
4、配置实战
config服务端配置测试OK,我们可以和config+github进行配置修改并获得内容
做一个wureka服务+一个dept访问的微服务,将两个微服务的配置统一由github获得实现统一配置,分布式管理,完成多环境变更。
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!