1.路由网关?
a.网关是系统的唯一对外的入口,介于客户端和服务端之间的中间层,
处理非业务功能;
2.为什么使用微服务网关?
1.不同的微服务有不同的的网络地址,
而客户端可能需要调用多个服务接口才能完成一个业务需求,
客户端和各个客户端通信可能会存在:
a.客户端多次请求不同微服务,增加了客户端的复杂性;
3.网关的优点
1.所有的外部请求先经过微服务网关进行交互,
无需调用特定微服务接口,简化了开发;
2.服务网关=路由转发+过滤器
a.路由转发:接收请求,转发到后端的微服务上;
b.过滤器:可在服务网关中可以完成一系列的横切功能(权限校验)
4.网关缺点:
1.增加了网关,多了一层转发,性能会下降一些;
5.搭建zuul项目
1.创建父项目(tour-parent)
a.添加依赖
2.创建子模块eureka注册中心(tour-eureka-server)
a.配置eureka server
b.启动eureka server
3.创建服务提供者模块(tour-account-service)
a.提供服务将服务注册到eureka注册中心
b.启动eureka服务
4.创建服务消费者模块(tour-account-web)
a.消费服务
b.启动消费者
5.创建zuul子模块
a.添加依赖
b.配置zuul
c.启动
1-4:https://blog.csdn.net/qq_29393273/article/details/99967213
创建zuul子项目
1.在父项目中添加依赖 添加zuul 依赖
<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.tour</groupId>
<artifactId>tour-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 将当前项目作为一个springboot-starter的子模块 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<modules>
<module>tour-online-web</module>
<module>tour-eureka-server</module>
<module>tour-account-service</module>
<module>tour-common</module>
<module>tour-presist</module>
<module>tour-model</module>
<module>tour-zuul-server</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.M3</spring-cloud.version>
<fastjson.version>1.2.62</fastjson.version>
<mybatis-plus.version>3.2.0</mybatis-plus.version>
<oracle.version>8.0.0</oracle.version>
</properties>
<dependencies>
<!-- oracle 驱动包 -->
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>${oracle.version}</version>
<scope>compile</scope>
</dependency>
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- springboot 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<!-- springCloud -->
<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>
<!--配置信息
id: 不同环境Profile的唯一id;
properties:properties下可以自定义多个字段(名字随便取,可以有多个);
activation:用来指定激活方式,可以根据jdk环境,环境变量,文件的存在或缺失;
-->
<profiles>
<!--开发环境-->
<profile>
<!--环境标识-->
<id>dev</id>
<activation>
<!--默认激活:如果在不指定profile时默认是开发环境-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profiles.active>dev</profiles.active>
<server.account.port>9092</server.account.port>
<server.online.port>9093</server.online.port>
<server.eureka.port>9090</server.eureka.port>
<server.zuul.port>9091</server.zuul.port>
<eureka.host>127.0.0.1</eureka.host>
<!-- 数据库相关 -->
<spring.datasource.url>jdbc:oracle:thin:@10.103.32.24:1521:onlinetest</spring.datasource.url>
<spring.datasource.driverClassName>oracle.jdbc.driver.OracleDriver</spring.datasource.driverClassName>
<spring.datasource.username>onlinetest</spring.datasource.username>
<spring.datasource.password>onlinetest438</spring.datasource.password>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<spring.profiles.active>test</spring.profiles.active>
<server.account.port>9092</server.account.port>
<server.online.port>9093</server.online.port>
<server.eureka.port>9090</server.eureka.port>
<server.zuul.port>9091</server.zuul.port>
<eureka.host>127.0.0.1</eureka.host>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>pro</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<spring.profiles.active>pro</spring.profiles.active>
<server.account.port>9092</server.account.port>
<server.online.port>9093</server.online.port>
<server.eureka.port>9090</server.eureka.port>
<server.zuul.port>9091</server.zuul.port>
<eureka.host>127.0.0.1</eureka.host>
</properties>
</profile>
</profiles>
</project>
2.配置
#application.properties
#端口号
server.port=@server.zuul.port@
#配置应用名称
spring.application.name=tour-zuul-server
#eureka
eureka.client.service-url.defaultZone=http://@eureka.host@:@server.eureka.port@/eureka
#zuul
#前缀
#zuul.prefix=/tour
zuul.routes.account.path=/account/**
zuul.routes.account.service-id=tour-account-service
zuul.routes.online.path=/online/**
zuul.routes.online.service-id=tour-online-web
application.yml 版
#端口号
server:
port: "@server.zuul.port@"
#应用名称
spring:
application:
name: tour-online-web
#eureka
eureka:
client:
service-url:
defaultZone: http://@eureka.host@:@server.zuul.port@/eureka
#zuul
zuul:
#prefix: tour
routes:
account:
path: /account/**
service-id: tour-account-service
online:
path: /online/**
service-id: tour-online-web
3.启动
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class,args);
}
}
访问:http://localhost:zuul模块端口/online/hello