SpringCloud路由(网关)
springcloud网关接口就类似于转发
搭建路由网关项目(ZuulDemo)
1、创建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> <groupId>com.cppdy</groupId> <artifactId>ZuulDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> </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> <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>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> </project>
2、创建application.yml配置文件
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka/
server:
port: 9005
spring:
application:
name: cppdy-zuul
zuul:
routes:
api-a:
path: /api-a/**
service-id: cppdy-ribbon
api-b:
path: /api-b/**
service-id: cppdy-feign
3、创建启动类(ZuulApp)
package com.cppdy; 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); } }
4、先启动EurekaDemo(注册中心项目),再启动ProduceDemo(服务提供者项目)端口号设置为9001,再启动RibbonDemo(消费者项目)端口号设置为9003,再启动FeignDemo(消费者项目)端口号设置为9004,再启动ZuulDemo(路由网关项目)
访问http://localhost:9005/api-a/hello时,会转发调用http://localhost:9003/hello(RibbonDemo项目)
访问http://localhost:9005/api-b/hello时,会转发调用http://localhost:9004/hello(FeignDemo项目)