SpringCloud从看不懂到放弃,第五章
一、zuul路由网关
1、zuul概述
xml
简述:外部接口的统一访问网关.
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
2、zuul基本配置
1》、新建cloud-zuul-9527 module
2》、POM
zuul服务也是要整合进eureka中的
xml
<dependencies>
<!-- zuul路由网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- actuator监控 -->
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 日常标配 -->
<dependency>
<groupId>com.lee</groupId>
<artifactId>cloud-api</artifactId>
<version>${project.version}</version>
</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>
3》、YML新增
XML
server:
port: 9527
spring:
application:
name: cloud-zuul-gateway
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true
info:
app.name: cloud
author.name: lee
app.function: 网关
build.artifactId: $project.artifactId$
build.version: $project.version$
4》、HOST配置修改
XML
127.0.0.1 zuul9527.com
5》、主启动类
xml
package com.lee.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class Zuul_9527_App {
public static void main(String[] args) {
SpringApplication.run(Zuul_9527_App.class,args);
}
}
测试:
xml
①、启动3个eureka集群
②、启动cloud-provider-dept-8001服务提供者
③、一个路由cloud-zuul-9527
④、访问:
不用路由:http://localhost:8001/dept/get/1
启用路由:http://zuul9527.com:9527/cloud-dept/dept/get/1
注意:zuul和feign或ribbon没有多大关系。feign、ribbon http://localhost7001/consumer/xxx之类的访问和zuul.com:9521/cloud-dept/xxxx访问没有关系。在实际项目中feign\ribbon这些服务的消费方一般也是以服务的提供发而存在的。
3、zuul路由访问映射规则
3.1>、原因:
xml
before:
http://zuul9527.com:9527/cloud-dept/dept/get/1访问路径中cloud-dept微服务名称容易暴露出来,所以我们要将其隐藏置换。
修改:YML
xml
zuul:
routes:
mydept.serviceId: microservicecloud-dept #微服务实例名称
mydept.path: /mydept/** #映射名称
测试:
XML
http://zuul9527.com:9527/mydept/dept/list
3.2>、原因:
XML
before:
http://zuul9527.com:9527/cloud-dept/dept/get/1 还能看到,上面这一步只做了置换,没做隐藏
修改YML
XML
忽略单个:
zuul:
ignored-services: cloud-dept
忽略多个:
zuul:
ignored-services: "*"
测试:
XML
http://zuul9527.com:9527/cloud-dept/dept/get/1
3.3>、原因:
xml
设置一个公共的前缀名
修改YML
XML
zuul:
prefix: /lee
测试:
XML
http://zuul9527.com:9527/lee/mydept/dept/list
如果这样访问不了,可能是yml的问题,在POM文件中引入
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
即可
二、分布式配置中心
1、概述
xml
原因:
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理....../(ㄒoㄒ)/~~
是什么
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
怎么玩
SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
xml
springcloud config 分布式配置中心能干嘛:
1》、集中管理配置文件
2》、不同环境不同配置,动态化的配置更新,分环境部署:比如dev/test/prod/beta/release
3》、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉去配置自己的信息
4》、当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
5》、将配置信息以rest接口的形式暴露
2、与Gitee码云通讯
2.1》、config server服务端配置
xml
①、先在gitee上新建一个cloud-config的新repository
②、获得SSH协议的Git地址
③、本地硬盘目录上新建Git仓库并clone
git clone xxxxxxxxxxxxxxxxxxxxxxxxxxx.git
④、新建一个application.yml(文件内容保存的格式一定要UTF-8)
spring:
profiles:
active:
- dev
---
spring:
profiles: dev #开发环境
application:
name: cloud-config-lee-dev
---
spring:
profiles: test #测试环境
application:
name: cloud-config-lee-test
# 请保存为UTF-8格式
⑤、将上yml文件推送到gitee上
git status #查看状态
git add .
git commit -m "init file"
git push origin master
⑥、新建module模块 cloud-config-3344配置中心模块
⑦、POM
<dependencies>
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</dependency>
<!-- 图形化监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
⑧、yml
server:
port: 3344
spring:
application:
name: cloud-config
cloud:
config:
server:
git:
uri: https://gitee.com/night_wish/cloud-config.git #Gitee上面的git仓库名字https
username: xxxxx
password: ooooo
⑨、主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Config_3344_App
{
public static void main(String[] args)
{
SpringApplication.run(Config_3344_App.class,args);
}
}
⑩、修改HOST
127.0.0.1 config3344.com
①①、启动config 测试是否可以从gitee上获取配置内容
http://config3344.com:3344/application-dev.yml
http://config3344.com:3344/application-test.yml
http://config3344.com:3344/application-xxxxx.yml
或
http://config3344.com:3344/application/dev/master
http://config3344.com:3344/application/test/master
http://config3344.com:3344/application/xxxxx/master
2.2》、config client客户端的配置
XML
①、在config server本地目录下创建cloud-config-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: cloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: cloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/
②、将cloud-config-client.yml提交到gitee中
git status
git add .
git commit -m "cloud-config-client"
git push origin master
③、新建module cloud-config-client-3355
④、POM
<dependencies>
<!-- SpringCloud Config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
⑤、bootstrap.yml
spring:
cloud:
config:
name: cloud-config-client #需要从gitee上读取的资源名称,注意没有yml后缀名
profile: dev #本次访问的配置项
label: master
uri: http://config3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取Gitee的服务地址
⑥、application.yml
spring:
application:
name: cloud-config-client
⑦、修改hosts文件
127.0.0.1 configClient3355.com
⑧、新建controller,验证client端能否通过config server从gitee上读取配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientRest {
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServers;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig()
{
String str = "applicationName: "+applicationName+"\t eurekaServers:"+eurekaServers+"\t port: "+port;
System.out.println("******str: "+ str);
return "applicationName: "+applicationName+"\t eurekaServers:"+eurekaServers+"\t port: "+port;
}
}
⑨、主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClient_3355_App
{
public static void main(String[] args)
{
SpringApplication.run(ConfigClient_3355_App.class,args);
}
}
⑩、测试
启动config配置中心3344微服务 并自测http://config3344.com:3344/application-dev.yml
启动config客户端3355 并自测:
如果profile:dev http://configClient3355.com:8201/config
如果profile:test http://configClient3355.com:8202/config
BOOTSTRAP.YML是什么
XML
applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
Spring Cloud会创建一个`Bootstrap Context`,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,
所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。