前置条件
介绍
本小节介绍如何在本地通过 Choerodon
来进行微服务开发。
启动todo
服务
进入choerodon-todo-service
目录下,运行以下命令启动本地项目
mvn clean spring-boot:run
- 本地开发服务,如果不调用其他服务,则可以不需要启动注册中心,在
TodoServiceApplication
中不需要添加@EnableEurekaClient
注解。
服务注册
在TodoServiceApplication
中添加@EnableEurekaClient
注解。
在application.yml
中添加关于Eureka的配置
eureka:
instance:
preferIpAddress: true
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 3
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
在pom.xml
中添加相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
接口权限
Choerodon 的接口权限遵循RBAC
。需要在接口上添加@Permission
注解。
包含如下参数。
参数名 | 说明 |
---|---|
type | 接口层级,ResourceType.SITE,ResourceType.ORGANIZATION,ResourceType.PROJECT三种 |
permissionLogin | 登录允许访问,默认为false |
permissionPublic | 公开接口,默认为false |
permissionWithin | 是否为内部接口 |
其中如果层级为组织层或项目层,则接口的mapping
中必须包含organization_id
或 project_id
作为变量。否则gateway-helper
校验时不会识别该权限。
本地测试
由于Choerodon 包含的服务比较多和依赖的组件较多。一般不会在本地将所有的服务都启动,只会根据自己的需要启动对应的服务。建议在服务器运行一整套环境,本地做服务的单体测试,服务器上进行集成测试。
1. 如果不需要获取当前登录的用户信息,在TodoServiceApplication
中不需要添加@EnableChoerodonResourceServer
注解。然后直接通过postman
或其他接口测试工具对服务提供的接口进行测试。
2. 如果需要获取当前登录的用户信息,则需要在本地启动如下服务,进行登录。
- eureka-server
- api-gateway
- oauth-server
3. 然后通过api-gateway
的输出日志,获取登录用户的 jwt_token
。然后添加请求头。
{
"Authorization": jwt_token
}
启动相关服务
如果需要启动其他模块,可以在github上获取到对应服务的最新代码,克隆到本地,将./src/main/resources/application.yml
复制一份出来,修改里面的默认值。根据本地环境信息,修改数据库和redis连接。
$ cp ./src/main/resources/application.yml ./src/main/resources/application-default.yml
$ mvn clean spring-boot:run
这里提供一份docker-compose.yaml
仅供参考,具体根据配置修改本地程序的配置。服务启动之前,请确保base-service
和 manager-service
的数据库已初始化完成。
version: "3"
services:
mysql:
container_name: mysql
hostname: mysql
image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/mysql:5.7.17
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./mysql/mysql_data:/var/lib/mysql
- ./mysql/mysql_db.cnf:/etc/mysql/conf.d/mysql_db.cnf
- ./mysql/init_user.sql:/docker-entrypoint-initdb.d/init_user.sql
expose:
- "3306"
networks:
- "c7nNetwork"
redis:
container_name: redis
hostname: redis
image: redis:4.0.11
ports:
- "6379:6379"
expose:
- "6379"
networks:
- "c7nNetwork"
eureka-server:
container_name: eureka-server
hostname: eureka-server
image: registry.choerodon.com.cn/choerodon-framework/eureka-server:0.9.0
ports:
- "8000:8000"
environment:
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
- EUREKA_CLIENT_REGISTER_WITH_EUREKA=false
- EUREKA_CLIENT_FETCH_REGISTRY=false
- LOGGING_LEVEL=WARN
- JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms256M -Xmx512M
expose:
- "8000"
networks:
- "c7nNetwork"
api-gateway:
container_name: api-gateway
hostname: api-gateway
image: registry.cn-shanghai.aliyuncs.com/choerodon/api-gateway:0.19.0
links:
- eureka-server
depends_on:
- eureka-server
ports:
- "8080:8080"
environment:
- SPRING_CLOUD_CONFIG_ENABLED=false
- HYSTRIX_STREAM_QUEUE_ENABLED=false
- SPRING_CLOUD_BUS_ENABLED=false
- SPRING_SLEUTH_STREAM_ENABLED=false
- LOGGING_LEVEL=WARN
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
- ZUUL_ADDHOSTHEADER=true
- SPRING_APPLICATION_JSON={"zuul":{"routes":{"dev":{"path":"/todo/**", "serviceId":"choerodon-todo-service"}}}}
- JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
expose:
- "8080"
networks:
- "c7nNetwork"
base-service:
container_name: base-service
image: registry.cn-shanghai.aliyuncs.com/choerodon/base-service:0.19.0
depends_on:
- eureka-server
- mysql
links:
- eureka-server
- mysql
ports:
- "8030:8030"
environment:
- SPRING_CLOUD_CONFIG_ENABLED=false
- HYSTRIX_STREAM_QUEUE_ENABLED=false
- SPRING_CLOUD_BUS_ENABLED=false
- SPRING_SLEUTH_STREAM_ENABLED=false
- LOGGING_LEVEL=WARN
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
- SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/base_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
- SPRING_DATASOURCE_USERNAME=choerodon
- SPRING_DATASOURCE_PASSWORD=123456
- CHOERODON_SAGA_CONSUMER_ENABLED=false
- CHOERODON_SCHEDULE_CONSUMER_ENABLED=false
- JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
networks:
- "c7nNetwork"
manager-service:
container_name: manager-service
image: registry.cn-shanghai.aliyuncs.com/choerodon/manager-service:0.19.0
depends_on:
- eureka-server
- mysql
links:
- eureka-server
- mysql
ports:
- "8963:8963"
environment:
- SPRING_CLOUD_CONFIG_ENABLED=false
- HYSTRIX_STREAM_QUEUE_ENABLED=false
- SPRING_CLOUD_BUS_ENABLED=false
- SPRING_SLEUTH_STREAM_ENABLED=false
- LOGGING_LEVEL=WARN
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
- SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
- SPRING_DATASOURCE_USERNAME=choerodon
- SPRING_DATASOURCE_PASSWORD=123456
- CHOERODON_SWAGGER_CLIENT=client
- CHOERODON_SWAGGER_OAUTH_URL=http://api-gateway:8080/oauth/oauth/authorize
- CHOERODON_GATEWAY_DOMAIN=api-gateway:8080
- JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
networks:
- "c7nNetwork"
oauth-server:
container_name: oauth-server
image: registry.cn-shanghai.aliyuncs.com/choerodon/oauth-server:0.19.0
depends_on:
- eureka-server
- redis
- mysql
links:
- eureka-server
- redis
- mysql
ports:
- "8020:8020"
environment:
- SPRING_CLOUD_CONFIG_ENABLED=false
- HYSTRIX_STREAM_QUEUE_ENABLED=false
- SPRING_CLOUD_BUS_ENABLED=false
- SPRING_SLEUTH_STREAM_ENABLED=false
- LOGGING_LEVEL=WARN
- EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
- SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/base_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
- SPRING_DATASOURCE_USERNAME=choerodon
- SPRING_DATASOURCE_PASSWORD=123456
- SPRINT_REDIS_HOST=redis
- SPRING_REDIS_PORT=6379
- SPRING_REDIS_DATABASE=4
- CHOERODON_OAUTH_LOGIN_SSL=false
- CHOERODON_OAUTH_LOGIN_PATH=/login
- CHOERODON_DEFAULT_REDIRECT_URL=http://localhost:9000
- JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
networks:
- "c7nNetwork"
networks:
c7nNetwork:
driver: bridge
有关Docker的更多信息请见此处
有关Docker-Compose的更多信息请见此处