• 前置条件
  • 介绍
  • 启动todo 服务
  • 服务注册
  • 接口权限
  • 本地测试
  • 启动相关服务

    前置条件

    • 开发环境配置:开发环境安装
    • 数据环境准备:数据初始化
    • Demo程序已经开发完毕,详见 开发Demo程序
    • 启动 mysqlredis等容器

    介绍

    本小节介绍如何在本地通过 Choerodon 来进行微服务开发。

    启动todo 服务

    进入choerodon-todo-service目录下,运行以下命令启动本地项目

    1. mvn clean spring-boot:run
    • 本地开发服务,如果不调用其他服务,则可以不需要启动注册中心,在TodoServiceApplication中不需要添加@EnableEurekaClient 注解。

    服务注册

    TodoServiceApplication中添加@EnableEurekaClient 注解。

    application.yml中添加关于Eureka的配置

    1. eureka:
    2. instance:
    3. preferIpAddress: true
    4. leaseRenewalIntervalInSeconds: 1
    5. leaseExpirationDurationInSeconds: 3
    6. client:
    7. serviceUrl:
    8. defaultZone: http://localhost:8000/eureka/

    pom.xml中添加相关依赖

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    4. </dependency>

    接口权限

    Choerodon 的接口权限遵循RBAC。需要在接口上添加@Permission注解。

    包含如下参数。

    参数名说明
    type接口层级,ResourceType.SITE,ResourceType.ORGANIZATION,ResourceType.PROJECT三种
    permissionLogin登录允许访问,默认为false
    permissionPublic公开接口,默认为false
    permissionWithin是否为内部接口

    其中如果层级为组织层或项目层,则接口的mapping 中必须包含organization_idproject_id 作为变量。否则gateway-helper校验时不会识别该权限。

    本地测试

    由于Choerodon 包含的服务比较多和依赖的组件较多。一般不会在本地将所有的服务都启动,只会根据自己的需要启动对应的服务。建议在服务器运行一整套环境,本地做服务的单体测试,服务器上进行集成测试。

    1. 如果不需要获取当前登录的用户信息,在TodoServiceApplication中不需要添加@EnableChoerodonResourceServer 注解。然后直接通过postman 或其他接口测试工具对服务提供的接口进行测试。

    2. 如果需要获取当前登录的用户信息,则需要在本地启动如下服务,进行登录。

    • eureka-server
    • api-gateway
    • oauth-server3. 然后通过api-gateway 的输出日志,获取登录用户的 jwt_token。然后添加请求头。
    1. {
    2. "Authorization": jwt_token
    3. }

    启动相关服务

    如果需要启动其他模块,可以在github上获取到对应服务的最新代码,克隆到本地,将./src/main/resources/application.yml 复制一份出来,修改里面的默认值。根据本地环境信息,修改数据库和redis连接。

    1. $ cp ./src/main/resources/application.yml ./src/main/resources/application-default.yml
    2. $ mvn clean spring-boot:run

    这里提供一份docker-compose.yaml仅供参考,具体根据配置修改本地程序的配置。服务启动之前,请确保base-servicemanager-service 的数据库已初始化完成。

    1. version: "3"
    2. services:
    3. mysql:
    4. container_name: mysql
    5. hostname: mysql
    6. image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/mysql:5.7.17
    7. ports:
    8. - "3306:3306"
    9. environment:
    10. MYSQL_ROOT_PASSWORD: root
    11. volumes:
    12. - ./mysql/mysql_data:/var/lib/mysql
    13. - ./mysql/mysql_db.cnf:/etc/mysql/conf.d/mysql_db.cnf
    14. - ./mysql/init_user.sql:/docker-entrypoint-initdb.d/init_user.sql
    15. expose:
    16. - "3306"
    17. networks:
    18. - "c7nNetwork"
    19. redis:
    20. container_name: redis
    21. hostname: redis
    22. image: redis:4.0.11
    23. ports:
    24. - "6379:6379"
    25. expose:
    26. - "6379"
    27. networks:
    28. - "c7nNetwork"
    29. eureka-server:
    30. container_name: eureka-server
    31. hostname: eureka-server
    32. image: registry.choerodon.com.cn/choerodon-framework/eureka-server:0.9.0
    33. ports:
    34. - "8000:8000"
    35. environment:
    36. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    37. - EUREKA_CLIENT_REGISTER_WITH_EUREKA=false
    38. - EUREKA_CLIENT_FETCH_REGISTRY=false
    39. - LOGGING_LEVEL=WARN
    40. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms256M -Xmx512M
    41. expose:
    42. - "8000"
    43. networks:
    44. - "c7nNetwork"
    45. api-gateway:
    46. container_name: api-gateway
    47. hostname: api-gateway
    48. image: registry.cn-shanghai.aliyuncs.com/choerodon/api-gateway:0.19.0
    49. links:
    50. - eureka-server
    51. depends_on:
    52. - eureka-server
    53. ports:
    54. - "8080:8080"
    55. environment:
    56. - SPRING_CLOUD_CONFIG_ENABLED=false
    57. - HYSTRIX_STREAM_QUEUE_ENABLED=false
    58. - SPRING_CLOUD_BUS_ENABLED=false
    59. - SPRING_SLEUTH_STREAM_ENABLED=false
    60. - LOGGING_LEVEL=WARN
    61. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    62. - ZUUL_ADDHOSTHEADER=true
    63. - SPRING_APPLICATION_JSON={"zuul":{"routes":{"dev":{"path":"/todo/**", "serviceId":"choerodon-todo-service"}}}}
    64. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    65. expose:
    66. - "8080"
    67. networks:
    68. - "c7nNetwork"
    69. base-service:
    70. container_name: base-service
    71. image: registry.cn-shanghai.aliyuncs.com/choerodon/base-service:0.19.0
    72. depends_on:
    73. - eureka-server
    74. - mysql
    75. links:
    76. - eureka-server
    77. - mysql
    78. ports:
    79. - "8030:8030"
    80. environment:
    81. - SPRING_CLOUD_CONFIG_ENABLED=false
    82. - HYSTRIX_STREAM_QUEUE_ENABLED=false
    83. - SPRING_CLOUD_BUS_ENABLED=false
    84. - SPRING_SLEUTH_STREAM_ENABLED=false
    85. - LOGGING_LEVEL=WARN
    86. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    87. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/base_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    88. - SPRING_DATASOURCE_USERNAME=choerodon
    89. - SPRING_DATASOURCE_PASSWORD=123456
    90. - CHOERODON_SAGA_CONSUMER_ENABLED=false
    91. - CHOERODON_SCHEDULE_CONSUMER_ENABLED=false
    92. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    93. networks:
    94. - "c7nNetwork"
    95. manager-service:
    96. container_name: manager-service
    97. image: registry.cn-shanghai.aliyuncs.com/choerodon/manager-service:0.19.0
    98. depends_on:
    99. - eureka-server
    100. - mysql
    101. links:
    102. - eureka-server
    103. - mysql
    104. ports:
    105. - "8963:8963"
    106. environment:
    107. - SPRING_CLOUD_CONFIG_ENABLED=false
    108. - HYSTRIX_STREAM_QUEUE_ENABLED=false
    109. - SPRING_CLOUD_BUS_ENABLED=false
    110. - SPRING_SLEUTH_STREAM_ENABLED=false
    111. - LOGGING_LEVEL=WARN
    112. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    113. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    114. - SPRING_DATASOURCE_USERNAME=choerodon
    115. - SPRING_DATASOURCE_PASSWORD=123456
    116. - CHOERODON_SWAGGER_CLIENT=client
    117. - CHOERODON_SWAGGER_OAUTH_URL=http://api-gateway:8080/oauth/oauth/authorize
    118. - CHOERODON_GATEWAY_DOMAIN=api-gateway:8080
    119. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    120. networks:
    121. - "c7nNetwork"
    122. oauth-server:
    123. container_name: oauth-server
    124. image: registry.cn-shanghai.aliyuncs.com/choerodon/oauth-server:0.19.0
    125. depends_on:
    126. - eureka-server
    127. - redis
    128. - mysql
    129. links:
    130. - eureka-server
    131. - redis
    132. - mysql
    133. ports:
    134. - "8020:8020"
    135. environment:
    136. - SPRING_CLOUD_CONFIG_ENABLED=false
    137. - HYSTRIX_STREAM_QUEUE_ENABLED=false
    138. - SPRING_CLOUD_BUS_ENABLED=false
    139. - SPRING_SLEUTH_STREAM_ENABLED=false
    140. - LOGGING_LEVEL=WARN
    141. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    142. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/base_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    143. - SPRING_DATASOURCE_USERNAME=choerodon
    144. - SPRING_DATASOURCE_PASSWORD=123456
    145. - SPRINT_REDIS_HOST=redis
    146. - SPRING_REDIS_PORT=6379
    147. - SPRING_REDIS_DATABASE=4
    148. - CHOERODON_OAUTH_LOGIN_SSL=false
    149. - CHOERODON_OAUTH_LOGIN_PATH=/login
    150. - CHOERODON_DEFAULT_REDIRECT_URL=http://localhost:9000
    151. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    152. networks:
    153. - "c7nNetwork"
    154. networks:
    155. c7nNetwork:
    156. driver: bridge

    有关Docker的更多信息请见此处

    有关Docker-Compose的更多信息请见此处