3 changed files with 106 additions and 29 deletions
@ -0,0 +1,35 @@ |
|||||
|
package com.epmet.commons.tools.feign; |
||||
|
|
||||
|
import feign.RequestInterceptor; |
||||
|
import feign.RequestTemplate; |
||||
|
import org.springframework.web.context.request.RequestAttributes; |
||||
|
import org.springframework.web.context.request.RequestContextHolder; |
||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import java.util.Enumeration; |
||||
|
|
||||
|
|
||||
|
public class EpmetBaseRequestInterceptor implements RequestInterceptor { |
||||
|
|
||||
|
@Override |
||||
|
public void apply(RequestTemplate template) { |
||||
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); |
||||
|
if (requestAttributes == null) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); |
||||
|
Enumeration<String> headerNames = request.getHeaderNames(); |
||||
|
if (headerNames != null) { |
||||
|
while (headerNames.hasMoreElements()) { |
||||
|
String name = headerNames.nextElement(); |
||||
|
Enumeration<String> values = request.getHeaders(name); |
||||
|
while (values.hasMoreElements()) { |
||||
|
String value = values.nextElement(); |
||||
|
template.header(name, value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,59 @@ |
|||||
|
/** |
||||
|
* Copyright (c) 2018 人人开源 All rights reserved. |
||||
|
* |
||||
|
* https://www.renren.io
|
||||
|
* |
||||
|
* 版权所有,侵权必究! |
||||
|
*/ |
||||
|
|
||||
|
package com.epmet.config; |
||||
|
|
||||
|
import com.epmet.commons.tools.constant.AppClientConstant; |
||||
|
import com.epmet.commons.tools.feign.EpmetBaseRequestInterceptor; |
||||
|
import feign.RequestInterceptor; |
||||
|
import feign.RequestTemplate; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
|
/** |
||||
|
* Feign调用,携带header |
||||
|
* |
||||
|
* @author Mark sunlightcs@gmail.com |
||||
|
* @since 1.0.0 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class JobFeignConfig { |
||||
|
|
||||
|
@Bean |
||||
|
RequestInterceptor requestInterceptor() { |
||||
|
return new JobRequestInterceptor(); |
||||
|
} |
||||
|
|
||||
|
class JobRequestInterceptor extends EpmetBaseRequestInterceptor { |
||||
|
@Override |
||||
|
public void apply(RequestTemplate template) { |
||||
|
super.apply(template); |
||||
|
|
||||
|
// job服务自己生成流水号
|
||||
|
template.header(AppClientConstant.TRANSACTION_SERIAL_KEY, generateTransactionSerial()); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取事务流水号 |
||||
|
* |
||||
|
* @return |
||||
|
*/ |
||||
|
public String generateTransactionSerial() { |
||||
|
String[] letterPool = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n" |
||||
|
, "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; |
||||
|
|
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
for (int i = 0; i < 2; i++) { |
||||
|
sb.append(letterPool[(int) (Math.random() * 25)]); |
||||
|
} |
||||
|
|
||||
|
sb.append(System.currentTimeMillis()); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue