SpringCloud的网关主要用来代理多个服务访问的IP地址,就像是nginx一样nginx实现反向代理
要怎么玩呢?其实跟之前创建提供者一样的只是yml文件和pom.xml有点改变而已
首先要创建一个新的项目:
pom.xml的文件配置:就多加了一个网关的jar包,跟之前配置的提供者pom.xml没什么区别
<?xml version="1.0" encoding="UTF-8"?>
<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.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.accp</groupId>
<artifactId>qq-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>qq-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--网关jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</repository>
</repositories>
</project>
yml文件的配置: QQ-user-consumer就是消费者的名称,如果有多个消费者可以配置多个 消费者名称: /user/**
server:
port: 8000
spring:
application:
name: QQ-zuul
eureka:
client:
service-url:
defaultZone: http://root:111111@localhost:5000/eureka/
zuul:
routes:
QQ-user-consumer: /user/**
#QQ-user-consumer2: /user2/**
#QQ-user-consumer3: /user3/**
在启动类添加一个 @EnableZuulProxy注解
package com.accp.qqzuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class QqZuulApplication {
public static void main(String[] args) {
SpringApplication.run(QqZuulApplication.class, args);
}
}
然后就可以通过网关的端口号加user前缀访问浏览器了:
控制台打印:
上一篇:SpringCloud(五)负载均衡之随机策略
下一篇:SpringCloud(七)网关zuul配置过滤器