Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/toquery/example-spring-cloud-gateway
https://github.com/toquery/example-spring-cloud-gateway
demo example gateway simple spring spring-boot spring-cloud spring-cloud-gateway
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/toquery/example-spring-cloud-gateway
- Owner: ToQuery
- Created: 2019-12-09T01:06:28.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-12-24T05:35:34.000Z (about 1 month ago)
- Last Synced: 2024-12-24T06:26:50.650Z (about 1 month ago)
- Topics: demo, example, gateway, simple, spring, spring-boot, spring-cloud, spring-cloud-gateway
- Language: Java
- Size: 246 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# example-spring-cloud-gateway
## 基础网关服务
### 服务应用信息
| 服务名 | 类型 | 端口 | 说明 |
| -------- | ---------------------- | ---- | -------- |
| gateway | oauth2-client | 8080 | |
| resource | oauth2-resource-server | 9000 | 实际接口 |### 配置
- 去除路径前缀
```java
class RouteLocatorConfig {
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
// 路径路由,去除前缀
// http://localhost/get/users/toquery -> https://api.github.com/users/toquery
// http://localhost/rewrite/get -> http://httpbin.org:80/get
builder.routes().route("path_rewrite_route", r -> r.path("/rewrite/**")
.filters(f -> f.rewritePath("/rewrite/(?.*)", "/${path}"))
.uri(uri)
).build();
}
}
```- 添加接口的权重和负载均衡
```yaml
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: service1_prod
uri: http://localhost:8081
predicates:
- Path=/test
- Weight=service1, 50
- id: service1_canary
uri: http://localhost:8082
predicates:
- Path=/test
- Weight=service1, 50
```## 基于 Oauth2 的 SSO 认证授权网关
Spring 官方原文:[https://spring.io/blog/2019/08/16/securing-services-with-spring-cloud-gateway](https://spring.io/blog/2019/08/16/securing-services-with-spring-cloud-gateway)
![项目架构](docs/images/sso-gateway.png)
### Oauth2 角色说明
- server
- client### 服务与端口
| 服务名 | 类型 | 端口 | 说明 |
| ------------ | ---------------------- | ---- | -------- |
| uaa | oauth2-server | 8090 | |
| sso-gateway | oauth2-client | 8080 | |
| sso-resource | oauth2-resource-server | 9000 | 资源服务 |### 快速使用
```bash
./build-images.sh
docker-compose up
```In an INCOGNITO or PRIVATE browser window...
[http://localhost:8080/resource](http://localhost:8080/resource)
You'll be asked to login, use Username: `user1` and Password: `password`.
> These credentials are configured in the `[sso/uaa/uaa.yml](sso/uaa/uaa.yml)` file.
You'll be asked to Authorise that the resource-server is allowed to read your profile and check your scopes.
Click "Authorise` to allow this. You'll then be redirected to your original destination and the resource server
will print out your Subject's identity GUID.> Resource was accessed by a584b8b8-eaf2-4280-b9c3-12f65cf8524b (sub)
### Cleaning Up
When you're done use `Ctrl-C` to stop the containers, `docker-compose down` to remove leftovers. `docker-compose rm -f`
### Want to go again?
Probably have to clear your cookies and restart the Docker bits...