用户微服务Identity
1、创建表
先引入WBC.User.Domain中Volo.Abp.Identity.Domain 包
<PackageReference Include="Volo.Abp.Identity.Domain" Version="4.4.3" />
<PackageReference Include="Volo.Abp.IdentityServer.Domain" Version="4.4.3" />
在UserDomainModule中使用集成模块
typeof(AbpIdentityDomainModule)//集成Identity领域模块
由于创建表需要使用ef所以在ef中也需要集成Identity
<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="4.4.3" />
<PackageReference Include="Volo.Abp.Identity.EntityFrameworkCore" Version="4.4.3" />
在UserEntityFrameworkCoreModule使用
typeof(AbpIdentityEntityFrameworkCoreModule)//集成AbpIdentityEntityFrameworkCoreModule
//添加Identity表
//去除前缀
AbpIdentityDbProperties.DbTablePrefix = "";
builder.ConfigureIdentity();
在用户微服务中生成迁移文件并生成表(原有的如果有可以删除这样不容易出错)
dotnet ef migrations add user
dotnet ef database update
创建成功
在WBC.User.Application引入Volo.Abp.Identity.Application并使用
nuget包
<PackageReference Include="Volo.Abp.Identity.Application" Version="4.4.3" />
UserApplicationModule使用
typeof(AbpIdentityApplicationModule)//集成AbpIdentityApplicationModule
WBC.User.HttpApi集成Volo.Abp.Identity.HttpApi
<PackageReference Include="Volo.Abp.Identity.HttpApi" Version="4.4.3" />
在UserHttpApiModule使用
typeof(AbpIdentityHttpApiModule)//集成AbpIdentityHttpApiModule
集成Volo.Abp.Account.Application
<PackageReference Include="Volo.Abp.Account.Application" Version="4.4.3" />
UserApplicationModule使用
typeof(AbpAccountApplicationModule)//AbpAccountApplicationModule
集成Volo.Abp.Account.Application
<PackageReference Include="Volo.Abp.Account.HttpApi" Version="4.4.3" />
UserHttpApiModule使用
typeof(AbpAccountHttpApiModule)//集成AbpAccountHttpApiModule
由于创建时去掉了表前缀所以在微服务访问时也要去掉否则会访问失败
运行用户微服务看看是否能够正常使用
集成到网关
由于网关直接使用的微服务所以可以直接调用
在网关层加入配置文件(路径不一样所以需要添加一个配置)
{
"UpstreamPathTemplate": "/api/account/{everything}",
"UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ],
"DownstreamPathTemplate": "/api/account/{everything}",
"DownstreamScheme": "http",
"ServiceName": "userservices",
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
调用网关层看看能不能正常添加
聚合服务集成
引入Volo.Abp.Account.HttpApi.Client nuget包
<PackageReference Include="Volo.Abp.Account.HttpApi.Client" Version="4.4.3" />
SeckillAggregateModule中使用
typeof(AbpAccountHttpApiClientModule)//集成AbpAccountHttpApiClientModule
在appsettings.json中加入AbpAccount的地址
"AbpAccount": {
"BaseUrl": "http://localhost:8002/"
}
添加Users控制器并创建注册方法
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Account;
using Volo.Abp.Identity;
namespace WBC.SeckillAggregate.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
public IAccountAppService _accountAppService { get; set; }
//用户注册
[HttpPost("Registry")]
public async Task<IdentityUserDto> CreateUser(RegisterDto req)
{
return await _accountAppService.RegisterAsync(req);
}
}
}
运行聚合服务测试是否能够正常使用
vue项目调用注册
创建注册页面并定义路由
注册功能实现(布局简单使用elementpuls写了一下)
<template>
<el-form :model="form" label-width="120px">
<el-form-item label="姓名">
<el-input v-model="form.userName" />
</el-form-item>
<el-form-item label="邮箱">
<el-input v-model="form.emailAddress" />
</el-form-item>
<el-form-item label="密码">
<el-input type="password" v-model="form.password" />
</el-form-item>
<el-form-item label="服务名称">
<el-input v-model="form.appName" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">添加</el-button>
<router-link to="index"><el-button>返回主页</el-button></router-link>
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { Post } from "@/http";
import { ElMessage } from "element-plus";
//引入rouer路由
import { useRoute, useRouter } from "vue-router";
const router = useRouter();
// do not use same name with ref
const form = ref({
userName: "wangbenchi",
emailAddress: "69945864@qq.com",
password: "Wbc123!@#",
appName: "聚合服务",
});
onMounted(() => {
});
const onSubmit = () => {
console.log(form.value);
//请求接口
Post("/Users/Registry", form.value).then((res: any) => {
console.log(res);
if (res.data.Success) {
Success();
}
});
};
const Success = () => {
ElMessage({
message: "注册成功",
type: "success",
duration: 2000,
onClose: () => {
router.push("login");
},
});
};
</script>