前置条件

介绍

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

启动todo 服务

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

  1. mvn clean spring-boot:run
  • 本地开发服务,如果不调用其他服务,则可以不需要启动注册中心,在TodoServiceApplication中不需要添加@EnableEurekaClient 注解。
  • 如果不需要测试到kafka相关的,可以将kafka的相关的依赖注释掉。提交时再打开。

服务注册

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/

如果需要自动将该服务在线上添加到路由列表中,需在xxx.infra.util包下创建拓展数据配置类,并继承ExtraDataManager,以用于自动初始化路由。示例如下:

  1. @ChoerodonExtraData
  2. public class CustomExtraDataManager implements ExtraDataManager {
  3. @Override
  4. public ExtraData getData() {
  5. ChoerodonRouteData choerodonRouteData = new ChoerodonRouteData();
  6. choerodonRouteData.setName("todo");
  7. choerodonRouteData.setPath("/todo/**");
  8. choerodonRouteData.setServiceId("choerodon-todo-service");
  9. extraData.put(ExtraData.ZUUL_ROUTE_DATA, choerodonRouteData);
  10. return extraData;
  11. }
  12. }

或者以管理员权限登录平台,在管理 -> 微服务管理 -> 路由管理 中添加对应服务的路由信息。

接口权限

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

包含如下参数。

参数名说明
level接口层级,ResourceLevel.SITE,ResourceLevel.ORGANIZATION,ResourceLevel.PROJECT三种
permissionLogin登录允许访问,默认为false
permissionPublic公开接口,默认为false

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

本地测试

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

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

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

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

启动相关服务

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

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

这里提供一份docker-compose.yaml仅供参考,具体根据配置修改本地程序的配置。服务启动之前,请确保iam-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.11.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. gateway-helper:
  70. container_name: gateway-helper
  71. image: registry.cn-shanghai.aliyuncs.com/choerodon/gateway-helper:0.11.0
  72. depends_on:
  73. - eureka-server
  74. - redis
  75. - mysql
  76. - redis
  77. links:
  78. - eureka-server
  79. - redis
  80. - mysql
  81. - redis
  82. ports:
  83. - "9180:9180"
  84. environment:
  85. - SPRING_CLOUD_CONFIG_ENABLED=false
  86. - HYSTRIX_STREAM_QUEUE_ENABLED=false
  87. - SPRING_CLOUD_BUS_ENABLED=false
  88. - SPRING_SLEUTH_STREAM_ENABLED=false
  89. - LOGGING_LEVEL=WARN
  90. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
  91. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
  92. - SPRING_DATASOURCE_USERNAME=choerodon
  93. - SPRING_DATASOURCE_PASSWORD=123456
  94. - SPRINT_CACHE_NULTI_L1_ENABLED=false
  95. - SPRINT_CACHE_NULTI_L2_ENABLED=false
  96. - SPRINT_REDIS_HOST=redis
  97. - SPRING_REDIS_PORT=6379
  98. - SPRING_REDIS_DATABASE=4
  99. - SPRING_APPLICATION_JSON={"zuul":{"routes":{"dev":{"path":"/todo/**", "serviceId":"choerodon-todo-service"}}}}
  100. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
  101. networks:
  102. - "c7nNetwork"
  103. iam-service:
  104. container_name: iam-service
  105. image: registry.cn-shanghai.aliyuncs.com/choerodon/iam-service:0.11.0
  106. depends_on:
  107. - eureka-server
  108. - mysql
  109. links:
  110. - eureka-server
  111. - mysql
  112. ports:
  113. - "8030:8030"
  114. environment:
  115. - SPRING_CLOUD_CONFIG_ENABLED=false
  116. - HYSTRIX_STREAM_QUEUE_ENABLED=false
  117. - SPRING_CLOUD_BUS_ENABLED=false
  118. - SPRING_SLEUTH_STREAM_ENABLED=false
  119. - LOGGING_LEVEL=WARN
  120. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
  121. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
  122. - SPRING_DATASOURCE_USERNAME=choerodon
  123. - SPRING_DATASOURCE_PASSWORD=123456
  124. - CHOERODON_SAGA_CONSUMER_ENABLED=false
  125. - CHOERODON_SCHEDULE_CONSUMER_ENABLED=false
  126. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
  127. networks:
  128. - "c7nNetwork"
  129. manager-service:
  130. container_name: manager-service
  131. image: registry.cn-shanghai.aliyuncs.com/choerodon/manager-service:0.11.0
  132. depends_on:
  133. - eureka-server
  134. - mysql
  135. links:
  136. - eureka-server
  137. - mysql
  138. ports:
  139. - "8963:8963"
  140. environment:
  141. - SPRING_CLOUD_CONFIG_ENABLED=false
  142. - HYSTRIX_STREAM_QUEUE_ENABLED=false
  143. - SPRING_CLOUD_BUS_ENABLED=false
  144. - SPRING_SLEUTH_STREAM_ENABLED=false
  145. - LOGGING_LEVEL=WARN
  146. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
  147. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
  148. - SPRING_DATASOURCE_USERNAME=choerodon
  149. - SPRING_DATASOURCE_PASSWORD=123456
  150. - CHOERODON_SWAGGER_CLIENT=client
  151. - CHOERODON_SWAGGER_OAUTH_URL=http://api-gateway:8080/oauth/oauth/authorize
  152. - CHOERODON_GATEWAY_DOMAIN=api-gateway:8080
  153. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
  154. networks:
  155. - "c7nNetwork"
  156. oauth-server:
  157. container_name: oauth-server
  158. image: registry.cn-shanghai.aliyuncs.com/choerodon/oauth-server:0.11.0
  159. depends_on:
  160. - eureka-server
  161. - redis
  162. - mysql
  163. - redis
  164. links:
  165. - eureka-server
  166. - redis
  167. - mysql
  168. - redis
  169. ports:
  170. - "8020:8020"
  171. environment:
  172. - SPRING_CLOUD_CONFIG_ENABLED=false
  173. - HYSTRIX_STREAM_QUEUE_ENABLED=false
  174. - SPRING_CLOUD_BUS_ENABLED=false
  175. - SPRING_SLEUTH_STREAM_ENABLED=false
  176. - LOGGING_LEVEL=WARN
  177. - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
  178. - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
  179. - SPRING_DATASOURCE_USERNAME=choerodon
  180. - SPRING_DATASOURCE_PASSWORD=123456
  181. - SPRINT_REDIS_HOST=redis
  182. - SPRING_REDIS_PORT=6379
  183. - SPRING_REDIS_DATABASE=4
  184. - CHOERODON_OAUTH_LOGIN_SSL=false
  185. - CHOERODON_OAUTH_LOGIN_PATH=/login
  186. - CHOERODON_DEFAULT_REDIRECT_URL=http://localhost:9000
  187. - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
  188. networks:
  189. - "c7nNetwork"
  190. networks:
  191. c7nNetwork:
  192. driver: bridge

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

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