SpringCloud从零构建(七)——网关中心(gateway)配置
网关中心(gateway)配置
一)创建项目
package com.glen.gatewayserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.glen")
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication{
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
5、接下来配置application.properties文件。如图4所示:
spring.application.name=gateserver-server eureka.client.service-url.defaultZone= http://localhost:8081/eureka/ server.port=8084 eureka.instance.prefer-ip-address=true ## 网关配置 # 配置通过serverid小写访问 spring.cloud.gateway.discovery.locator.enabled=true spring.cloud.gateway.locator.lowerCaseServiceId=true # 测试路由 # 路由名称 spring.cloud.gateway.routes[0].id=test_route # 这个是服务名称spring.application.name,全部拦截到这里,lb代表从注册中心获取服务 spring.cloud.gateway.routes[0].uri=lb://app-customer-login/user/login # 要拦截的地址 spring.cloud.gateway.routes[0].predicates[0]=Path=/** # 去掉路径的 n 个前缀 spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1到此网关就搭建完成了,是不是感觉很简单,主要还是配置内容比较麻烦。
三)测试项目
接下来我们做测试: 1、首先打开之前已经几节课写好的模块,在controller里新增一个方法作为测试,如下图所示: 代码如下:
主要讲基于OAuth2.0+JWT的鉴权中心。
github地址: 如果有问题欢迎小伙伴留言和我沟通交流。
