增加 spring cloud 用例
This commit is contained in:
74
spring-cloud/spring-cloud-sleuth-zipkin/zuul/pom.xml
Normal file
74
spring-cloud/spring-cloud-sleuth-zipkin/zuul/pom.xml
Normal file
@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.8.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<artifactId>zuul</artifactId>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!--eureka-client-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
<!--zuul-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
<!--zipkin-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zipkin</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<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>
|
||||
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.zuul;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableZuulProxy
|
||||
@EnableDiscoveryClient
|
||||
public class ZuulApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZuulApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,81 @@
|
||||
package com.heibaiying.zuul.config;
|
||||
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : zuul 的熔断器
|
||||
*/
|
||||
@Component
|
||||
public class CustomZuulFallbackProvider implements FallbackProvider {
|
||||
|
||||
/*
|
||||
* 定义熔断将用于哪些路由的服务
|
||||
*/
|
||||
@Override
|
||||
public String getRoute() {
|
||||
return "consumer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
|
||||
return new ClientHttpResponse() {
|
||||
|
||||
/**
|
||||
* 返回响应的HTTP状态代码
|
||||
*/
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return HttpStatus.SERVICE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回HTTP状态代码
|
||||
*/
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return HttpStatus.SERVICE_UNAVAILABLE.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回响应的HTTP状态文本
|
||||
*/
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
return HttpStatus.SERVICE_UNAVAILABLE.getReasonPhrase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将消息正文作为输入流返回
|
||||
*/
|
||||
@Override
|
||||
public InputStream getBody() throws IOException {
|
||||
return new ByteArrayInputStream("商城崩溃了,请稍后重试!".getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将消息正文作为输入流返回
|
||||
*/
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
return httpHeaders;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.heibaiying.zuul.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 登录
|
||||
*/
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@RequestMapping("index")
|
||||
public String login(){
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping("login")
|
||||
public String login(String username,HttpSession session){
|
||||
session.setAttribute("code",username+String.valueOf(new Random().nextInt(10*1000)));
|
||||
return "redirect:/consumer/sell/products";
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.heibaiying.zuul.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
import org.omg.CORBA.Request;
|
||||
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 自定义filter过滤器
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class CustomZuulFilter extends ZuulFilter {
|
||||
|
||||
/**
|
||||
* 返回过滤器的类型
|
||||
*/
|
||||
@Override
|
||||
public String filterType() {
|
||||
return FilterConstants.PRE_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回过滤器的优先级顺序
|
||||
*/
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从此方法返回“true”意味着应该调用下面的 run()方法
|
||||
*/
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* ZuulFilter的核心校验方法
|
||||
*/
|
||||
@Override
|
||||
public Object run() throws ZuulException {
|
||||
RequestContext currentContext = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = currentContext.getRequest();
|
||||
String code = (String)request.getSession().getAttribute("code");
|
||||
if (StringUtils.isEmpty(code)){
|
||||
// 设置值为false 不将请求转发到对应的服务上
|
||||
currentContext.setSendZuulResponse(false);
|
||||
// 设置返回的状态码
|
||||
currentContext.setResponseStatusCode(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value());
|
||||
HttpServletResponse response = currentContext.getResponse();
|
||||
try {
|
||||
// 跳转到登录页面
|
||||
response.sendRedirect("/index");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
server:
|
||||
port: 8090
|
||||
# 指定服务命名
|
||||
spring:
|
||||
application:
|
||||
name: zuul
|
||||
zipkin:
|
||||
base-url: http://localhost:9411/
|
||||
# 指定注册中心地址
|
||||
eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://localhost:8010/eureka/
|
||||
# 网关的路由
|
||||
zuul:
|
||||
routes:
|
||||
xxxx: #这个地方的值是可以任意的字符串
|
||||
path: /producer/**
|
||||
serviceId: producer
|
||||
consumer:
|
||||
path: /consumer/**
|
||||
serviceId: consumer
|
||||
# 指定前缀
|
||||
# prefix: /v1
|
||||
|
@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/login" method="post">
|
||||
<input name="username" type="text">
|
||||
<button id="btn">输入临时用户名后登录!</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.zuul;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ZuulApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user