瀏覽代碼

feign示例

yucheng 4 年之前
父節點
當前提交
8ad22e4468

+ 6 - 0
fm-supplier/pom.xml

@@ -11,6 +11,12 @@
     <artifactId>fm-supplier</artifactId>
     <packaging>jar</packaging>
     <dependencies>
+        <!-- swagger -->
+        <dependency>
+            <groupId>com.persagy</groupId>
+            <artifactId>integrated-swagger2-spring-boot-starter</artifactId>
+            <version>${platform.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>

+ 39 - 0
fm-supplier/src/main/java/com/persagy/fm/common/config/FeignConfig.java

@@ -0,0 +1,39 @@
+package com.persagy.fm.common.config;
+
+import feign.Logger;
+import feign.codec.Encoder;
+import feign.form.spring.SpringFormEncoder;
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
+import org.springframework.cloud.openfeign.support.SpringEncoder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.context.annotation.Scope;
+
+import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;
+
+/**
+ * feign配置类
+ * @author lixing
+ * @version V1.0 2021/3/29 8:11 下午
+ */
+@Configuration
+public class FeignConfig {
+
+    @Autowired
+    private ObjectFactory<HttpMessageConverters> messageConverters;
+
+    @Bean
+    @Primary
+    @Scope(SCOPE_PROTOTYPE)
+    Encoder feignFormEncoder() {
+        return new SpringFormEncoder(new SpringEncoder(this.messageConverters));
+    }
+
+    @Bean
+    Logger.Level feignLevel(){
+        return Logger.Level.FULL;
+    }
+}

+ 78 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/client/RoleClient.java

@@ -0,0 +1,78 @@
+package com.persagy.fm.saas.client;
+
+import com.persagy.fm.common.config.FeignConfig;
+import com.persagy.fm.common.response.FmResponseContent;
+import com.persagy.fm.common.response.FmResponseMsg;
+import com.persagy.fm.saas.fallback.RoleClientFallbackFactory;
+import com.persagy.fm.saas.model.SaasRole;
+import com.persagy.fm.saas.model.dto.DeleteSaasRoleDTO;
+import com.persagy.fm.saas.model.dto.PageQuerySaasRoleDTO;
+import com.persagy.fm.saas.model.dto.QuerySaasRoleDTO;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+import java.util.List;
+
+/**
+ * 运维系统角色接口
+ * @author lixing
+ * @version V1.0 2021/3/22 2:13 下午
+ **/
+@FeignClient(name = RoleClient.CLIENT_NAME, fallbackFactory = RoleClientFallbackFactory.class,
+        configuration = FeignConfig.class)
+public interface RoleClient {
+
+    /** 服务名 */
+    String CLIENT_NAME = "saas-account";
+
+    /**
+     * 分页查询账号列表
+     * @param pageQuerySaasRoleDTO 分页查询条件
+     * @return 角色列表
+     * @author lixing
+     * @version V1.0 2021/3/23 11:57 上午
+     */
+    @PostMapping("/role/queryRolePageList")
+    FmResponseContent<List<SaasRole>> queryRolePageList(@RequestBody PageQuerySaasRoleDTO pageQuerySaasRoleDTO);
+
+    /**
+     * 查询账号列表
+     * @param querySaasRoleDTO 查询入参
+     * @return 角色列表
+     * @author lixing
+     * @version V1.0 2021/4/1 10:05 上午
+     */
+    @PostMapping("/role/querySaasRoleList")
+    FmResponseContent<List<SaasRole>> queryRoleList(@RequestBody QuerySaasRoleDTO querySaasRoleDTO);
+
+    /**
+     * 创建角色
+     * @param saasRole 创建角色入参
+     * @return 角色id
+     * @author lixing
+     * @version V1.0 2021/3/23 11:57 上午
+     */
+    @PostMapping("/role/createSaasRole")
+    FmResponseContent<String> createSaasRole(@RequestBody SaasRole saasRole);
+
+    /**
+     * 更新角色
+     * @param saasRole 更新角色入参
+     * @return FmResponseMsg
+     * @author lixing
+     * @version V1.0 2021/3/23 11:57 上午
+     */
+    @PostMapping("/role/updateSaasRole")
+    FmResponseMsg updateSaasRole(@RequestBody SaasRole saasRole);
+
+    /**
+     * 删除角色
+     * @param deleteSaasRoleDTO 删除角色入参
+     * @return FmResponseMsg
+     * @author lixing
+     * @version V1.0 2021/3/23 11:57 上午
+     */
+    @PostMapping("/role/deleteSaasRole")
+    FmResponseMsg deleteSaasRole(@RequestBody DeleteSaasRoleDTO deleteSaasRoleDTO);
+}

+ 70 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/facade/RoleFeignFacade.java

@@ -0,0 +1,70 @@
+package com.persagy.fm.saas.facade;
+
+import com.persagy.common.enums.ResponseCode;
+import com.persagy.common.exception.BusinessException;
+import com.persagy.fm.common.response.FmResponseContent;
+import com.persagy.fm.common.response.FmResponseMsg;
+import com.persagy.fm.common.response.PageList;
+import com.persagy.fm.saas.client.RoleClient;
+import com.persagy.fm.saas.model.SaasRole;
+import com.persagy.fm.saas.model.dto.DeleteSaasRoleDTO;
+import com.persagy.fm.saas.model.dto.PageQuerySaasRoleDTO;
+import com.persagy.fm.saas.model.dto.QuerySaasRoleDTO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * 角色服务接口 门面模式
+ * @author Charlie Yu
+ * @date 2021-04-02
+ */
+@Component
+public class RoleFeignFacade {
+
+    @Autowired
+    private RoleClient client;
+
+    public PageList<SaasRole> queryRolePageList(PageQuerySaasRoleDTO dto) {
+        FmResponseContent<List<SaasRole>> page = client.queryRolePageList(dto);
+        // 失败
+        if(!ResponseCode.A00000.getCode().equals(page.getRespCode())) {
+            throw new BusinessException(page.getRespCode(), page.getRespMsg());
+        }
+        return new PageList<>(page.getContent(), page.getCount());
+    }
+
+    public List<SaasRole> queryRoleList(QuerySaasRoleDTO dto) {
+        FmResponseContent<List<SaasRole>> page = client.queryRoleList(dto);
+        // 失败
+        if(!ResponseCode.A00000.getCode().equals(page.getRespCode())) {
+            throw new BusinessException(page.getRespCode(), page.getRespMsg());
+        }
+        return page.getContent();
+    }
+
+    public void createSaasRole(SaasRole role) {
+        FmResponseContent<String> page = client.createSaasRole(role);
+        // 失败
+        if(!ResponseCode.A00000.getCode().equals(page.getRespCode())) {
+            throw new BusinessException(page.getRespCode(), page.getRespMsg());
+        }
+    }
+
+    public void updateSaasRole(SaasRole role) {
+        FmResponseMsg page = client.updateSaasRole(role);
+        // 失败
+        if(!ResponseCode.A00000.getCode().equals(page.getRespCode())) {
+            throw new BusinessException(page.getRespCode(), page.getRespMsg());
+        }
+    }
+
+    public void deleteSaasRole(DeleteSaasRoleDTO dto) {
+        FmResponseMsg page = client.deleteSaasRole(dto);
+        // 失败
+        if(!ResponseCode.A00000.getCode().equals(page.getRespCode())) {
+            throw new BusinessException(page.getRespCode(), page.getRespMsg());
+        }
+    }
+}

+ 19 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/fallback/RoleClientFallbackFactory.java

@@ -0,0 +1,19 @@
+package com.persagy.fm.saas.fallback;
+
+import com.persagy.fm.saas.client.RoleClient;
+import feign.hystrix.FallbackFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * 运维系统角色接口熔断处理类
+ * @author lixing
+ * @version V1.0 2021/3/24 2:59 下午
+ */
+@Component
+public class RoleClientFallbackFactory implements FallbackFactory<RoleClient> {
+
+    @Override
+    public RoleClient create(Throwable throwable) {
+        return null;
+    }
+}

+ 77 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/ConvertSaasRoleTool.java

@@ -0,0 +1,77 @@
+package com.persagy.fm.saas.model;
+
+import com.persagy.fm.saas.model.dto.AddSaasRoleDTO;
+import com.persagy.fm.saas.model.dto.UpdateSaasRoleDTO;
+import com.persagy.fm.saas.model.vo.SaasRoleItemVO;
+import com.persagy.fm.saas.model.vo.SaasRoleListItemVO;
+import org.mapstruct.Mapper;
+import org.mapstruct.MappingTarget;
+import org.mapstruct.NullValueCheckStrategy;
+import org.mapstruct.NullValueMappingStrategy;
+import org.mapstruct.factory.Mappers;
+
+import java.util.List;
+
+/**
+ * (SaasRole) dto、vo、do转换工具类
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:28 2021-03-22 19:04:28
+ */
+@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT,
+        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
+public interface ConvertSaasRoleTool {
+
+    ConvertSaasRoleTool INSTANCE = Mappers.getMapper(ConvertSaasRoleTool.class);
+
+    /**
+     * do转换为ResponseItemVO
+     *
+     * @param saasRole do对象
+     * @return ResponseItemVO
+     * @author lixing
+     * @version V1.0 2021-03-22 19:04:28
+     */
+    SaasRoleItemVO convert2ResponseItemDTO(SaasRole saasRole);
+
+    /**
+     * do转换为ResponseListItemVO
+     *
+     * @param saasRole do对象
+     * @return ResponseListItemVO
+     * @author lixing
+     * @version V1.0 2021-03-22 19:04:28
+     */
+    SaasRoleListItemVO convert2ResponseListItemDTO(SaasRole saasRole);
+
+    /**
+     * do列表转换为ResponseListItemVO列表
+     *
+     * @param saasRoleList do列表
+     * @return ResponseListItemVO列表
+     * @author lixing
+     * @version V1.0 2021-03-22 19:04:28
+     */
+    List<SaasRoleListItemVO> convert2List(List<SaasRole> saasRoleList);
+
+    /**
+     * addDTO转换为do
+     *
+     * @param addSaasRoleDTO addDTO
+     * @return 要创建的do对象
+     * @author lixing
+     * @version V1.0 2021-03-22 19:04:28
+     */
+    SaasRole convertAddDto2Entity(AddSaasRoleDTO addSaasRoleDTO);
+
+    /**
+     * updateDTO转换为实体
+     *
+     * @param saasRole          更新前的do对象
+     * @param updateSaasRoleDTO updateDTO
+     * @return 更新后的do对象
+     * @author lixing
+     * @version V1.0 2021-03-22 19:04:28
+     */
+    SaasRole convertUpdateDto2Entity(@MappingTarget SaasRole saasRole, UpdateSaasRoleDTO updateSaasRoleDTO);
+}
+

+ 32 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/SaasBaseDTO.java

@@ -0,0 +1,32 @@
+package com.persagy.fm.saas.model;
+
+import com.persagy.fm.common.context.DefaultAppContext;
+import com.persagy.fm.saas.account.constant.enums.AccountBelongEnum;
+import lombok.Data;
+
+/**
+ * 运维平台基础dto类
+ *
+ * @author lixing
+ * @version V1.0 2021/3/26 4:48 下午
+ **/
+@Data
+public class SaasBaseDTO {
+    private String accountBelong;
+    private String groupCode;
+    private String appId;
+    private String accountId;
+
+    /**
+     * 设置默认的属性
+     *
+     * @author lixing
+     * @version V1.0 2021/3/26 11:32 上午
+     */
+    public void setDefaultValue() {
+        this.setAccountBelong(AccountBelongEnum.BUSINESS.getType());
+        this.setAccountId(DefaultAppContext.getContext().getAccountId());
+        this.setGroupCode(DefaultAppContext.getContext().getGroupCode());
+        this.setAppId(DefaultAppContext.getContext().getAppId());
+    }
+}

+ 29 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/SaasBasePageDTO.java

@@ -0,0 +1,29 @@
+package com.persagy.fm.saas.model;
+
+import com.persagy.fm.common.constant.PageQueryConstants;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * 运维平台基础dto类
+ *
+ * @author lixing
+ * @version V1.0 2021/3/26 4:48 下午
+ **/
+@EqualsAndHashCode(callSuper = true)
+@Data
+public class SaasBasePageDTO extends SaasBaseDTO{
+    private Integer page;
+    private Integer size;
+
+    @Override
+    public void setDefaultValue() {
+        super.setDefaultValue();
+        if (this.page == null) {
+            this.setPage(PageQueryConstants.DEFAULT_PAGE);
+        }
+        if (this.size == null) {
+            this.setSize(PageQueryConstants.DEFAULT_SIZE);
+        }
+    }
+}

+ 42 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/SaasRole.java

@@ -0,0 +1,42 @@
+package com.persagy.fm.saas.model;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * (SaasRole)实体类
+ *
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:59
+ */
+@Data
+@ApiModel(description = "")
+public class SaasRole extends SaasBaseDTO {
+    private static final long serialVersionUID = -13451619873751031L;
+
+    @ApiModelProperty("主键")
+    private String id;
+
+    @ApiModelProperty("角色编码")
+    private String roleCode;
+
+    @ApiModelProperty("角色名称")
+    private String roleName;
+
+    @ApiModelProperty("角色类型")
+    private String roleType;
+
+    @ApiModelProperty("备注")
+    private String remark;
+
+
+    public static String PROP_ROLE_CODE = "role_code";
+    public static String PROP_ROLE_NAME = "role_name";
+    public static String PROP_ROLE_TYPE = "role_type";
+    public static String PROP_ACCOUNT_BELONG = "account_belong";
+    public static String PROP_ACCOUNT_ID = "account_id";
+    public static String PROP_APP_ID = "app_id";
+    public static String PROP_REMARK = "remark";
+
+}

+ 26 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/dto/AddSaasRoleDTO.java

@@ -0,0 +1,26 @@
+package com.persagy.fm.saas.model.dto;
+
+import com.persagy.fm.saas.model.SaasBaseDTO;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:19
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@ApiModel(value = "创建入参")
+public class AddSaasRoleDTO extends SaasBaseDTO {
+    @ApiModelProperty(value = "角色名称", required = true)
+    @NotNull(message = "角色名称不能为空")
+    private String roleName;
+
+    @ApiModelProperty(value = "角色类型", required = true)
+    @NotNull(message = "角色类型不能为空")
+    private String roleType;
+}

+ 23 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/dto/DeleteSaasRoleDTO.java

@@ -0,0 +1,23 @@
+package com.persagy.fm.saas.model.dto;
+
+import com.persagy.fm.saas.model.SaasBaseDTO;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:22
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@ApiModel(value = "删除入参")
+public class DeleteSaasRoleDTO extends SaasBaseDTO {
+    @NotNull(message = "id不能为空")
+    @ApiModelProperty(value = "主键", required = true)
+    private String id;
+
+}

+ 25 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/dto/PageQuerySaasRoleDTO.java

@@ -0,0 +1,25 @@
+package com.persagy.fm.saas.model.dto;
+
+import com.persagy.fm.saas.model.SaasBasePageDTO;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:24
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@ApiModel(value = "分页查询入参")
+public class PageQuerySaasRoleDTO extends SaasBasePageDTO {
+    @ApiModelProperty(value = "角色id")
+    private String id;
+
+    @ApiModelProperty(value = "角色名称")
+    private String roleName;
+
+    @ApiModelProperty(value = "角色类型", required = true)
+    private String roleType;
+}

+ 30 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/dto/QuerySaasRoleDTO.java

@@ -0,0 +1,30 @@
+package com.persagy.fm.saas.model.dto;
+
+import com.persagy.fm.saas.model.SaasBaseDTO;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.util.List;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:24
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@ApiModel(value = "查询入参")
+public class QuerySaasRoleDTO extends SaasBaseDTO {
+    @ApiModelProperty(value = "角色id")
+    private String id;
+
+    @ApiModelProperty(value = "角色id列表")
+    private List<String> roleIds;
+
+    @ApiModelProperty(value = "角色名称")
+    private String roleName;
+
+    @ApiModelProperty(value = "角色类型", required = true)
+    private String roleType;
+}

+ 26 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/dto/UpdateSaasRoleDTO.java

@@ -0,0 +1,26 @@
+package com.persagy.fm.saas.model.dto;
+
+import com.persagy.fm.saas.model.SaasBaseDTO;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:21
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@ApiModel(value = "更新入参")
+public class UpdateSaasRoleDTO extends SaasBaseDTO {
+    @ApiModelProperty(value = "主键", required = true)
+    @NotNull(message = "主键不能为空") 
+    private String id;
+
+    @ApiModelProperty(value = "角色名称", required = true)
+    @NotNull(message = "角色名称不能为空") 
+    private String roleName;
+}

+ 40 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/vo/SaasRoleItemVO.java

@@ -0,0 +1,40 @@
+package com.persagy.fm.saas.model.vo;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:25
+ */
+@Data
+@ApiModel(value = "返回结果参数")
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class SaasRoleItemVO {
+    @ApiModelProperty("主键")
+    private String id;
+
+    @ApiModelProperty("角色编码")
+    private String roleCode;
+
+    @ApiModelProperty("角色名称")
+    private String roleName;
+
+    @ApiModelProperty("角色类型")
+    private String roleType;
+
+    @ApiModelProperty("账号所属")
+    private String accountBelong;
+
+    @ApiModelProperty("账号id")
+    private String accountId;
+
+    @ApiModelProperty("应用id")
+    private String appId;
+
+    @ApiModelProperty("备注")
+    private String remark;
+
+}

+ 22 - 0
fm-supplier/src/main/java/com/persagy/fm/saas/model/vo/SaasRoleListItemVO.java

@@ -0,0 +1,22 @@
+package com.persagy.fm.saas.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+
+/**
+ * @author lixing
+ * @version V1.0 2021-03-22 19:04:27
+ */
+@Data
+@ApiModel(value = "列表返回结果参数")
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class SaasRoleListItemVO {
+    @ApiModelProperty("主键")
+    private String id;
+
+    @ApiModelProperty("角色名称")
+    private String roleName;
+}