|
server:
port: 8200
eureka:
client:
service-url:
defaultZone: http://ejava:[email protected]:8000/eureka/
spring:
application:
name: springCloud-zuul
zuul:
ignored-services:
"*"
routes:
springCloud-UltraService: /ultraService-proxy/**
|
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class,
args);
}
}
|
|
public class AuthRequestFilter extends ZuulFilter {
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();//获取当前请求上下文
String auth ="java:hello";
byte[] encodeAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
String header = new String(encodeAuth);
context.addZuulRequestHeader("Authorization", header);
return null;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public String filterType() {
return "pre";
}
}
|
@Configuration
public class ZuulConfig {
@Bean
public AuthRequestFilter getAuthRequestFilter(){
return new AuthRequestFilter();
}
}
|
security:
basic:
enabled: true
user:
name: zjava
password: zhello
|
@FeignClient(value="
ultraService-proxy
",configuration=FeignClientConfig.class,
fallbackFactory=IDeptClientServiceFallBack.class)
public interface IDeptClientService {
@RequestMapping(method=RequestMethod.GET,value="/dept/sessionId")
public String getSessionId();
@RequestMapping(method=RequestMethod.GET,value="/dept/user")
public UserBean getUser();
}
|
@Configuration
public class FeignClientConfig {
@Bean
public Level getLogger(){
return Logger.Level.FULL;
}
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("
zjava
", "
zhello
");
}
}
|
zuul:
AuthRequestFilter:
pre:
disable: true
|
@Component
public class DeptServiceFallback implements ZuulFallbackProvider {
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse(){
@Override
public InputStream getBody() throws IOException {
//可以设定异常时返回的降级服务信息
return new ByteArrayInputStream("【服务异常的主体内容信息】".getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","text/heml; charset=UTF-8");
return headers;
}
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST;
}
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.BAD_REQUEST.value();
}
@Override
public String getStatusText() throws IOException {
return HttpStatus.BAD_REQUEST.getReasonPhrase();
}
@Override
public void close() {
}
};
}
@Override
public String getRoute() {
//设置处理失败的路由
return "ultraService-proxy";
}
}
|