文章目录
Note
不同版本对应不同分支
springcloud-greenwich采用了consul-config,不需要下方的hui-base-springcloud-config-repo
Github
地址:https://github.com/ithuhui/hui-base-springcloud
分支:finchley | greenwich
模块:【hui-base-springcloud-api-gateway】
finchley分支配置中心配置存放在git-> https://github.com/ithuhui/hui-base-springcloud-config-repo
Greenwich分支(springcloud-api-gateway)
maven
<!--SpringBoot Parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<springcloud.version>Greenwich.RELEASE</springcloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!--Spring Cloud dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--网关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--服务发现-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml
这里详细说一下,假设我要访问localhost:8080/config-service-client/testConfig
网关的规则是怎样配置的呢?
${predicates:- Path} 首先匹配这个Path变量,-> ${ filters: - StripPrefix=1} -> 截取第一个不要,要后面的 -> 截取后结果/testConfig -> 前面拼接 ${- PrefixPath=/config-service-client} -> localhost:8080/config-service-client/testConfig -> 找到服务 u r i : l b : / / c o n f i g − s e r v i c e − c l i e n t − > 最 后 − > l o c a l h o s t : 8080 / {uri: lb://config-service-client} -> 最后 -> localhost:8080/ uri:lb://config−service−client−>最后−>localhost:8080/{serviceId}/${- PrefixPath}/testConfig
有人肯定会问,我直接用${ filters: - StripPrefix=0},或者不加不就行了?
解释一下我用PrefixPath的原因,主要是配置了ContextPath而且与serviceID不一致的时候。我这里也是为了写一些常用配置故意加上。
server:
port: 29000
spring:
application:
name: gateway-service
cloud:
## consul 配置
consul:
host: 127.0.0.1
port: 8500
discovery:
service-name: ${spring.application.name}
# 启用服务发现
enabled: true
# 启用服务注册
register: true
# 服务停止时取消注册
deregister: true
# 表示注册时使用IP而不是hostname
prefer-ip-address: true
# 执行监控检查的频率
health-check-interval: 30s
# 设置健康检查失败多长时间后,取消注册
health-check-critical-timeout: 30s
# 健康检查的路径
health-check-path: /actuator/info
# 服务注册标识,格式为:应用名称+服务器IP+端口
instance-id: ${spring.application.name}
## 网关配置
gateway:
routes:
# order服务
- id: order-service
uri: lb://order-service
predicates:
- Path=/order-service/**
- PrefixPath=/order-service
filters:
- StripPrefix=1
- PrefixPath=/order-service
# product服务
- id: product-service
uri: lb://product-service
predicates:
- Path=/product-service/**
filters:
- StripPrefix=1
- PrefixPath=/product-service
# config-service demo测试
- id: config-service-client
uri: lb://config-service-client
predicates:
- Path=/config-service-client/**
filters:
- StripPrefix=1
- PrefixPath=/config-service-client
Code
package com.hui.base.springcloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* <b><code>ZuulApplication</code></b>
* <p/>
* Description: SpringCloud Greenwich + gateway 网关中心
* <p/>
* <b>Creation Time:</b> 2018/11/26 0:06.
*
* @author Hu Weihui
*/
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
Finchley分支(netflix-zuul)
maven
<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.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
bootstrap.yml
spring:
application:
name: api-gateway
#EUREKA 注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:28761/eureka
server:
port: 29000
zuul:
routes:
hui-config:
path: /hui-config/**
serviceId: config-server
# 忽略的路由
ignored-patterns:
- /mapper-server/products
#敏感头 设置为空 则不拦截cookie authorization setcookie
sensitive-headers:
ribbon:
ReadTimeout: 60000
ConnectTimeout: 60000
CoreCode
package com.hui.base.springcloud.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;
/**
* <b><code>ZuulApplication</code></b>
* <p/>
* Description: SpringCloud Finchley 版本 + Zuul 网关中心
* <p/>
* <b>Creation Time:</b> 2018/11/26 0:06.
*
* @author Hu Weihui
*/
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
Author
作者:HuHui
转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