Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyZeng committed Nov 23, 2022
1 parent a86b08d commit 0be7dc9
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
8 changes: 8 additions & 0 deletions skeleton-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.projectreactor</groupId>-->
<!-- <artifactId>reactor-core</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package git.snippets.skeleton.common.interceptor;

import org.springframework.http.server.reactive.ServerHttpRequest;
import reactor.core.publisher.Mono;

public class ReactiveRequestContextHolder {
public static final Class<ServerHttpRequest> CONTEXT_KEY = ServerHttpRequest.class;

public static Mono<ServerHttpRequest> getRequest() {
return Mono.subscriberContext()
.map(ctx -> ctx.get(CONTEXT_KEY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@Component
public class DataFilter implements OrderedWebFilter {

Expand All @@ -21,15 +18,15 @@ public int getOrder() {

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
System.out.println("data service filter");
// System.out.println("data service filter");
HttpHeaders headers = exchange.getRequest().getHeaders();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
System.out.println("request key is " + entry.getKey() + "and values are" + entry.getValue());
}
headers = exchange.getResponse().getHeaders();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
System.out.println("response key is " + entry.getKey() + "and values are" + entry.getValue());
}
// for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
// System.out.println("request key is " + entry.getKey() + "and values are" + entry.getValue());
// }
// headers = exchange.getResponse().getHeaders();
// for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
// System.out.println("response key is " + entry.getKey() + "and values are" + entry.getValue());
// }
// Map<String, String> header = httpRequestToMap(request);
exchange.getResponse().getHeaders().addAll(exchange.getRequest().getHeaders());
return chain.filter(exchange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import git.snippets.skeleton.common.exception.BaseException;
import git.snippets.skeleton.common.exception.BaseExceptionBody;
import git.snippets.skeleton.common.interceptor.ReactiveRequestContextHolder;
import git.snippets.skeleton.common.vo.User;
import git.snippets.skeleton.gateway.config.UaaProperties;
import org.apache.commons.lang.StringUtils;
Expand All @@ -10,6 +11,7 @@
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpHeaders;
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;
Expand Down Expand Up @@ -116,7 +118,8 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// for (Map.Entry<String, String> entry : header.entrySet()) {
// ctx.addZuulRequestHeader(entry.getKey(), entry.getValue());
// }
return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().header(User.CONTEXT_KEY_USERID, userId).build()).build()).contextWrite(ctx -> ctx.put(User.CONTEXT_KEY_USERID, userId));
//ServerHttpRequest request = exchange.getRequest();
return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().header(User.CONTEXT_KEY_USERID, userId).build()).build());
}
//如果在忽略的url里,则跳过
// String path = replacePrefix(exchange.getRequest().getURI().getPath());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package git.snippets.skeleton.user.controller;


import git.snippets.skeleton.common.interceptor.ReactiveRequestContextHolder;
import git.snippets.skeleton.common.vo.User;
import git.snippets.skeleton.user.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -36,9 +38,11 @@ public String getDefaultUser() {
*/
@GetMapping("/getContextUserId")
public Mono<String> getContextUserId() {
return userService.getContextUserId();
//return userService.getContextUserId();
// return Mono.deferContextual(ctx -> ctx.get(User.CONTEXT_KEY_USERID));
//return Mono.subscriberContext().map(x -> x.get(User.CONTEXT_KEY_USERID));
return ReactiveRequestContextHolder.getRequest()
.map(request -> request.getHeaders().getFirst(User.CONTEXT_KEY_USERID));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package git.snippets.skeleton.user.filter;

import git.snippets.skeleton.common.interceptor.ReactiveRequestContextHolder;
import git.snippets.skeleton.common.vo.User;
import org.springframework.boot.web.reactive.filter.OrderedWebFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
Expand Down Expand Up @@ -31,7 +33,8 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
}
// Map<String, String> header = httpRequestToMap(request);
String userId = headers.getFirst(User.CONTEXT_KEY_USERID);
return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().header(User.CONTEXT_KEY_USERID, userId).build()).build()).contextWrite(ctx -> ctx.put(User.CONTEXT_KEY_USERID, userId));
ServerHttpRequest request = exchange.getRequest();
return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().header(User.CONTEXT_KEY_USERID, userId).build()).build()).contextWrite(ctx -> ctx.put(ReactiveRequestContextHolder.CONTEXT_KEY, request));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
package git.snippets.skeleton.user.service.dataservice;


import feign.Headers;
import feign.Param;
import git.snippets.skeleton.common.vo.User;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import reactivefeign.spring.config.ReactiveFeignClient;
import reactor.core.publisher.Mono;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Headers(User.CONTEXT_KEY_USERID + ": {requester}")
@ReactiveFeignClient(value = "sc-data-service", url = "sc-data-service")
public interface DataService {
// @RequestMapping(value = "/user/add", method = GET)
Expand All @@ -37,7 +35,7 @@ public interface DataService {
// @RequestMapping(value = "/user/update", method = POST)
// String updateUser(@RequestBody User user);
@RequestMapping(value = "/getContextUserId", method = GET)
Mono<String> getContextUserId(@Param("requester") String requester);
Mono<String> getContextUserId();

// @RequestMapping(value = "/getDefaultUser", method = GET)
// String getDefaultUser();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git.snippets.skeleton.user.service.impl;


import git.snippets.skeleton.common.interceptor.ReactiveRequestContextHolder;
import git.snippets.skeleton.common.vo.User;
import git.snippets.skeleton.user.service.IUserService;
import git.snippets.skeleton.user.service.dataservice.DataService;
Expand Down Expand Up @@ -37,14 +38,14 @@ public String getDefaultUser() {

@Override
public Mono<String> getContextUserId() {
return dataService.getContextUserId("dddd");
// return ReactiveRequestContextHolder.getRequest().map(request -> request.getHeaders().getFirst(User.CONTEXT_KEY_USERID));
return dataService.getContextUserId();
}

@Override
public Flux<String> getProviderData() {
return webClient.get().uri("http://sc-data-service/getProviderData").header(User.CONTEXT_KEY_USERID, "abc").retrieve().bodyToFlux(String.class);
}
// @Override
// public List<String> getProviderData() {
// return Arrays.asList("a", "b");
// }
Expand Down

0 comments on commit 0be7dc9

Please sign in to comment.