14 changed files with 355 additions and 31 deletions
@ -1,6 +1,6 @@ |
|||||||
server.port=8762 |
server.port=8762 |
||||||
|
|
||||||
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ |
eureka.client.serviceUrl.defaultZone=http://eureka.xumy.vip/eureka/ |
||||||
|
|
||||||
spring.application.name=eureka-hi |
spring.application.name=eureka-hi |
||||||
|
|
||||||
|
@ -1,4 +1,4 @@ |
|||||||
server.port=8761 |
server.port=8080 |
||||||
|
|
||||||
eureka.instance.hostname=localhost |
eureka.instance.hostname=localhost |
||||||
eureka.client.registerWithEureka=false |
eureka.client.registerWithEureka=false |
@ -0,0 +1,8 @@ |
|||||||
|
/.classpath |
||||||
|
/.project |
||||||
|
/.settings |
||||||
|
/bin/ |
||||||
|
/logs |
||||||
|
/target |
||||||
|
*.log |
||||||
|
/src/main/resources/static |
@ -0,0 +1,72 @@ |
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
<parent> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||||
|
<version>2.0.3.RELEASE</version> |
||||||
|
</parent> |
||||||
|
<artifactId>eureka-client-zuul</artifactId> |
||||||
|
<version>1.0.0</version> |
||||||
|
<name>eureka-client-zuul</name> |
||||||
|
<packaging>jar</packaging> |
||||||
|
|
||||||
|
<properties> |
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
||||||
|
<java.version>1.8</java.version> |
||||||
|
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version> |
||||||
|
</properties> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.cloud</groupId> |
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.cloud</groupId> |
||||||
|
<artifactId>spring-cloud-starter-netflix-zuul</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
</dependencies> |
||||||
|
|
||||||
|
<dependencyManagement> |
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.cloud</groupId> |
||||||
|
<artifactId>spring-cloud-dependencies</artifactId> |
||||||
|
<version>${spring-cloud.version}</version> |
||||||
|
<type>pom</type> |
||||||
|
<scope>import</scope> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
</dependencyManagement> |
||||||
|
|
||||||
|
<build> |
||||||
|
<finalName>zuul</finalName> |
||||||
|
<plugins> |
||||||
|
<plugin> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||||
|
<configuration> |
||||||
|
<executable>true</executable> |
||||||
|
</configuration> |
||||||
|
</plugin> |
||||||
|
</plugins> |
||||||
|
</build> |
||||||
|
|
||||||
|
</project> |
@ -0,0 +1,65 @@ |
|||||||
|
package vip.xumy.learn.eureka.client; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import com.netflix.zuul.ZuulFilter; |
||||||
|
import com.netflix.zuul.context.RequestContext; |
||||||
|
|
||||||
|
@Component |
||||||
|
public class MyFilter extends ZuulFilter { |
||||||
|
|
||||||
|
private static Logger log = LoggerFactory.getLogger(MyFilter.class); |
||||||
|
|
||||||
|
/** |
||||||
|
* filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: pre:路由之前 |
||||||
|
* routing:路由之时 post: 路由之后 error:发送错误调用 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public String filterType() { |
||||||
|
return "pre"; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* filterOrder:过滤的顺序 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public int filterOrder() { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* shouldFilter:这里可以写逻辑判断,是否要过滤,true,永远过滤。 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public boolean shouldFilter() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public Object run() { |
||||||
|
RequestContext ctx = RequestContext.getCurrentContext(); |
||||||
|
HttpServletRequest request = ctx.getRequest(); |
||||||
|
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString())); |
||||||
|
Object accessToken = request.getParameter("token"); |
||||||
|
if (accessToken == null) { |
||||||
|
log.warn("token is empty"); |
||||||
|
ctx.setSendZuulResponse(false); |
||||||
|
ctx.setResponseStatusCode(401); |
||||||
|
try { |
||||||
|
ctx.getResponse().getWriter().write("token is empty"); |
||||||
|
} catch (Exception e) { |
||||||
|
} |
||||||
|
|
||||||
|
return null; |
||||||
|
} |
||||||
|
log.info("ok"); |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
package vip.xumy.learn.eureka.client; |
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication; |
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
||||||
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; |
||||||
|
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; |
||||||
|
|
||||||
|
@SpringBootApplication |
||||||
|
@EnableEurekaClient |
||||||
|
@EnableDiscoveryClient |
||||||
|
@EnableZuulProxy |
||||||
|
public class ServiceZuulApplication { |
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
SpringApplication.run( ServiceZuulApplication.class, args ); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,10 @@ |
|||||||
|
server.port=8080 |
||||||
|
|
||||||
|
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ |
||||||
|
|
||||||
|
spring.application.name=eureka-zuul |
||||||
|
|
||||||
|
zuul.routes.ribbon.path=/ribbon/** |
||||||
|
zuul.routes.ribbon.serviceId=eureka-ribbon |
||||||
|
zuul.routes.feign.path=/feign/** |
||||||
|
zuul.routes.feign.serviceId=eureka-feign |
Loading…
Reference in new issue