一:新建一个IpRequestOriginParser类,实现RequestOriginParser接口,配置如下
public class IpRequestOriginParser implements RequestOriginParser { /** * Parse the origin from given HTTP request. * * @param request HTTP request * @return parsed origin */ @Override public String parseOrigin(HttpServletRequest request) { return request.getRemoteAddr(); } }
二:新建一个SentinelConfig类,配置如下
@Configuration public class SentinelConfig { @PostConstruct public void init() { WebCallbackManager.setUrlBlockHandler(new DemoUrlBlockHandler()); WebCallbackManager.setRequestOriginParser(new IpRequestOriginParser()); } }
三:新建一个DemoUrlBlockHandler类,实现UrlBlockHandler接口,配置如下
public class DemoUrlBlockHandler implements UrlBlockHandler { @Override public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException { httpServletResponse.getWriter().println(“error url”); } }
四:访问一次/white接口,sentinel控制台就会出现地址了,是懒加载的
六:在浏览器访问/white接口,可以看到有返回值