Gateway网关配置完成
This commit is contained in:
parent
ea08aaa2a9
commit
c1bcb68295
@ -1,9 +1,7 @@
|
|||||||
package com.test;
|
package com.test;
|
||||||
|
|
||||||
import com.apple.eawt.Application;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
|
||||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package com.test.controller;
|
package com.test.controller;
|
||||||
|
|
||||||
import com.test.service.BookService;
|
import com.test.service.BookService;
|
||||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import com.test.entity.Book;
|
import com.test.entity.Book;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,14 +18,15 @@ import com.test.entity.Book;
|
|||||||
* @Create 2023/8/14 17:11
|
* @Create 2023/8/14 17:11
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@EnableEurekaClient
|
|
||||||
public class BookController {
|
public class BookController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
BookService service;
|
BookService service;
|
||||||
|
|
||||||
@RequestMapping("/book/{bid}")
|
@RequestMapping("/book/{bid}")
|
||||||
Book findBookById(@PathVariable("bid") int bid){
|
Book findBookById(@PathVariable("bid") int bid,
|
||||||
|
HttpServletRequest request){
|
||||||
|
System.out.println(request.getHeader("Test"));
|
||||||
return service.getBookById(bid);
|
return service.getBookById(bid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package com.test;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
|
||||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
|
||||||
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
|
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
|
||||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
package com.test.controller;
|
package com.test.controller;
|
||||||
|
|
||||||
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
|
|
||||||
import com.test.entity.UserBorrowDetail;
|
import com.test.entity.UserBorrowDetail;
|
||||||
import com.test.service.BorrowService;
|
import com.test.service.BorrowService;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,7 +6,7 @@ import lombok.Data;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClassName: com.test.entity.UserBorrowDetail
|
* ClassName: entity.com.test.filter.UserBorrowDetail
|
||||||
* Package: PACKAGE_NAME
|
* Package: PACKAGE_NAME
|
||||||
*
|
*
|
||||||
* @author yovinchen
|
* @author yovinchen
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.test.service;
|
package com.test.service;
|
||||||
|
|
||||||
import com.test.entity.User;
|
|
||||||
import com.test.entity.UserBorrowDetail;
|
import com.test.entity.UserBorrowDetail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,7 +10,6 @@ import com.test.service.BorrowService;
|
|||||||
import com.test.service.client.BookClient;
|
import com.test.service.client.BookClient;
|
||||||
import com.test.service.client.UserClient;
|
import com.test.service.client.UserClient;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
30
gateway-service/pom.xml
Normal file
30
gateway-service/pom.xml
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.example</groupId>
|
||||||
|
<artifactId>SpringCloudStudy</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>gateway-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.test;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: GatewayApplication
|
||||||
|
* Package: com.test
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/16 08:52
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GatewayApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GatewayApplication.class,args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.test.filter;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: filterTestFilter
|
||||||
|
* Package: com.test
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/16 08:51
|
||||||
|
*/
|
||||||
|
@Component //需要注册为Bean
|
||||||
|
public class TestFilter implements GlobalFilter , Ordered {
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
//先获取ServerHttpRequest对象,注意不是HttpServletRequest
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
//打印一下所有的请求参数
|
||||||
|
System.out.println(request.getQueryParams());
|
||||||
|
//判断是否包含test参数,且参数值为1
|
||||||
|
List<String> value = request.getQueryParams().get("test");
|
||||||
|
if(value != null && value.contains("1")) {
|
||||||
|
//将ServerWebExchange向过滤链的下一级传递(跟JavaWeb中介绍的过滤器其实是差不多的)
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}else {
|
||||||
|
//直接在这里不再向下传递,然后返回响应
|
||||||
|
return exchange.getResponse().setComplete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//设置过滤等级(越小优先级越高)
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
28
gateway-service/src/main/resources/application.yml
Normal file
28
gateway-service/src/main/resources/application.yml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
server:
|
||||||
|
port: 8500
|
||||||
|
eureka:
|
||||||
|
instance:
|
||||||
|
prefer-ip-address: true
|
||||||
|
instance-id: ${spring.cloud.client.ip-address}:${server.port}
|
||||||
|
client:
|
||||||
|
service-url:
|
||||||
|
defaultZone: http://localhost:8801/eureka, http://localhost:8802/eureka
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: gateway
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
routes:
|
||||||
|
- id: borrow-service
|
||||||
|
uri: lb://borrowservice
|
||||||
|
predicates:
|
||||||
|
- Path=/borrow/**
|
||||||
|
# 继续添加新的路由配置,这里就以书籍管理服务为例
|
||||||
|
# 注意-要对齐routes:
|
||||||
|
- id: book-service
|
||||||
|
uri: lb://bookservice
|
||||||
|
predicates:
|
||||||
|
- Path=/book/**
|
||||||
|
filters: # 添加过滤器
|
||||||
|
- AddRequestHeader=Test, HelloWorld!
|
||||||
|
# AddRequestHeader 就是添加请求头信息,其他工厂请查阅官网
|
1
pom.xml
1
pom.xml
@ -21,6 +21,7 @@
|
|||||||
<module>commons</module>
|
<module>commons</module>
|
||||||
<module>eureka-service</module>
|
<module>eureka-service</module>
|
||||||
<module>hystrix-dashboard</module>
|
<module>hystrix-dashboard</module>
|
||||||
|
<module>gateway-service</module>
|
||||||
</modules>
|
</modules>
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
|
@ -2,7 +2,6 @@ package com.test;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
|
||||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,7 +4,6 @@ import com.test.entity.User;
|
|||||||
import com.test.service.UserService;
|
import com.test.service.UserService;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
Loading…
Reference in New Issue
Block a user