# icloud-backend **Repository Path**: zhang_hai_qiang/icloud-backend ## Basic Information - **Project Name**: icloud-backend - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-14 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 微服务 ## 目录结构说说明 ```$xslt ├── icloud-auth 通用类定义 │ │ │  ├── icloud-auth-client 权限服务客户端 │ │ │  └─── icloud-auth-server 权限服务 │  ├── icloud-common 通用类定义 │ │ │  ├── icloud-common-base 基础工具类 │ │ │  ├── icloud-common-util 工具类 │ │ │  └── icloud-common-porovider 微服务相关 │ ├── icloud-control 服务控制管理 │ │  │  └── icloud-app-admin spring boot admin │ ├── icloud-gateway 服务网关 │ ├── icloud-provider 服务提供者(每个模块都是一个微服务) │ │ │ ├── icloud-provider-uac 用户管理中心 系统管理/应用管理 │ │ │ └─ └── icloud-provider-api 服务提供者api(暴露出api供其他微服务模块使用) │ └── icloud-provider-uac-api 用户管理中心api ``` ## 服务端 - mysql - redis - nacos - zipkin - sentinel-dashboard ## 启动顺序 1. AuthServerApplication 2. UacApplication 3. GatewayApplication 4. 其他服务 # 服务开发步骤 ## 添加服务模块 - 在 icloud-provider 模块下新建子模块 - 子模块命名格式为 icloud-provider-服务名称 ## 编写配置文件 - 文件模版在根目录 "微服务相关文件模版"下,拷贝文件到新建微服务项目目录下 - 将模版文件中的 "xxxx" 替换为服务名称 - 模版文件说明 - 微服务相关文件模版/src/main/resources/application.yml:基本配置 - 微服务相关文件模版/src/main/resources/application-dev.yml: 开发环境配置 - 微服务相关文件模版/src/main/resources/bootstrap.yml: 默认配置,不可被覆盖 - 微服务相关文件模版/.yo-rc.json: jhipster相关配置 ## 添加依赖 在pom.xml 加入如下依赖: ```xml com.xd. icloud icloud-common-provider com.xd. icloud icloud-auth-client de.codecentric spring-boot-admin-starter-client org.springframework.cloud spring-cloud-starter-netflix-hystrix org.springframework.cloud spring-cloud-alibaba-sentinel io.github.jhipster jhipster-framework org.mapstruct mapstruct-jdk8 org.mapstruct mapstruct-processor provided provider-xxxx com.xd. icloud icloud-codegen-maven-plugin ${project.groupId} ${project.artifactId} xxxx jdl false 10 org.springframework.boot spring-boot-maven-plugin ``` ## 生成服务代码 ## 编写jdl - 当前为服务模块的根目录建立`jdl`文件夹,在此文件夹下建立jdl文件 - jdl文件可以有多 ## 安装 jhipster ```bash npm install yo -g npm install generator-jhipster -g ``` ### 安装代码生成插件 ![安装插件](./imgs/install_plugin.PNG) ### 生成代码 以微服务 `proplan`为例,生成代码方式如下 ![生成代码](./imgs/gen_code.PNG) ## 将服务配置到网关 - 在` icloud-gateway/src/main/resources/application.yml`新增路由配置: ```yaml spring: cloud: gateway: routes: - id: icloud-provider-xxxx uri: lb:// icloud-provider-xxxx order: 8000 predicates: - Path=/api/xxxx/** filters: - StripPrefix=2 ``` - 在` icloud-gateway/src/main/resources`下新增路以机器名命名的配置文件,如:application-zhinengsuo4.yml 这里的配置主要用来覆盖` icloud-gateway/src/main/resources/application.yml`中路由配置,将在本机开发的服务地址改为`http://localhost:本机端口`。 ```yaml spring: cloud: gateway: routes: - id: icloud-auth-server uri: lb:// icloud-auth-server order: 8000 predicates: - Path=/api/auth/** filters: - StripPrefix=2 - id: icloud-provider-uac uri: lb:// icloud-provider-uac order: 8001 predicates: - Path=/api/uac/** filters: - StripPrefix=2 - id: icloud-provider-proplan uri: http://localhost:9004 order: 8003 predicates: - Path=/api/proplan/** filters: - StripPrefix=2 ``` ## 启动服务 - 启动网关: icloud-gateway\src\main\java\com\xd\ icloud\gateway\GatewayApplication.java - 启动个人开发的微服务: icloud-provider\ icloud-provider-xxxx\src\main\java\com\xd\ icloud\xxxx\XxxxApplication.java # 权限配置说明 在 application.yml 加入如下配置: ```yaml auth: serviceId: icloud-auth-server # 允许权限拦截,需要 interceptor: true user: token-header: Authorization # 需要拦截用户权限的url,这里配置的是拦截所有 include-url-patter: - /** # 不需要拦截用户权限的url exclude-url-patter: - /test client: id: ${spring.application.name} secret: 123456 token-header: x-client-token application-name: ${spring.application.name} # 需要拦截服务权限的url,这里配置的是拦截所有 include-url-patter: - /** ``` 在启动类中按照如下形式编写: ```java @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker @EnableSwagger2Doc @EnableAuthClient // 允许权限控制客服端 @EnableFeignClients // 允许FeignClient访问远程接口 public class XxxApplication { public static void main(String[] args) { SpringApplication.run(XxxApplication.class, args); } } ``` > !!!!!! > 微服务的启动类必须放到包 `com.xd.digitalship.provider`下 > !!!!!! 权限信息获取工具类: TaskDtlxtHandler.getToken: 获取token BaseContextHandler.getName: 获取应户名 BaseContextHandler.getUserID: 获取用户ID