springcloud-zuul动态网关路由转发

1、基础工程

参考springcloud之zuul网关学习

2、修改springcloud-2.0-zuul工程

2.1 自动刷新

bootstrap.properties

server.port=80

spring.application.name=zuul-server

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=yml
#spring.cloud.nacos.config.group=dev-group
spring.cloud.nacos.config.shared-dataids=common.properties

#zuul.routes.app-member.path=/member/**
#zuul.routes.app-member.serviceId=app-member
#
#zuul.routes.app-order.path=/order/**
#zuul.routes.app-order.serviceId=app-order

#management.endpoints.web.exposure.include=*

AppZuul

package com.mine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class AppZuul {

	public static void main(String[] args) {
		SpringApplication.run(AppZuul.class, args);
	}

	@RefreshScope
	@ConfigurationProperties("zuul")
	public ZuulProperties zuulProperties() {
		return new ZuulProperties();
	}

}

在nacos配置管理中添加【zuul-server】配置文件

zuul.routes.app-member.path=/member/**
zuul.routes.app-member.serviceId=app-member

zuul.routes.app-order.path=/order/**
zuul.routes.app-order.serviceId=app-order

注意:删除app-order或者app-member其中一个,配置会动态加载,但是删除的服务能正常返回结果,可能存在缓存。新增服务或者修改服务地址路径经过尝试,都是没有问题的。

2.2 手动刷新(未尝试,自动刷新已经很简单了,手动相对还复杂一些,没有必要)

手动需要修改的地方:

1.引入spring-boot-starter-actuator
2.AppZuul配置文件动态获取,可以删掉注解【RefreshScope】试试
3.配置文件【bootstrap.properties】修改同自动刷新,需要放开【management.endpoints.web.exposure.include=*】
4.修改完配置文件需要用postman通过post方式请求自动刷新请求
http://localhost/actuator/refresh

你可能感兴趣的:(#,zuul)