今天接着介绍springcloud,今天介绍下springcloud的路由网关-Zuul,外围系统或者用户经过网关访问服务,网关经过注册中心找到对应提供服务的客户端,网关也须要到注册中心进行注册。java
先把官网的图在贴一下,便于理解。web
说明: gateway负责与外部进行交互,是sprincloud微服务对外的窗口。面试
(1)注册客户端spring
(2)路由Zuulapache
next->next ->finish完成建立springboot
说明:Zuul其实也是注册中心的客户端,主要也是3个类或文件。微信
(1)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>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spc</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-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> </project>
说明:app
springboot两个starter:client与Zuul。maven
(2)application启动类
package com.spc.gateway; 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 GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
说明:
启动类主要三个标签:@SpringBootApplication、@EnableEurekaClient、@EnableZuulProxy。
(3)application.yml
spring: application: name: gateway eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ server: port: 9000
说明:
主要就一个地址:http://localhost:8761/eureka/,这个是注册中心地址。
若是你以为文章对你有些帮助,欢迎微信搜索「软件老王」第一时间阅读或交流!
(1)启动注册中心、客户端、路由Zuul
(2)经过路由访问client客户端服务
路由地址:http://localhost:9000/client
访问格式:路由地址+客户端服务注册名称,对应上面的application。
更多知识请关注公众号:「软件老王」,IT技术与相关干货分享,回复关键字获取对应干货,java,送必看的10本“武功秘籍”;图片,送100多万张可商用高清图片;面试,送刚毕业就能月薪“20k”的java面试题,软考,送官方pdf书籍与通关论文,后续会不断更新,好比“工具”,“视频“等,已经在整理中。