Spring Cloud Netflix入门(Zuul)

Zuul就是一个API网关。

1、ZuulGatewayApp.java

package com.neohope.springcloud.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableZuulProxy
//@EnableZuulServer
public class ZuulGatewayApp 
{
	@Value("${spring.application.name}")
	private String appName;
	
	@RequestMapping("/appName")
	public String appName() {
		return appName;
	}
	
	public static void main(String[] args)
	{
		new SpringApplicationBuilder(ZuulGatewayApp.class).web(true).run(args);
	}
	
	@RequestMapping("/")
	public String hello() {
		return "It works";
	}
}

2、pom.xml

<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.cloud.stream.app</groupId>
		<artifactId>app-starters-build</artifactId>
		<version>1.1.3.M1</version>
	</parent>

	<groupId>com.neohope.springcloud.test</groupId>
	<artifactId>netflix-zuul-gateway</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>netflix-zuul-gateway</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.netflix.hystrix</groupId>
			<artifactId>hystrix-javanica</artifactId>
		</dependency>
		<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>com.netflix.feign</groupId>
			<artifactId>feign-okhttp</artifactId>
			<version>8.18.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot</artifactId>
			<version>1.5.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
			<version>1.5.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

3、application.properties

spring.application.name=zuul-gateway-app
server.port=7001
security.user.name=user
security.user.password=hystrix

eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/
eureka.client.preferSameZoneEureka=true
eureka.client.healthcheck.enabled=true
eureka.instance.metadataMap.zone = zone1
eureka.instance.statusPageUrlPath=${management.context-path}/info
eureka.instance.healthCheckUrlPath=${management.context-path}/health
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10

zuul.host.maxTotalConnections=200
zuul.host.maxPerRouteConnections=20
#zuul.ignoredServices='*'
zuul.routes.netflix-feign-server.sensitiveHeaders
zuul.routes.netflix-feign-server.path=/invokeA/**
zuul.routes.netflix-feign-server.serviceId=netflix-feign-server
#zuul.routes.netflix-feign-server.url=
#zuul.ribbonIsolationStrategy=THREAD
#zuul.prefix=api
zuul.stripPrefix=false
#zuul.addProxyHeaders=false
#zuul.ignoredHeaders=
#zuul.forceOriginalQueryStringEncoding=true
#zuul.SendResponseFilter.post.disable=true.

#ribbon.eureka.enabled=false
#ribbon.restclient.enabled=true
#ribbon.okhttp.enabled=true
#ribbon.ConnectTimeout=3000
#ribbon.ReadTimeout=60000
#users.ribbon.listOfServers=172.16.172.63

#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000

Comments are closed.