package com.wl.springcloud.zookeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2019/3/29.
*/
@RequestMapping("/zookeeper")
@RestController
public class ZookeeperController {
@Autowired
private Environment environment;
@RequestMapping("/zookeeper")
public String zookeeper() throws InterruptedException {
Thread.sleep(1000L);
return "zookeeper port:" + environment.getProperty("server.port");
}
}
浏览器输入http://localhost:8080/zookeeper/zookeeper
BasicErrorController断点如下
自定义异常捕获
方式一自定义ErrorController覆盖BasicErrorController
package com.wl.springcloud.zuul.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by Administrator on 2019/4/2.
*/
@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
@Value(value = "${server.error.path:${error.path:/error}}")
private String errorPath;
@Override
public String getErrorPath() {
return errorPath;
}
private final ErrorAttributes errorAttributes;
public ErrorController(ErrorAttributes errorAttributes){
this.errorAttributes = errorAttributes;
}
@RequestMapping("${server.error.path:${error.path:/error}}")
@ResponseBody
public ResponseEntity error(HttpServletRequest request) {
Map body = getErrorAttributes(request);
body.put("code",1);
body.remove("trace");
return new ResponseEntity<>(body, HttpStatus.OK);
}
private Map getErrorAttributes(HttpServletRequest request) {
WebRequest webRequest = new ServletWebRequest(request);
return this.errorAttributes.getErrorAttributes(webRequest, true);
}
}
方式二 自定义SendErrorFilter
配置
zuul.SendErrorFilter.error.disable=true
filter
package com.wl.springcloud.zuul.filter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.client.ClientException;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.http.HttpStatus;
import org.springframework.util.ReflectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ERROR_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_ERROR_FILTER_ORDER;
/**
* Created by Administrator on 2019/4/2.
*/
public class CusSendErrorFilter extends ZuulFilter {
protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";
@Value("${error.path:/error}")
private String errorPath;
@Override
public String filterType() {
return ERROR_TYPE;
}
@Override
public int filterOrder() {
return SEND_ERROR_FILTER_ORDER;
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
// only forward to errorPath if it hasn't been forwarded to already
return ctx.getThrowable() != null
&& !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false);
}
@Override
public Object run() {
try {
RequestContext ctx = RequestContext.getCurrentContext();
ExceptionHolder exception = findZuulException(ctx.getThrowable());
HttpServletResponse response = ctx.getResponse();
Map body = new HashMap<>();
body.put("code",1);
body.put("status",exception.getStatusCode());
body.put("msg",exception.getErrorCause());
response.setContentType("application/json; charset=utf8");
response.setStatus(HttpStatus.OK.value());
response.getWriter().write(new ObjectMapper().writeValueAsString(body));
}
catch (Exception ex) {
ReflectionUtils.rethrowRuntimeException(ex);
}
return null;
}
protected ExceptionHolder findZuulException(Throwable throwable) {
if (throwable.getCause() instanceof ZuulRuntimeException) {
Throwable cause = null;
if (throwable.getCause().getCause() != null) {
cause = throwable.getCause().getCause().getCause();
}
if (cause instanceof ClientException && cause.getCause() != null
&& cause.getCause().getCause() instanceof SocketTimeoutException) {
ZuulException zuulException = new ZuulException("", 504,
ZuulException.class.getName() + ": Hystrix Readed time out");
return new ZuulExceptionHolder(zuulException);
}
// this was a failure initiated by one of the local filters
if(throwable.getCause().getCause() instanceof ZuulException) {
return new ZuulExceptionHolder((ZuulException) throwable.getCause().getCause());
}
}
if (throwable.getCause() instanceof ZuulException) {
// wrapped zuul exception
return new ZuulExceptionHolder((ZuulException) throwable.getCause());
}
if (throwable instanceof ZuulException) {
// exception thrown by zuul lifecycle
return new ZuulExceptionHolder((ZuulException) throwable);
}
// fallback
return new DefaultExceptionHolder(throwable);
}
protected interface ExceptionHolder {
Throwable getThrowable();
default int getStatusCode() {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
default String getErrorCause() {
return null;
}
}
protected static class DefaultExceptionHolder implements ExceptionHolder {
private final Throwable throwable;
public DefaultExceptionHolder(Throwable throwable) {
this.throwable = throwable;
}
@Override
public Throwable getThrowable() {
return this.throwable;
}
}
protected static class ZuulExceptionHolder implements ExceptionHolder {
private final ZuulException exception;
public ZuulExceptionHolder(ZuulException exception) {
this.exception = exception;
}
@Override
public Throwable getThrowable() {
return this.exception;
}
@Override
public int getStatusCode() {
return this.exception.nStatusCode;
}
@Override
public String getErrorCause() {
return this.exception.errorCause;
}
}
}
config
package com.wl.springcloud.zuul.config;
import com.wl.springcloud.zuul.filter.CusSendErrorFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2019/4/2.
*/
@Configuration
public class FilterConfig {
@Bean
public CusSendErrorFilter cusSendErrorFilter(){
return new CusSendErrorFilter();
}
}
4.2 自定义过滤器
package com.wl.springcloud.zuul.filter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2019/4/2.
*/
public class CusZuulFilter extends ZuulFilter {
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
/**
* a "true" return from this method means that the run() method should be invoked
*/
@Override
public boolean shouldFilter() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
String uri = request.getRequestURI();
return uri.contains("/service/"); //请求路径包含/service/的会被拦截
}
@Override
public Object run() throws ZuulException {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
Object object = request.getSession().getAttribute("USER");
if(object != null){
requestContext.setSendZuulResponse(true);//会进行路由,也就是会调用api服务提供者
requestContext.setResponseStatusCode(HttpStatus.OK.value());
requestContext.set("isOK",true);// 相当于设置上下文的key-value键值对 requestContext(请求上下文)可以通过requestContext.get获取
boolean b = (boolean) requestContext.get("isOK");
System.out.println(b);
}else{
requestContext.setSendZuulResponse(false); //不会调用下级服务 直接在网关返回
requestContext.setResponseStatusCode(HttpStatus.OK.value());
Map body = new HashMap<>();
body.put("code",1);
body.put("msg","session is closed please login again");
//设置响应头信息 Content-Type
requestContext.addZuulResponseHeader("Content-Type","application/json");
try {
requestContext.setResponseBody(new ObjectMapper().writeValueAsString(body));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return null;
}
}
package com.wl.springcloud.zuul.config;
import com.wl.springcloud.zuul.filter.CusSendErrorFilter;
import com.wl.springcloud.zuul.filter.CusZuulFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2019/4/2.
*/
@Configuration
public class FilterConfig {
@Bean
public CusSendErrorFilter cusSendErrorFilter(){
return new CusSendErrorFilter();
}
@Bean
public CusZuulFilter cusZuulFilter(){
return new CusZuulFilter();
}
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
// only forward to errorPath if it hasn't been forwarded to already
return ctx.getThrowable() != null
&& !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false)
&& !ctx.getBoolean("fallback",false);
}
参考:http://blog.csdn.net/qingfeilee/article/details/7052736
org.hibernate.NonUniqueResultException: query did not return a unique result: 2
在项目中出现了org.hiber
由Oracle通信技术部门主导的演示项目并没有在本月较早前法国南斯举行的行业集团TM论坛大会中获得嘉奖。但是,Oracle通信官员解雇致力于打造一个支持零干预分配和编制功能的网络即服务(NaaS)平台,帮助企业以更灵活和更适合云的方式实现通信服务提供商(CSP)的连接产品。这个Oracle主导的项目属于TM Forum Live!活动上展示的Catalyst计划的19个项目之一。Catalyst计
Spring MVC提供了非常方便的文件上传功能。
1,配置Spring支持文件上传:
DispatcherServlet本身并不知道如何处理multipart的表单数据,需要一个multipart解析器把POST请求的multipart数据中抽取出来,这样DispatcherServlet就能将其传递给我们的控制器了。为了在Spring中注册multipart解析器,需要声明一个实现了Mul
the CollabNet user information center http://help.collab.net/
How do I create a new Wiki page?
A CollabNet TeamForge project can have any number of Wiki pages. All Wiki pages are linked, and
package beautyOfCoding;
import java.util.Arrays;
import java.util.Random;
public class MaxSubArraySum2 {
/**
* 编程之美 子数组之和的最大值(二维)
*/
private static final int ROW = 5;
private stat
示例程序,swap_1和swap_2都是错误的,推理从1开始推到2,2没完成,推到3就完成了
# include <stdio.h>
void swap_1(int, int);
void swap_2(int *, int *);
void swap_3(int *, int *);
int main(void)
{
int a = 3;
int b =
同步工具类包括信号量(Semaphore)、栅栏(barrier)、闭锁(CountDownLatch)
闭锁(CountDownLatch)
public class RunMain {
public long timeTasks(int nThreads, final Runnable task) throws InterruptedException {
fin
不止一次,看到很多讲技术的文章里面出现过这个词语。今天终于弄懂了——通过朋友给的浏览软件,上了wiki。
我再一次感到,没有辞典能像WiKi一样,给出这样体贴人心、一清二楚的解释了。为了表达我对WiKi的喜爱,只好在此一一中英对照,给大家上次课。
In computer science, bleeding edge is a term that