Spring Cloud Zuul 统一访问代理

在微服务框中(Spring Cloud)存在很多服务,客户端逐个调用这些服务非常繁琐,Zuul提供了统一访问接口访问这些微服务。zuul的核心是一系列的filters, 其作用可以类比Servlet框架的Filter,或者AOP。
zuul的过滤器之间不能直接通信,它们之间通过一个RequestContext的静态类来进行数据传递的。RequestContext类中有ThreadLocal变量来记录每个Request所需要传递的数据。

Zuul有几种标准的过滤器类型

Zuul大部分功能都是通过过滤器来实现的。Zuul中定义了四种标准过滤器类型,这些过滤器类型对应于请求的典型生命周期。
(1) PRE:这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。
(2) ROUTING:这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并使用Apache HttpClient或Netfilx Ribbon请求微服务。
(3) POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。
(4) ERROR:在其他阶段发生错误时执行该过滤器。
Zuul的生命周期也就是对应的几种过滤器的类型。
Spring Cloud Zuul 统一访问代理_第1张图片

Zuul提供的功能

Spring Cloud Zuul 统一访问代理_第2张图片

设计注册中心

POM.xml


  4.0.0
  com.test
  eureka-server
  war
  0.0.1-SNAPSHOT
  eureka-server Maven Webapp
  http://maven.apache.org
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	

	
	  UTF-8
	

	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR1
				pom
				import
			
		
	

	
	  
		org.springframework.boot
		spring-boot-starter-test
		test
	  
	  
	  
		org.springframework.cloud
		spring-cloud-starter-eureka-server
	  
	  
	  
		 
			org.springframework.boot 
			spring-boot-starter-security 	
		 
	

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



application.properties

server.port=8110
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8110/eureka/

security.user.name=chenqx
security.user.password=123

启动类

package com.test.eureka_server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

设计订单微服务

POM.xml


  4.0.0
  com.test
  eureka-service-order
  war
  0.0.1-SNAPSHOT
  eureka-service-order Maven Webapp
  http://maven.apache.org
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	
	
		UTF-8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka-server
		
		
		
			org.springframework.cloud
			spring-cloud-starter-hystrix
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
	
  
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR1
				pom
				import
			
		
	
	
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



application.properties

server.port=9100
spring.application.name=service-order
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true

OrderInfo实体类

package com.test.eureka_service_order;

import java.io.Serializable;
import java.sql.Date;

public class OrderInfo implements Serializable {
    private static final long serialVersionUID = -7233238826463139634L;
    private Long id;
    private String name;
    private String prdtName;
    private Integer count;
    private Integer amount;
    private Date createDt = new Date(System.currentTimeMillis());
    private String userId = null;
    
 
    public OrderInfo() {
    }
 
    public OrderInfo(String name,String prdtName,Integer count,Integer amount,Date createDt,String userId) {
        this.name = name;
        this.prdtName = prdtName;
        this.count = count;
        this.amount = amount;
        this.createDt = createDt;
        this.userId = userId;
    }
 
 
    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrdtName() {
		return prdtName;
	}

	public void setPrdtName(String prdtName) {
		this.prdtName = prdtName;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public Date getCreateDt() {
		return createDt;
	}

	public void setCreateDt(Date createDt) {
		this.createDt = createDt;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ",prdtName=" + prdtName +
                ",count=" + count +
                ",amount=" + amount +
                '}';
    }
}

服务实现类

package com.test.eureka_service_order;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderServiceApiImpl{

	
	@RequestMapping(value = "/hello1", method = RequestMethod.GET)
	public String hello(@RequestParam("name") String name) {
		System.out.println("name="+name);
		return "hello " + name;
	}

	@RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public OrderInfo hello(@RequestParam("name") String name,
    		@RequestParam("prdtName") String prdtName) {
		System.out.println("hello2 name="+name+",prdtName="+prdtName);
        try {
            name= URLDecoder.decode(name,"UTF-8");
            prdtName= URLDecoder.decode(prdtName,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new OrderInfo(name, prdtName,1,1,null,null);
    }
 
	@RequestMapping(value = "/hello3", method = RequestMethod.GET)
    public String hello(@RequestBody OrderInfo order) {
        if (order == null) {
            return "未知";
        }
        return order.toString();
    }

}

启动类

package com.test.eureka_service_order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

编写库存微服务

POM.xml


  4.0.0
  com.test
  eureka-service-order
  war
  0.0.1-SNAPSHOT
  eureka-service-order Maven Webapp
  http://maven.apache.org
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	
	
		UTF-8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka-server
		
		
		
			org.springframework.cloud
			spring-cloud-starter-hystrix
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
	
  
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR1
				pom
				import
			
		
	
	
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



application.properties

server.port=9101
spring.application.name=service-store
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true

服务实现类

package com.test.eureka_service_store;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StoreServiceApiImpl{

	
	@RequestMapping(value = "/store", method = RequestMethod.GET)
	public String hello(@RequestParam("name") String name) {
		System.out.println("name="+name);
		return "仓库名称= " + name;
	}

}

启动类

package com.test.eureka_service_store;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

编写Zuul代理

POM.xml


  4.0.0
  com.test
  eureka-service-order
  war
  0.0.1-SNAPSHOT
  eureka-service-order Maven Webapp
  http://maven.apache.org
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	
	
		UTF-8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
		
			org.springframework.cloud
			spring-cloud-starter-zuul
		
	
  
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Dalston.SR1
				pom
				import
			
		
	
	
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



application.properties

server.port=9300
spring.application.name=zuul-proxy
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true

启动类

package com.test.eureka_service_order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

测试效果:
订单服务和库存服务都被Zuul统一代理访问
Spring Cloud Zuul 统一访问代理_第3张图片

在浏览器中输入
http://localhost:9300/service-store/store?name=Java
访问库存服务
Spring Cloud Zuul 统一访问代理_第4张图片

在浏览器中输入
http://localhost:9300/service-order/hello1?name=Java
访问订单服务
Spring Cloud Zuul 统一访问代理_第5张图片

你可能感兴趣的:(SpringCloud)