Implementing dynamic routing with spring Cloud Zuul

Source: Internet
Author: User
Tags bind
Zuul is an edge service that provides dynamic routing, monitoring, resiliency, security, and more. Zuul corresponds to the front door of all requests from the Web site backend of the device and Netflix streaming app.

Zuul can appropriately route requests to multiple Amazon Auto Scaling Groups.

First create a new MAVEN project and add the following dependencies

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId> org.springframework.cloud</groupid>
			<artifactId>spring-cloud-netflix</artifactId>
			<version>1.1.3.RELEASE</version>
			<type>pom</type>
			<scope>import</ scope>
		</dependency>
	</dependencies>
</dependencyManagement>

< dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-hystrix</artifactId>
	</dependency>
	<dependency >
		<groupId>org.springframework.cloud</groupId>
		<artifactId> spring-cloud-starter-zuul</artifactid>
	</dependency>
</dependencies>

Package Com.pp.zuul;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@SpringBootApplication Public
class App  {public
    static void Main (string[] args) { C8/>springapplication.run (App.class, args);
    }
}
Package Com.pp.zuul;

Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

@RestController public
class HomeController {
	
	@RequestMapping ("/index") public
	Object Index () {
		Return "index";
	}
	
	The @RequestMapping ("Home") public
	Object Home () {
		return "to";
	}
}
Configuration file: Application.properties

server.port=8181

#这里的配置表示, Access/baidu/** Direct redirect to http://www.baidu.com
zuul.routes.baidu.path=/baidu/**
zuul.routes.baidu.url=http://www.baidu.com

#反响代理配置
#这里的配置类似nginx的反响代理
#当请求/api/** will be delivered directly to the Listofservers configured server
when processing #当stripPrefix =true (http://127.0.0.1:8181/api/user/list/HTTP// 192.168.1.100:8080/user/list)
when #当stripPrefix =false (http://127.0.0.1:8181/api/user/list/HTTP// 192.168.1.100:8080/api/user/list)
zuul.routes.api.path=/api/**
zuul.routes.api.stripprefix=false
api.ribbon.listofservers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080

#url重写配置
#这里的配置, The equivalent of accessing/index/** directly renders the request content of/home (as with the direct request to/home effect), and the URL address is the same
zuul.routes.index.path=/index/**
Zuul.routes.index.url=forward:/home

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.