Skip to content

Commit

Permalink
Fix issue of grpc inbound request get header
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhiguo committed Jan 6, 2025
1 parent cb1ee3f commit d5208d0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,45 @@
*/
package com.jd.live.agent.demo.grpc.consumer.controller;

import com.google.common.collect.Sets;
import com.jd.live.agent.demo.grpc.service.api.*;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Set;

@RestController
public class UserServiceController {

@GrpcClient("grpc-provider")
private UserServiceGrpc.UserServiceBlockingStub userServiceGrpc;

private final Set<String> excludeHeaders = Sets.newHashSet(
"host",
"content-length",
"connection",
"accept-encoding",
"user-agent",
"content-type",
"accept"
);

@GetMapping("/get")
public String get(@RequestParam("id") Integer id) {
public String get(@RequestParam("id") Integer id, HttpServletRequest servletRequest) {
UserGetRequest request = UserGetRequest.newBuilder().setId(id).build();
UserGetResponse response = userServiceGrpc.get(request);

Metadata metadata = getMetadata(servletRequest);
// 使用 stub.withInterceptors() 方法
UserGetResponse response = userServiceGrpc.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata))
.get(request);

return response.toString();
}

Expand All @@ -45,4 +68,39 @@ public String create(@RequestParam("name") String name,
return response.toString();
}

private Metadata getMetadata(HttpServletRequest servletRequest) {
// 创建 Metadata 对象来存储 header 和 cookie
Metadata metadata = new Metadata();

// 获取并转移所有 header
Enumeration<String> headerNames = servletRequest.getHeaderNames();
// 在添加 header 时进行过滤
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
if (!excludeHeaders.contains(headerName.toLowerCase())) {
String headerValue = servletRequest.getHeader(headerName);
metadata.put(
Metadata.Key.of(headerName.toLowerCase(), Metadata.ASCII_STRING_MARSHALLER),
headerValue
);
}
}

// 获取并转移所有 cookie
Cookie[] cookies = servletRequest.getCookies();
if (cookies != null) {
StringBuilder cookieString = new StringBuilder();
for (Cookie cookie : cookies) {
if (cookieString.length() > 0) {
cookieString.append("; ");
}
cookieString.append(cookie.getName()).append("=").append(cookie.getValue());
}
metadata.put(
Metadata.Key.of("cookie", Metadata.ASCII_STRING_MARSHALLER),
cookieString.toString()
);
}
return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public String getClientIp() {
public boolean isNativeGroup() {
return false;
}

@Override
public String getHeader(String key) {
return headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
}

@Override
public List<String> getHeaders(String key) {
Iterable<String> iterable = headers.getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
if (iterable == null) {
return null;
}
return StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
}

}

/**
Expand Down

0 comments on commit d5208d0

Please sign in to comment.