基于Finchley.SR2的springcloud系列代码(zuul 网关服务)(十)

备注:源代码已经全部上传到github,有错误地方请指正,谢谢!地址:https://github.com/hcmony/springcloud 

1,pom.xml



	4.0.0

	
		com.hcmony
		springcloud
		0.0.1-SNAPSHOT
	

	zuul
	jar

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-starter-zuul
			RELEASE
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



2,配置文件

server.port=8094
spring.application.name=zuul
eureka.instance.ip-address=true
eureka.client.service-url.defaultZone= http://localhsot:8888/eureka/

spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.percentage =1.0

zuul.routes.provider.path=/provider/**
zuul.routes.provider.serviceId=provider

zuul.routes.consumer.path=/consumer/**
zuul.routes.consumer.serviceId=consumer

3,java代码

3.1异常信息控制类

package com.hcmony.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 

异常信息控制类

*

* * @author hcmony * @since V1.0.0, 2019/03/05 10:52 */ @RestController public class MyErrorController implements ErrorController{ /** * Returns the path of the error page. * * @return the error path */ @Override public String getErrorPath() { return "/error"; } @RequestMapping(value = "/error") public String error(HttpServletResponse resp, HttpServletRequest req) { return "参数异常或者是其它未知异常"; } @RequestMapping(value = "/notoken") public String notoken(HttpServletResponse resp, HttpServletRequest req) { return "notoken"; } }
3.2网关拦截实现类
package com.hcmony;


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 

网关拦截实现类/h3> *

* * @author hcmony * @since V1.0.0, 2019/03/05 10:04 */ @Component public class MyZuulFilter extends ZuulFilter { private static final Logger LOGGER = LoggerFactory.getLogger(MyZuulFilter.class); /** * pre:路由之前 routing:路由之时 post: 路由之后 error:发送错误调用 * @return */ @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest(); String token = request.getParameter("token"); LOGGER.info("token is {}",token); if (StringUtils.isNotBlank(token)&&"token".equals(token)){ return null; } try { currentContext.getResponse().sendRedirect("/notoken"); }catch (Exception e){ } return null; } }

3.3启动类
package com.hcmony;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;


@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {

	public static void main(String[] args) {
		SpringApplication.run(ZuulApplication.class, args);
	}
}

 

 

你可能感兴趣的:(springcloud,zuul,网关,java基础,SpringCloud)