在上一篇文章中,网关路由转发规则如下,是写死在网关的配置文件里面的
如果网关服务启动之后,要新增一个服务的网关路由转发规则,这时只能通过重启网关服务了,这种方式太不靠谱,结合前面的文章分布式配置中心springcloud config 可以把网关配置中的路由转发规则放到分布式配置中心上,这样就可以动态路由规则了。
在git上创建一个文件service-zuul-dev.yml ,内容就是原来配置文件中的路由转发规则
<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.0modelVersion>
<groupId>com.lchtestgroupId>
<artifactId>springcloud2.0-zuul-apigatewayartifactId>
<version>0.0.1-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-zuulartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-clientartifactId>
dependency>
dependencies>
project>
######## 搭建动态网关时使用 ########
#注册中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
server:
port: 80
#网关名称
spring:
application:
name: service-zuul
# zuul动态网关从git上读取配置文件的版本
# git配置文件地址:https://gitee.com/liuch890228/distributed_profile_learning/blob/master/configtest/service-zuul-dev.yml
cloud:
config:
#读取的版本,通过后缀确定是开发环境or测试环境or生产环境
profile: dev
discovery:
#读取config-server在注册中心上注册的别名
service-id: config-server
#开启读取权限
enabled: true
# zuul动态网关-开启监控中心所有端口
management:
endpoints:
web:
exposure:
include: "*"
package com.lchtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}
// zuul配置能够使用config实现实时更新
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
}
这里为了区分,新建一个configserver项目springcloud2.0-zuul-apigateway-configserver
<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.0modelVersion>
<groupId>com.lchtestgroupId>
<artifactId>springcloud2.0-zuul-apigateway-configserverartifactId>
<version>0.0.1-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
project>
这里有几个配置需要重点关注:
1.配置文件的git地址
2.configserver在git仓库查找配置的路径,search-paths的值就是配置文件的所属目录名
3.要读取的配置在git上的分支标签,git上创建配置时默认用的master分支
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
spring:
application:
#configserver在注册中心的应用名称
name: config-server
cloud:
config:
server:
git:
###git环境地址
uri: https://gitee.com/liuch890228/distributed_profile_learning.git
####搜索目录,自己在码云上的工程的仓库里面创建的目录名称
search-paths:
- configtest
#读取的配置的分支
label: master
# 码云创建仓库时选择的是公开,这里不用配置密码
#configserver服务的端口号
server:
port: 8888
package com.lchtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* zuul动态网关-分布式配置中心ConfigServer
* 1.git上创建配置文件
* 2.依次启动eureka注册中心,AppGatewayConfigServer
* @author pc
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class AppGatewayConfigServer {
public static void main(String[] args) {
SpringApplication.run(AppGatewayConfigServer.class, args);
}
}
网关服务改造完成,配置中心configServer也搭建好,此时动态网关和SpringCloudConfig的关系如下:
COnfigServer作为分布式配置中心的服务端,Zuul网关作为ConfigServer的客户端,读取配置,当配置文件有更新,通过网关调用/actuator/refresh接口获取最新配置。
分别启动eureka注册中心,springcloud2.0-zuul-apigateway-configserver项目,API网关项目,member服务,order服务:
访问http://localhost:8888/service-zuul-dev.yml 读取配置文件中的路由转发信息,说明configserver 没有问题了。
接下来看看通过网关能不能正常访问到服务:
Tips,查看网关重启时日志:
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
c.c.c.ConfigServicePropertySourceLocator : Located environment: name=service-zuul, profiles=[dev], label=null, version=e636d0697ddf8919f58ff2b85f53419db1336f5f, state=null
b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/liuch890228/distributed_profile_learning.git/configtest/service-zuul-dev.yml'}]}
可以发现,在网关启动时,会从configServer(端口8888)去拉取配置信息,得到git的地址是https://gitee.com/liuch890228/distributed_profile_learning.git ,要拉取的配置所在的目录:configtest ,
而网关启动日志中 Located environment: name=service-zuul, profiles=[dev] 这个配置是网关服务自己的配置,最终,会把从configServer获取的配置和网关自己的配置拼起来,获得一个完整的git配置地址:https://gitee.com/liuch890228/distributed_profile_learning.git/configtest/service-zuul-dev.yml,保存到MapPropertySource 这个类中。
此时我在git上面再添加一个pay支付服务的路由转发规则:
并创建一个pay服务:
<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.0modelVersion>
<groupId>com.lchtestgroupId>
<artifactId>springcloud2.0-payartifactId>
<version>0.0.1-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
project>
server:
port: 8006
spring:
application:
name: app-pay
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
#把自己注册到注册中心
register-with-eureka: true
# 从eureka上获取注册服务列表
fetch-registry: true
package com.lchtest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class AppPay {
public static void main(String[] args) {
SpringApplication.run(AppPay.class, args);
}
@Value("${server.port}")
private String serverPort;
@GetMapping("/")
public String index() {
return "Pay service port=" + serverPort;
}
}
启动pay服务,直接去访问网关http://localhost/api-pay?userToken=sss
会报404错误,因为网关获取到的配置还是旧的,通过网关服务器调用/actuator/refresh接口手动刷新网关服务的配置文件:
再次访问http://localhost/api-pay?userToken=sss
点击打开代码地址
使用到的项目:
springcloud2.0-zuul-apigateway 网关服务(分布式配置中心客户端)
springcloud2.0-zuul-apigateway-configserver 分布式配置中心服务端
springcloud2.0-eureak-server(单注册中心)
springcloud2.0-feign-parent( order服务, member服务)
springcloud2.0-pay