01_分布式电商系统搭建dubbo+zk+SpringBoot

01_分布式电商系统搭建dubbo+zk+SpringBoot

代码请前往我上传的资料 分布式电商系统
zk使用window版本

商城

一、主要功能点:

  • 前后分离
  • FastDFS图片上传. springboot配置静态资源上传路径
  • 商品展示
  • 全文搜索
  • 购物车
  • 订单
  • 支付
  • 单点登录(一个公司下有多个项目,某个场景中A项目中用到B项目,A项目不用等了都可以使用B项目相关服务)、第三方登录
  • 支付订单和库存消息通知【分布式事物】
  • 秒杀、限流
  • 运维部署 docker + k8s

建立模块的时候,使用maven工程,使用parent标签引用父工程。

项目框架搭建

每个模块需要的依赖

分布式微服务

二、项目架构

  • gmall —– 项目文件夹
    • gmall-parent 版本控制
    • gmall-common-util
    • gmall-service-util —— 通用服务工具类
    • gmall-web-util
    • gmall-api ——接口
    • gmall-user-service
    • gmall-user-web ——这个是给前端工程访问 @restController

1.gamll-parent

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<fastjson.version>1.2.46</fastjson.version>
<dubbo-starter.version>1.0.10</dubbo-starter.version>
<dubbo.version>2.6.0</dubbo.version>
<zkclient.version>0.10</zkclient.version>
<mybatis.version>1.3.1</mybatis.version>
<nekohtml.version>1.9.20</nekohtml.version>
<xml-apis.version>1.4.01</xml-apis.version>
<batik-ext.version>1.9.1</batik-ext.version>
<jsoup.version>1.11.2</jsoup.version>
<httpclient.version>4.5.5</httpclient.version>
<commons-lang3.version>3.7</commons-lang3.version>
<mapper-starter.version>1.2.3</mapper-starter.version>
<jedis.version>2.9.0</jedis.version>
<jest.version>5.3.3</jest.version>
<jna.version>4.5.1</jna.version>
<beanUtils.version>1.9.3</beanUtils.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>com.gitee.reger</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-starter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>${nekohtml.version}</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>${xml-apis.version}</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-ext</artifactId>
<version>${batik-ext.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper-starter.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>${jest.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>${jna.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${beanUtils.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

2.gmall-common-util

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--都需要依赖父工程 gmall-parent -->
<parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-common-util</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<!--start: dubbo、zk、springboot整合dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.gitee.reger</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
</dependency>
<!--end :dubbo、zk、springboot整合dubbo -->
</dependencies>
</project>

3.gmall-service-util

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-service-util</artifactId>
<version>1.0-SNAPSHOT</version>
<!--都需要依赖父工程 gmall-parent -->
<parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-common-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
</project>

4.gmall-web-util

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-web-util</artifactId>
<version>1.0-SNAPSHOT</version>
<!--都需要依赖父工程 gmall-parent -->
<parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 依赖gmall-common-util-->
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-common-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 静态模板 所需要的依赖,gmall-user-web 需要使用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>

5.gmall-api

5.1 pom.xml

编写接口和存放相关的bean。也可以把bean放到common-util 这个要看个人怎么搭建项目架构.user-web。依赖api接口,不就把bean一起了。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-api</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 通用mapper gmall-user-service 依赖gmall-api接口-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.12</version>
</dependency>
</dependencies>
</project>

5.2 UmsMember.java

package com.atguigu.gmall.bean;
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;
@Data
public class UmsMember implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
private String username;
private String password;
private String nickname;
private String phone;
private int status;
private Date createTime;
private String icon;
private int  gender;
private Date birthday;
private String  city;
private String job;
private String  personalizedSignature;
private int sourceType;
private int  integration;
private int growth;
private int  luckeyCount;
private int historyIntegration;
}

5.3 UmsMemberService.java

package com.atguigu.gmall.service;
import com.atguigu.gmall.bean.UmsMember;
import java.util.List;
public interface UmsMemberService {

List<UmsMember> findUmsMemberAll();
}

6.gmall-user-service

6.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-user-service</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-service-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-common-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

6.2 UmsMemberMapper.java

package com.atguigu.gmall.user.mapper;
import com.atguigu.gmall.bean.UmsMember;
import tk.mybatis.mapper.common.BaseMapper;
import java.util.List;
public interface UmsMemberMapper  extends BaseMapper<UmsMember> {

List<UmsMember> selectAllUser();
}

通用mapper和springboot整合,还有如果需要写xml文件的sql。需要在application.properties中指定xxxMapper.xml文件的路径,需要tik.mapper和springboot整合jar包,需要mybatis的jar包。

6.3 application.properties

# 服务端口
server.port=8070
# jdbc
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/gmall?characterEncoding=UTF-8
# mybtais配置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
# 日志级别
logging.level.root=info
# dubbo的配置
# dubbo中的服务名称
spring.dubbo.application=user-service
# dubbo的通讯协议名称
spring.dubbo.protocol.name=dubbo
# zookeeper注册中心的地址
#spring.dubbo.registry.address=192.168.222.20:2181
spring.dubbo.registry.address=192.168.2.167:2181
# zookeeper的通讯协议的名称
spring.dubbo.registry.protocol=zookeeper
# dubbo的服务的扫描路径
spring.dubbo.base-package=com.atguigu.gmall

6.4 UmsMemberMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.gmall.user.mapper.UmsMemberMapper">
<select id="selectAllUser" resultType="com.atguigu.gmall.bean.UmsMember">
select t.*  from ums_member t
</select>
</mapper>

6.5 UmsMemberServiceImpl.java

注意:@Service 使用dubbo

package com.atguigu.gmall.user.service.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.gmall.bean.UmsMember;
import com.atguigu.gmall.service.UmsMemberService;
import com.atguigu.gmall.user.mapper.UmsMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service
public class UmsMemberServiceImpl  implements UmsMemberService {

@Autowired
private UmsMemberMapper umsMemberMapper;
@Override
public List<UmsMember> findUmsMemberAll() {

return umsMemberMapper.selectAll();
}
}

6.6 启动类

注意:@MapperScan使用tk.mapper的

package com.atguigu.gmall.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.atguigu.gmall.user.mapper")
public class GmallUserServiceApp {

public static void main(String[] args) {

SpringApplication.run(GmallUserServiceApp.class, args);
}
}

7.gmall-user-web

7.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-user-web</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu.gmall</groupId>
<artifactId>gmall-web-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

7.2 UmsMemberController

注意:@Reference 使用dubbo 相当于 @Autowried

package com.atguigu.gmall.user.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.gmall.bean.UmsMember;
import com.atguigu.gmall.service.UmsMemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class UmsMemberController {

@Reference
private UmsMemberService umsMemberService;
@RequestMapping("/findAll")
@ResponseBody
public List<UmsMember> findAll(){

return umsMemberService.findUmsMemberAll();
}
}

7.3 启动类

package com.atguigu.gmall;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GmalUserWebApplication {

public static void main(String[] args) {

SpringApplication.run(GmalUserWebApplication.class,args);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/100752.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • pycharm 激活码Key is invalid(JetBrains全家桶)

    (pycharm 激活码Key is invalid)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.cn/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1STL5S9V8F-eyJsaWNlbnNlSWQiOi…

  • 事务隔离级别与锁的对应关系_速度级别H和速度级别S

    事务隔离级别与锁的对应关系_速度级别H和速度级别S隔离级别org.springframework.transaction.annotation.Isolationpublic enum Isolation { DEFAULT(-1), READ_UNCOMMITTED(1), READ_COMMITTED(2), REPEATABLE_READ(4), SERIALIZABLE(8);}DEFAULT :这是默认值,表示使用底层数据库的默认隔离级别。对大部分数据库而言,通常这值就是: READ_CO..

  • 网站接入微信扫码登录并获取用户基本信息(微信开放平台)

    网站接入微信扫码登录并获取用户基本信息(微信开放平台)现在的网站基本都接入微信登陆了,就好像下面这样的。只需要用微信扫一下二维码,这个网站就可以展示你的微信昵称和头像,免去注册账号和输入密码登录的步骤,还免去设置头像和昵称的步骤,所以是挺方便的。那么如何把自己的网站接入这个呢?首先咱们得先在微信开放平台注册账号并且创建一个网站应用,等待审核通过后就可以获得Appid和AppSecretbingqie并且还要设置回调域名,这个不多说。首…

  • LOADRUNNER8.1卸载

    LOADRUNNER8.1卸载

  • apache做负载均衡_apache负载均衡配置

    apache做负载均衡_apache负载均衡配置2019独角兽企业重金招聘Python工程师标准>>>…

  • java通过jdbc连接sql server数据库_mysqljdbc连接数据库代码

    java通过jdbc连接sql server数据库_mysqljdbc连接数据库代码文章目录一、需求二、项目结构三、步骤1、创建数据库、数据表,插入数据2、创建javaweb项目3、下载驱动包4、导入驱动包5、创建包,创建类6、程序7、运行结果一、需求创建一个javaweb项目,读取bookinfo表中的数据,并输出到控制台二、项目结构JDBC.java用来写主程序mysql-connector-java-5.1.47.jar是java连接mysql需要导入的jar包…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号