网关服务很多,比如:Zuul、Kong、spring cloud gateway ……, 这里不纠结哪种性能好,本次体验是用的 spring cloud gateway

更多网关比较可以了解这篇文章: 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异

配置网关服务步骤:

1、初始化一个 SpringBoot 应用

2、引入相关依赖

<?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> <groupId>com.zjj7</groupId>
<artifactId>gateway_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>gateway_server</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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> <!--配置仓库-->
<repositories>
<repository>
<id>aliRepository</id>
<name>aliRepository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <!--cloud 引来-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!--<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> <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-gateway</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

pom.xml 的配置代码

3、修改配置文件

spring:
application:
name: gateway_server
cloud:
gateway:
default-filters:
routes:
- id: user-server
# lb代表从注册中心获取服务
uri: lb://user
predicates:
# 转发该路径
- Path=/user/**
# 带前缀
filters:
- StripPrefix=1
- id: publish-server
uri: lb://publish
predicates:
- Path=/publish/**
filters:
- StripPrefix=1 #服务注册中心端口号
server:
port: 6400 #服务注册中心实例的主机名、端口
#是否向服务注册中心注册自己
#是否检索服务
#服务注册中心的配置内容,指定服务注册中心的位置
eureka:
port:
6110
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${eureka.port}/eureka/

application.yml 的 相关配置

这里和其它服务不同的是: 启动类上面除了默认注解,无需添加更多

这时,一个简单的网关服务就搭建完了, 启动后所有的 API 就能以网关服务的地址为入口开始访问了

最新文章

  1. Android中Activity的生命周期
  2. openURL的使用方法:
  3. 其他常用HTML 片段
  4. mysql ERROR 1044 (42000): Access denied for user &#39;&#39;@&#39;localhost&#39; to database
  5. datagridview 中添加了一个button类型的列,怎么写button的事件
  6. SVN并行开发管理策略
  7. iOS sqlite3数据库解析
  8. FTP操作类(支持异步)
  9. [改善Java代码]不推荐使用binarySearch对列表进行检索
  10. Log4net创建日志及简单扩展
  11. Oracle 基本命令
  12. HashMap的存储结构及原理
  13. Linux经常使用命令(十一) - more
  14. OpenStack命令 创建网络和路由管理
  15. this.$router.push、replace、go的区别
  16. Failed to set MokListRT: Invalid Parameter Something as gone seriously wrong: import_mok_state() failed: Invalid Parameter
  17. C# 异步编程4 async与await 异步程序开发
  18. Jquery 组 表单验证
  19. Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)
  20. 基于jQuery仿迅雷影音官网幻灯片特效

热门文章

  1. loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)
  2. 20155201 第十一周Java课堂实践
  3. 20145311王亦徐《JAVA程序设计》课程总结
  4. 20162314 Experiment 3 - Sorting and Searching
  5. leetcode刷题吧
  6. Redis之字符串
  7. SDN前瞻 该来的来了!SDN 软件定义网络
  8. perl模块终极解决方案--转载
  9. 两个cookie的合并
  10. Rails 5 Test Prescriptions 第10章 Testing for Security