首页 > 其他分享 >会员中心微服务

会员中心微服务

时间:2024-03-23 23:30:22浏览次数:23  
标签:服务 sun 会员中心 member springcloud import com public

文章目录

1.环境配置

1.创建会员中心模块

image-20240322113235884

2.检查父子模块的pom.xml
1.父模块
注意:如果父模块中的依赖显示not found,原因是子模块并没有引用,不用在意

image-20240322113712318

2.子模块

image-20240322113535868

3.pom.xml 引入相关依赖(别忘记刷新maven)
    <dependencies>
        <!-- springboot web starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 如果在子工程/模块指定了 version,则以指定为准 -->
        </dependency>
        <!-- 
        1. starter-actuator 是 springboot 程序的监控系统,可以实现健康检查,info 信息
        等
        2. 访问 http://localhost:10000/actuator 可以看到相关链接, 还可以做相关设置. -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <!-- 这里我们重新指定一下 version 因为父项目中没有对这个依赖进行版本仲裁-->
            <version>1.1.13</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
4.application.yml 进行基本配置
server:
  port: 10000 # 配置服务端口
spring:
  application:
    name: member-service-provider-10000 # 配置服务的名称,名字任意这里与项目名保持一致
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    # 别忘记创建数据库之后修改数据库名称
    url: jdbc:mysql://localhost:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml # 扫描所有Mapper.xml
  type-aliases-package: com.sun.springcloud.entity # 扫描所有实体类,可以直接使用类名来标识

5.com/sun/springcloud/MemberApplication.java 编写主程序测试
package com.sun.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/22 12:49
 * @Version 1.0
 */
@SpringBootApplication
public class MemberApplication {
    public static void main(String[] args) {
        SpringApplication.run(MemberApplication.class, args);
    }
}

6.出现两个问题
1.端口冲突,所以这里换一个端口为10001,将子模块名也一并修改!

image-20240323101726428

2.数据库没创建

image-20240323101910525

7.使用云数据库
1.数据库表设计
CREATE DATABASE e_commerce_center_db;
USE e_commerce_center_db;
CREATE TABLE member
(
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT 'id',
    NAME VARCHAR(64) COMMENT '用户名',
    pwd CHAR(32) COMMENT '密码',
    mobile VARCHAR(20) COMMENT '手机号码',
    email VARCHAR(64) COMMENT '邮箱',
    gender TINYINT COMMENT '性别', PRIMARY KEY (id)
);
INSERT INTO member VALUES
    (NULL, 'smith', MD5('123'), '123456789000', '[email protected]', 1);
SELECT * from member;
2.修改application.yml 的port,name以及数据库配置
server:
  port: 10001 # 配置服务端口
spring:
  application:
    name: member-service-provider-10001 # 配置服务的名称,名字任意这里与项目名保持一致
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    # 别忘记创建数据库之后修改数据库名称
    url: 你连接数据库的url
    username: sdfsdfsd
    password: 88888
mybatis:
  mapper-locations: classpath:mapper/*.xml # 扫描所有Mapper.xml
  type-aliases-package: com.sun.springcloud.entity # 扫描所有实体类,可以直接使用类名来标识

3.执行主程序测试

image-20240323110603378

2.代码编写

1.com/sun/springcloud/entity/Member.java 创建实体类
package com.sun.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/23 11:10
 * @Version 1.0
 */

@AllArgsConstructor
@NoArgsConstructor
@Data
//Serializable 加上,后面可能使用
public class Member implements Serializable {
    private Long id;
    private String name;
    private String pwd;
    private String mobile;
    private String email;
    private Integer gender;
}

2.com/sun/springcloud/util/Result.java 封装响应结果的工具类
package com.sun.springcloud.util;

/**
 * Description: 用于返回结果, 利于 json 格式
 *
 * @Author sun
 * @Create 2024/3/23 11:12
 * @Version 1.0
 */

