手把手搭建微服务框架(五):搭建网关Zuul,并注册到注册中心(Eureka),解决前后端分离跨域问题

1.新建maven工程,在pom.xml导入相关依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

2.在resources目录下新建application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:5555/eureka/
server:
  port: 9999
spring:
  application:
    name: server-zuul
zuul:
  routes:
    api-a:
      path: /api-fegin/**
      service-id: server-fegin
    api-b:
      path: /api-provision/**
      service-id: server-provision 

3.新建MyFiler继承ZuulFilter

package com.zuul.filter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
@Component
public class MyFilter extends ZuulFilter{

	@Override
	public boolean shouldFilter() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public Object run() {
		//用于过滤是否携带token
		/*
		 * RequestContext currentContext = RequestContext.getCurrentContext();
		 * HttpServletRequest request = currentContext.getRequest(); String parameter =
		 * request.getParameter("token"); if(parameter!=null) { return null; }
		 * currentContext.setSendZuulResponse(false);
		 * currentContext.setResponseStatusCode(401); try {
		 * currentContext.getResponse().getWriter().write("error"); } catch (Exception
		 * e) { // TODO: handle exception }
		 */
		return null;
	}

	@Override
	public String filterType() {
		// TODO Auto-generated method stub
		return "pre";
	}

	@Override
	public int filterOrder() {
		// TODO Auto-generated method stub
		return 0;
	}

}

4.新建启动类

package com.zuul;

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

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApp {
	public static void main(String[] args) {
		SpringApplication.run(ZuulApp.class, args);
	}
}

5.注意:启动服务顺序

1.启动eureka
2.启动服务server-provision
3.启动服务server-fegin
4.查询eureka,服务是否注册成功
5.查看上面服务启动成功后,启动zuul服务
6.访问网关,携带server-fegin服务请求路径,会自动跳转到server-fegin服务的username方法
http://localhost:9999/api-fegin/username
7.访问网关,携带server-provision服务请求路径,会自动跳转到server-provision服务的username方法
http://localhost:9999/api-provision/username
8.有效解决前后端分离跨域问题

你可能感兴趣的:(微服务)