文章目录
Zuul路由网关
概述:
Zuul包含了对请求的路由和过滤两大重要功能:
- 路由功能:负责将外部请求转发到具体微服务上,是实现外部访问同一入口的基础
- 过滤器功能:负责对请求的过程处理进行干预,是实现请求校验,服务聚合等功能的基础。
Zuul和Eureka整合,将zuul自身注册未eureka服务治理下的应用,同时从eureka中获得其他微服务消息,即以后访问微服务都是通过zuul跳转后获得。
注意:Zuul服务最终还是会注册到Eureka
提供:代理 + 路由 + 过滤三大功能。
案例代码
导入依赖
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
编写配置
server:
port: 6662
spring:
application:
name: cloud1-zuul
# 添加ribbon的超时时间设置
ribbon:
ReadTimeout: 3000
ConnectTimeout: 3000
eureka:
client:
service-url:
defaultZone: http://localhost:8881/eureka/,http://localhost:8882/eureka/
instance:
instance-id: zuul-6662
prefer-ip-address: true
zuul:
routes:
# 配置后访问地址 http://127.0.0.1:6662/order/getOrder
order.serviceId: CLOUD1-PROVIDER
order.path: /order/**
ignored-services: "*" # 忽略所有真实的服务访问
prefix: /hx # 配置访问前缀 访问地址:http://127.0.0.1:6662/hx/order/getOrder
使用注解,开启Zuul代理
@SpringBootApplication
@EnableZuulProxy
public class ZuulApp {
public static void main(String[] args) {
SpringApplication.run(ZuulApp.class, args);
}
}
Config配置中心
分布式系统面临的问题——配置文件的问题
微服务意味着要将单体应用中的业务拆分成一个个的子模块,每个模块的粒度相对较小,因此系统会出现大量的服务,由于每个服务都需要必要的配置信息才可以运行,所以集中一套管理,动态的配置管理设施必不可少。
SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带有一个application.yml,那么上百的配置文件要修起来,管理困难麻烦。
什么是SpringCloud Config分布式配置中心
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。
SpringCloudConfig分为服务端和客户端俩个部分:
服务端:也成为分布式配置中心,是一个独立的微服务应用,用来连接配置服务器,并为客户端提供获取配置信息、加密、解密信息等访问接口。
客户端:通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,有助于对环境配置进行版本管理。并且可以通过git客户端工具来方便的管理和访问配置内容。
作用:
- 集中管理配置文件
- 不同环境,不同配置,动态化的配置更新,分环境部署/dev、/test、/prd
- 运行期间动态调整配置,无需在每个服务器上编写配置文件,服务会向注册中心统一的拉去配置自己的信息
- 当配置发生变动时,服务不需要重启,动态感知配置变化,并应用最新配置文件
- 将配置信息以Rest接口形式暴露
SpringCloudConfig分布式配置中心与Gitee的整合
新建仓库,并在仓库中编写配置文件
SpringCloud Config Server案例代码
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
编写配置
server:
port: 6663
spring:
application:
name: cloud1-config-server
# 连接远程仓库
cloud:
config:
server:
git:
uri: https://gitcode.net/huathy/cloud1-config.git
# username:
# password:
开启注解 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
@RestController
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
@RequestMapping("/")
public String index(){
return "hi config server";
}
}
访问测试
http://localhost:6663/app-dev.yml
http://localhost:6663/master/app.yml
http://localhost:6663/master/app
SpringCloud Config Client案例代码
bootstrap.yml与application.yml区别:bootstrap.yml是系统级别配置,相较于application的级别更高。
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
编写配置
- bootstrap.yml
# 系统级别配置,相较于application的级别更高
spring:
cloud:
config:
uri: http://localhost:6663
name: config-server # 需要从git读取的资源名称
profile: dev
label: master
- application.yml
# 用户级别配置
spring:
application:
name: spring-cloud-client-1
编写启动类
@SpringBootApplication
@RestController
public class ConfigClientApp {
@Value("${server.port}")
private String port;
public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class, args);
}
@RequestMapping("/")
public String index(){
return "hi config client : " + port;
}
}
总结:
注册中心 Eureka:(AP原则)(集群配置,对比Zookeeper)
负载均衡 Ribbon:(Irule)
负载均衡 Feign:(面向接口编程)
断路器 Hystrix:熔断降级
网关 Zuul
基本流程
导入依赖 -> 编写配置 -> @EnableXXX