sringcloud2.0学习-17- 通过springcloud config分布式配置中心搭建Zuul 动态网关

1.动态网关的意义

在上一篇文章中,网关路由转发规则如下,是写死在网关的配置文件里面的
在这里插入图片描述
如果网关服务启动之后,要新增一个服务的网关路由转发规则,这时只能通过重启网关服务了,这种方式太不靠谱,结合前面的文章分布式配置中心springcloud config 可以把网关配置中的路由转发规则放到分布式配置中心上,这样就可以动态路由规则了。

2.分布式配置中心新增动态路由规则配置

在git上创建一个文件service-zuul-dev.yml ,内容就是原来配置文件中的路由转发规则
在这里插入图片描述

3. 网关项目改造
  • pom加入spring-boot-starter-actuator和分布式配置中心configClient依赖
<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>
	<groupId>com.lchtest</groupId>
	<artifactId>springcloud2.0-zuul-apigateway</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
	    <!-- SpringBoot整合zuul网关服务 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		
		<!-- 整合分布式配置中心begin -->
		<!-- actuator监控中心 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- springcloud config 2.0 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
		<!-- 整合分布式配置中心end -->
	</dependencies>
</project>
  • 配置文件改造
    加入分布式配置中心的配置,从ConfigServer读取git上的动态路由转发配置
######## 搭建动态网关时使用  ########
#注册中心
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: "*"
  • 启动类改造:
    加上@RefreshScope注解,动态刷新git配置
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();
	}
}
4.分布式配置中心ConfigServer

这里为了区分,新建一个configserver项目springcloud2.0-zuul-apigateway-configserver

  • pom 整合spring-cloud-config-server 和eureka客户端spring-cloud-starter-netflix-eureka-client
<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>
	<groupId>com.lchtest</groupId>
	<artifactId>springcloud2.0-zuul-apigateway-configserver</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!--spring-cloud 整合 config-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</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);
	}
}
5.动态网关测试

网关服务改造完成,配置中心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 这个类中。

6.动态刷新网关配置

此时我在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.0</modelVersion>
	<groupId>com.lchtest</groupId>
	<artifactId>springcloud2.0-pay</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值