public class Result<T> {
    private String code;
    private String msg;
    private T data;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    public static Result success() {
        Result result = new Result<>();
        result.setCode("200");
        result.setMsg("success");
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setCode("200");
        result.setMsg("success");
        return result;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>(data);
        result.setCode("200");
        result.setMsg(msg);
        return result;
    }

    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(String code, String msg, T data) {
        Result<T> result = new Result<>(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
3.编写dao层
1.com/sun/springcloud/dao/MemberDao.java 编写Mapper接口并注入容器
package com.sun.springcloud.dao;

import com.sun.springcloud.entity.Member;
import org.apache.ibatis.annotations.Mapper;


/**
 * Description: 对member表进行操作
 *
 * @Author sun
 * @Create 2024/3/23 21:06
 * @Version 1.0
 */
@Mapper // 注入容器
public interface MemberDao {

    /**
     * 根据id进行查询
     * @param id
     * @return 查询成功返回Member对象,查询失败返回null
     */
    public Member queryMemberById(Long id);

    /**
     * 保存一条记录
     * @param member
     * @return 返回受影响的行数
     */
    public int save(Member member);
}

2.mapper/MemberMapper.xml 编写Mapper实现类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sun.springcloud.dao.MemberDao">
    <resultMap id="BaseResultMap" type="Member">
        <id column="id" property="id" jdbcType="BIGINT"></id>
        <id column="name" property="name" jdbcType="VARCHAR"></id>
        <id column="pwd" property="pwd" jdbcType="VARCHAR"></id>
        <id column="mobile" property="mobile" jdbcType="VARCHAR"></id>
        <id column="email" property="email" jdbcType="VARCHAR"></id>
        <id column="gender" property="gender" jdbcType="TINYINT"></id>
    </resultMap>
    <select id="queryMemberById" parameterType="Long" resultMap="BaseResultMap">
        select * from `member` where `id` = #{id}
    </select>

    <!-- useGeneratedKeys="true" keyProperty="id" 表示插入之后将自增的主键id的值返回到Member对象中 -->
    <insert id="save" parameterType="Member" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO `member` VALUES
        (NULL, #{name}, MD5(#{pwd}), #{mobile}, #{email}, #{gender});
    </insert>
</mapper>
3.com/sun/springcloud/MemberApplicationTest.java 测试dao层
package com.sun.springcloud;

import com.sun.springcloud.dao.MemberDao;
import com.sun.springcloud.entity.Member;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/23 21:46
 * @Version 1.0
 */
@SpringBootTest
@Slf4j
public class MemberApplicationTest {
    @Resource
    private MemberDao memberDao;

    @Test
    public void queryMemberById() {
        Member member = memberDao.queryMemberById(1L);
        log.info("member={}", member);
    }

    @Test
    public void save() {
        Member member = new Member();
        member.setName("sun");
        member.setPwd("12121");
        member.setMobile("2342343");
        member.setEmail("[email protected]");
        member.setGender(0);

        int save = memberDao.save(member);
        Assert.assertEquals(1, save);
    }
}

image-20240323215932670

4.编写service层
1.com/sun/springcloud/service/MemberService.java 编写service接口
package com.sun.springcloud.service;

import com.sun.springcloud.entity.Member;

/**
 * Description: 对member表进行操作的service
 *
 * @Author sun
 * @Create 2024/3/23 22:02
 * @Version 1.0
 */
public interface MemberService {
    /**
     * 根据id获取一条记录
     *
     * @param id
     * @return 成功返回member对象,失败返回null
     */
    public Member queryMemberById(Long id);


    /**
     * 保存一条记录
     *
     * @param member
     * @return 成功返回1,失败返回0
     */
    public int save(Member member);

}

2.com/sun/springcloud/service/Impl/MemberServiceImpl.java 编写service实现类并注入容器
package com.sun.springcloud.service.Impl;

import com.sun.springcloud.dao.MemberDao;
import com.sun.springcloud.entity.Member;
import com.sun.springcloud.service.MemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description: member表的service实现类
 *
 * @Author sun
 * @Create 2024/3/23 22:05
 * @Version 1.0
 */
@Service // 注入容器
public class MemberServiceImpl implements MemberService {
    @Resource // 依赖注入memberdao
    private MemberDao memberDao;
    @Override
    public Member queryMemberById(Long id) {
        return memberDao.queryMemberById(id);
    }

    @Override
    public int save(Member member) {
        return memberDao.save(member);
    }
}

3.测试service层
    @Test
    public void queryMemberById2() {
        Member member = memberService.queryMemberById(1L);
        log.info("member={}", member);
    }

    @Test
    public void save2() {
        Member member = new Member();
        member.setName("sun");
        member.setPwd("12121");
        member.setMobile("2342343");
        member.setEmail("[email protected]");
        member.setGender(0);
        int save = memberService.save(member);
        Assert.assertEquals(1, save);
    }

image-20240323221243863

5.编写controller层
1.com/sun/springcloud/controller/MemberController.java 编写controller
package com.sun.springcloud.controller;

import com.sun.springcloud.entity.Member;
import com.sun.springcloud.service.MemberService;
import com.sun.springcloud.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Description: member表的控制器
 *
 * @Author sun
 * @Create 2024/3/23 22:24
 * @Version 1.0
 */
@RestController
@Slf4j
public class MemberController {
    @Resource // 注入service
    private MemberService memberService;

    /**
     * 根据前端传进来的json数据插入一条记录
     *
     * @param member json类型的member对象数据
     * @return 返回json类型的响应
     */
    @PostMapping("/member/save")
    public Result save(@RequestBody Member member) {
        int save = memberService.save(member);
        if (save > 0) {
            return Result.success("添加会员成功!", save);
        } else {
            return Result.error("401", "添加会员失败");
        }
    }

    /**
     * 根据id来获取某个会员的信息
     *
     * @param id 使用路径参数的形式传入参数
     * @return 返回json格式的数据
     */
    @GetMapping("/member/get/{id}") // 这里使用的路径参数
    public Result getMemberById(@PathVariable("id") Long id) {
        Member member = memberService.queryMemberById(id);

        if (member != null) {
            return Result.success("查询成功!", member);
        } else {
            return Result.error("402", "ID= " + id + "不存在");
        }
    }
}

2.postman测试

image-20240323223620845

image-20240323225016196

标签:服务,sun,会员中心,member,springcloud,import,com,public
From: https://blog.csdn.net/m0_64637029/article/details/136977828

相关文章

  • Windows下安装git服务器
    Windows平台下Git服务器搭建该文章转自:https://www.cnblogs.com/android-joker/p/4513125.html安装和配置Java运行环境1:下载Java运行环境,下载地址:https://www.java.com/zh-CN/download/manual.jsp2:安装jre,配置java环境变量3:右键”计算机”=>"属性"=>"高级系统设置"......
  • Windows服务注册-极语言版
    以下代码请新建工程-初级程序-粘贴窗体对象-粘贴代码模块-保存-关闭程序-再打开。在使用编译好的exe代码之前,请先在以管理员模式运行的cmd窗口中执行以下语句:让任意的cmd窗口都可执行sc命令,让sc等命令不需要以管理员权限执行cmd窗口,执行完以下代码之后,需要重启电脑regadd"H......
  • 安装OpenStack认证服务组件KeyStone--笔记
       以下笔记根据腾讯专家讲解的《云计算与OpenStack》网络课程,地址:1KeyStone简介_哔哩哔哩_bilibili,整理并亲手操作,特此感谢。 OpenStack框架图 KeyStone简介  早期的OpenStack版本,并没有KeyStone身份认证模块。用户、消息、API调用的认证都是放在Nova模块中的......
  • 微服务设计中API Gateway设计
            SpringCloudGateway、Kong和Zuul都是流行的API网关解决方案,它们在微服务架构中扮演着至关重要的角色,今天我们来说说SpringCloudGateway,在后续的文章中我在对其他两个组件进行介绍        SpringCloudGateway是SpringCloud微服务生态系......
  • 自己编译RustDesk,并将自建ID服务器和key信息写入客户端
    前言:搭建RustDesk编译环境    今天总算是把编译环境给折腾清楚了,编译出来了至少能用,但说不上好用,问题还不少,官方的客户端就是要手工填写ID服务器地址和key才可以用,而且还容易被别人白嫖你搭建的服务器,当然如果拿到你编译后的客户端,也是存在被白嫖的可能。这方面还没......
  • 小程序配置服务器域名
    首先登录小程序,点击左侧的开发管理菜单然后找到服务器域名点击后面的修改按钮在弹框中验证管理员的身份后出现如图所示的界面:修改自己的域名后保存提交即可......
  • 游戏开发:服务器部署监控告警
    线上服务器的监控告警,我们暂且从三个层级上分析;业务层:业务相关的日志告警机制。跟业务设计强相关,比如客户端的业务上行请求数据异常之类的告警,业务上定义日志级别(INFO/WARN/ERROR),输出到指定日志文件并通过业务层逻辑抛出,数据分析的埋点、业务行为相关的辅助日志都在这里实现;一套......
  • 阿里云服务器安装mysql后本地连接失败
    阿里云服务器安装mysql后本地连接失败一、问题描述在阿里云安装mysql后,想在本地电脑用可视化工具连接mysql,但是提示连接失败错误如图所示:二、问题分析1、检查3306端口首先,检查阿里云服务器的安全组是否开放了3306端口可以看到,我已经放行了3306端口。2、检查防火墙我选......
  • 实现通过 Python 检查服务器的 443 端口是否接通
    实现通过Python检查服务器的443端口是否接通importsocketdefcheck_port(host,port):try:#创建socket对象s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#设置连接超时时间为3秒s.settimeout(3)#连接......
  • Uni-app 之uni-push1.0服务端推送
    一、配置1、uni-push1.0文档https://uniapp.dcloud.net.cn/unipush-v1.html2、服务端推送文档https://docs.getui.com/getui/server/rest_v2/push/二、示例//0:站内信,1:消息$type=isset($type)?$type:0;$clickType='intent';$payload=array('type'=>�......