学习使用Netflix Zuul及其与Spring Cloud 的牢固结合来创建负载均衡器。这里我们将主要关注API 网关模式及其用法。我们将构建一个netflix zuul 示例,我们将在其中创建一个微服务生态系统并测试其在整个生态系统中Zuul API 网关的有效性和适用性。项目源码下载:(访问密码:9987)
spring-cloud-apigateway_zuul.zip
Web
和Rest Repositories
。给出其他maven GAV坐标并下载项目。existing maven project
. 在此步骤中,使用命令执行全新的 Maven 构建,mvn clean install
以便正确下载所有 Maven 依赖项。
@RestController
。为简单起见,我们将添加一个模型类Student
。import java.util.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringBootStudentServiceApplication
{
@RequestMapping(value = "/echoStudentName/{name}")
public String echoStudentName(@PathVariable(name = "name") String name)
{
return "hello <strong style=\"color: red;\">" + name + " </strong> Responsed on : " + new Date();
}
@RequestMapping(value = "/getStudentDetails/{name}")
public Student getStudentDetails(@PathVariable(name = "name") String name)
{
return new Student(name, "Pune", "MCA");
}
public static void main(String[] args)
{
SpringApplication.run(SpringBootStudentServiceApplication.class, args);
}
}
class Student
{
String name;
String address;
String cls;
public Student(String name, String address, String cls) {
super();
this.name = name;
this.address = address;
this.cls = cls;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getCls() {
return cls;
}
}
application.properties
文件并添加这些条目。
spring.application.name=student
server.port=8090
在这里,我们通过属性为该服务命名,spring.application.name=student
并且我们通过server.port=8090
. 我们需要覆盖默认端口,因为我们将有多个不同微服务的实例将在 localhost 中运行。
mvn clean install
并通过运行 command 将该项目作为 spring boot 应用程序启动java -jar target\spring-boot-zuulgatwayproxy-student-service-0.0.1-SNAPSHOT.jar
。现在一旦服务器启动,转到浏览器并测试端点是否正常工作。Zuul
。给出其他maven GAV坐标并下载项目。mvn clean install
以便正确下载所有 Maven 依赖项。
@EnableZuulProxy
在src
文件夹中存在的 Spring boot 应用程序类上添加注释。有了这个注解,这个工件将像一个 Zuul 服务代理,并将启用 API 网关层的所有功能,如前所述。然后我们将添加一些过滤器和路由配置。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import com.example.springbootzuulgatwayproxy.filters.ErrorFilter;
import com.example.springbootzuulgatwayproxy.filters.PostFilter;
import com.example.springbootzuulgatwayproxy.filters.PreFilter;
import com.example.springbootzuulgatwayproxy.filters.RouteFilter;
@SpringBootApplication
@EnableZuulProxy
public class SpringBootZuulgatwayproxyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootZuulgatwayproxyApplication.class, args);
}
@Bean
public PreFilter preFilter() {
return new PreFilter();
}
@Bean
public PostFilter postFilter() {
return new PostFilter();
}
@Bean
public ErrorFilter errorFilter() {
return new ErrorFilter();
}
@Bean
public RouteFilter routeFilter() {
return new RouteFilter();
}
}
#Zuul routes. Here for /student path, we are routing to localhost:8090 with extra path after that.
zuul.routes.student.url=http://localhost:8090
#Ribbon is auto integrated with Zuul and for this exercise we are not using that.
ribbon.eureka.enabled=false
#Will start the gateway server @8080
server.port=8080
zuul.routes.student.url
会将所有流量路由/student
到实际的学生服务服务器。这样我们也可以将路由添加到其他服务。ribbon.eureka.enabled
与 Zuul 自动集成。server.port
– 需要覆盖默认端口,因为我们将在本地主机中运行不同微服务的多个实例。
pre
,post
,route
和error
。在这里,我们将创建每种类型的过滤器。com.netflix.zuul.ZuulFilter
filterType
,filterOrder
,shouldFilter
和run
方法。这里的filterType
方法只能返回四个 String – 中的任何一个pre/post/route/error
。根据这个值,过滤器将像一个特定的过滤器一样工作。run
方法是我们的过滤器逻辑应该根据我们的要求放置的地方。filterOrder
将用于确定在执行该类型过滤器的阶段该过滤器的顺序。println
用于测试目的之外什么都不做。但实际上,这些功能足够强大,可以完成前面提到的许多重要方面。
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
public class PreFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
System.out.println("Request Method : " + request.getMethod() + " Request URL : " + request.getRequestURL().toString());
return null;
}
}
后过滤器
import com.netflix.zuul.ZuulFilter;
public class PostFilter extends ZuulFilter {
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
System.out.println("Inside Response Filter");
return null;
}
}
路由过滤器
import com.netflix.zuul.ZuulFilter;
public class RouteFilter extends ZuulFilter {
@Override
public String filterType() {
return "route";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
System.out.println("Inside Route Filter");
return null;
}
}
import com.netflix.zuul.ZuulFilter;
public class ErrorFilter extends ZuulFilter {
@Override
public String filterType() {
return "error";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
System.out.println("Inside Route Filter");
return null;
}
}
@Bean
public PreFilter preFilter() {
return new PreFilter();
}
@Bean
public PostFilter postFilter() {
return new PostFilter();
}
@Bean
public ErrorFilter errorFilter() {
return new ErrorFilter();
}
@Bean
public RouteFilter routeFilter() {
return new RouteFilter();
}
mvn clean install
并通过运行命令将此项目作为 Spring Boot 应用程序启动java -jar target\spring-boot-zuulgatwayproxy-0.0.1-SNAPSHOT.jar
。现在一旦服务器启动,转到浏览器并通过访问学生服务来测试端点是否正常工作,但它的名称是/student
。项目源码下载:(访问密码:9987)
spring-cloud-apigateway_zuul.zip
https://www.leftso.com/article/857.html