我们接着上次的微服务的项目继续搭建网关组件: 搭建微服务项目
前提准备:
1.打开nacos服务注册中心,在浏览器通过这地址访问
http://10.48.185.7:8848/nacos/index.html
2.启动page和product的微服务
1.新建一个网关的项目
2.导入pom依赖
<!-- Spring Boot父启动器依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- 1Gateway网关 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- 2引入WebFlux --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- 日志依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- 测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Lombok工具 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> <scope>provided</scope> </dependency> <!-- 引入Jaxb开始 --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.2.10-b140310.1920</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- 引入Jaxb结束 --> <!-- Actuator可以帮助你监控和管理Spring Boot应用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- 链路追踪 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!--nacos的客户端依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- Spring Cloud Neflix 公司出品的微服务组件的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--Spring Cloud Alibaba微服务组件的依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> <!-- 打包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
3.编写配置文件
向nacos服务中心注册, application.properties
server.port=3300 spring.application.name=leq-cloud-gateway spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
编写网关的配置,application.yml
spring: cloud: gateway: # gateway网关配置 routes: # 配置路由 - id: service-page-router # 动态路由:从(消费)注册中心获取对应服务的实例 uri: lb://leq-server-page predicates: # 当断言匹配成功后,则将请求转发给某个微服务 - Path=/**
4.创建启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class GatewapApplication { public static void main(String[] args) { SpringApplication.run(GatewapApplication.class,args); } }
5.启动该项目,可以看到我们的网关服务注册到中心了
6.给page微服务中添加一个接口
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("page") public class PageController { @RequestMapping("find") public String find(){ return "获取message"; } }
7.重启page的微服务,访问刚刚写的接口
http://localhost:3100/page/find
将端口改为网关的端口重新访问 ,也是可以通过我们配置的网关来访问的
8.对访问的主机进行拦截
import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.ArrayList; import java.util.List; //定义全局过滤器,会对所有路由生效 @Slf4j @Component public class BlacklistGlobalFilter implements GlobalFilter, Ordered { //创建黑名单集合 private static List<String> blacklist =new ArrayList<>(); //添加黑名单 static{ // blacklist.add("127.0.0.1"); blacklist.add("10.48.185.7"); } @Override//编写过滤规则 exchange 封装了request和response对象 chain网关过滤器链(全局过滤器,单路由过滤器) Mono对象 public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { //获取请求和响应对象 ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); //通过request对象获取ip String clientIP = request.getRemoteAddress().getHostString(); System.out.println("ip:"+clientIP); if(blacklist.contains(clientIP)){ // 状态码 response.setStatusCode(HttpStatus.UNAUTHORIZED); DataBuffer wrap = response.bufferFactory().wrap("Request be denined!".getBytes()); //拒绝访问,返回执行的结果数据 return response.writeWith(Mono.just(wrap)); } //合法请求放行 return chain.filter(exchange); } @Override//返回值表示当前过滤器过滤的顺序,数字越小越靠前 public int getOrder() { return 0; } }
9.重启网关的微服务,被我们添加到黑名单的ip就无法访问我们的接口了