我目前正在建立一个微服务项目。使用Zuul作为API网关,使用Keycloak作为Auth Server,使用Consul作为Service Discovery和一些PHP服务。我正在将docker与docker-compose一起使用进行开发。我想通过Zuul作为反向代理从服务中获取资源,并通过Keycloak授权每个请求。
当我通过Zuul到Keycloak进行身份验证过程以获取授权令牌时,它没有问题。但是当我通过Zuul使用令牌请求资源时,就会发生错误。
此处提供一些图表,供您进一步了解:
Graph(很抱歉,我没有直接嵌入图片,我的信誉还不够)
我从邮递员得到的回复:
{
"timestamp": "2019-10-01T06:55:07.426+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"path": "/inventory/saras-php-poc/inventory/api/1.0.0/category"
}
错误显示如下:
这是Apache Tomcat的调试日志:
这是我的Zuul应用程序属性:
server:
port: 8080
logging:
level:
org:
apache: DEBUG
spring:
application:
name: saras-gateway
cloud:
consul:
discovery:
instance-id: ${spring.application.name}:${server.port}
serviceName: ${spring.application.name}
host: consul
port: 8500
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 127000
ribbon:
eureka:
enabled: false
ConnectTimeout: 3000
SocketTimeout: 60000
ReadTimeout: 60000
zuul:
ignored-services: '*'
routes:
inventory:
path: /inventory/**
serviceId: inventory
user:
path: /user/**
serviceId: user
order:
path: /order/**
serviceId: order
api-service:
path: /api/**
serviceId: api-service
keycloak:
sensitiveHeaders:
path: /keycloak/**
serviceId: keycloak
ratelimit:
enabled: true
repository: JPA
policy-list:
rest:
- limit: 5
refresh-interval: 60
type:
- origin
keycloak:
auth-server-url: keycloak/auth
realm: develop
resource: gateway
bearer-only: true
securityConstraints:
- authRoles:
- admin
securityCollections:
- name: admin
patterns:
- /user/*
- authRoles:
- user
securityCollections:
- name: user
patterns:
- /inventory/*
- /order/*
public-client: true
这是我的docker-compose.yml :
version: "3.1"
services:
rabbitmq:
image: rabbitmq:management
container_name: rabbitmq
ports:
- "15672:15672"
- "5672:5672"
networks:
- default
www_inventory:
build: .
container_name: www_inventory
ports:
- "8081:80"
volumes:
- ./inventory:/var/www/html/saras-php-poc/inventory
links:
- db_inventory
networks:
- default
working_dir: /var/www/html/saras-php-poc/inventory
expose:
- 80
db_inventory:
image: mysql:8.0
container_name: db_inventory
ports:
- "3307:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump_inventory:/docker-entrypoint-initdb.d
- ./conf_inventory:/etc/mysql/conf.d
- mysql_inventory:/var/lib/mysql
networks:
- default
phpmyadmin_inventory:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin_inventory
links:
- db_inventory:db_inventory
ports:
- 8084:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
PMA_HOST: db_inventory
PMA_PORT: 3306
www_order:
build: .
container_name: www_order
ports:
- "8082:80"
volumes:
- ./order:/var/www/html/saras-php-poc/order
links:
- db_order
networks:
- default
working_dir: /var/www/html/saras-php-poc/order
expose:
- 80
db_order:
image: mysql:8.0
container_name: db_order
ports:
- "3308:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump_order:/docker-entrypoint-initdb.d
- ./conf_order:/etc/mysql/conf.d
- mysql_order:/var/lib/mysql
networks:
- default
phpmyadmin_order:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin_order
links:
- db_order:db_order
ports:
- 8085:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
PMA_HOST: db_order
PMA_PORT: 3306
www_user:
build: .
container_name: www_user
ports:
- "8083:80"
volumes:
- ./user:/var/www/html/saras-php-poc/user
links:
- db_user
networks:
- default
working_dir: /var/www/html/saras-php-poc/user
expose:
- 80
db_user:
image: mysql:5.7
container_name: db_user
ports:
- "3309:3306"
command:
- --default-authentication-plugin=mysql_native_password
- --disable-partition-engine-check
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump_user:/docker-entrypoint-initdb.d
- ./conf_user:/etc/mysql/conf.d
- mysql_user:/var/lib/mysql
networks:
- default
phpmyadmin_user:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin_user
links:
- db_user:db_user
ports:
- 8086:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
PMA_HOST: db_user
PMA_PORT: 3306
consul:
image: bitnami/consul:latest
networks:
- default
ports:
- '8300:8300'
- '8301:8301'
- '8301:8301/udp'
- '8500:8500'
- '8600:8600'
- '8600:8600/udp'
gateway:
build:
context: gateway
container_name: api_gateway
ports:
- 8080:8080
networks:
- default
working_dir: /user/app
volumes:
mysql_inventory:
mysql_order:
mysql_user:
这是我的Zuul(网关)Dockerfile
FROM maven:3.5-jdk-8 AS build
COPY src /user/app/src
COPY pom.xml /user/app
RUN mvn -f /user/app/pom.xml clean package -Dmaven.test.skip=true -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
FROM openjdk:8-jre-slim
COPY --from=build /user/app/target/gateway-0.0.1-SNAPSHOT.jar /user/app/gateway-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/user/app/gateway-0.0.1-SNAPSHOT.jar"]
我为Keycloak制作了一个单独的docker-compose,因为如果我将其打包在一起,它将不会运行。
Keycloak docker-compose.yml :
version: '3'
services:
keycloak:
image: jboss/keycloak
container_name: keycloak
environment:
DB_VENDOR: MYSQL
DB_ADDR: db_user
DB_DATABASE: saras
DB_USER: root
DB_PASSWORD: test
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
# PROXY_ADDRESS_FORWARDING: 'true'
networks:
- default
ports:
- 8087:8080
expose:
- 8080
networks:
default:
external:
name: saras-php-poc_default
这是领事的注册服务:
{
"api-service": {
"ID": "api-service",
"Service": "api-service",
"Tags": [],
"Meta": {},
"Port": 8181,
"Address": "127.0.0.1",
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false
},
"inventory": {
"ID": "inventory",
"Service": "inventory",
"Tags": [],
"Meta": {},
"Port": 80,
"Address": "www_inventory",
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false
},
"keycloak": {
"ID": "keycloak",
"Service": "keycloak",
"Tags": [],
"Meta": {},
"Port": 8080,
"Address": "keycloak",
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false
},
"order": {
"ID": "order",
"Service": "order",
"Tags": [],
"Meta": {},
"Port": 80,
"Address": "www_order",
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false
},
"saras-gateway-8080": {
"ID": "saras-gateway-8080",
"Service": "saras-gateway",
"Tags": [
"secure=false"
],
"Meta": {},
"Port": 8080,
"Address": "e2f5518828f7",
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false
},
"user": {
"ID": "user",
"Service": "user",
"Tags": [],
"Meta": {},
"Port": 80,
"Address": "www_user",
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false
}
}
我错过了什么吗?请让我知道。
谢谢?