搭建一个微服务项目

系列文章目录

聚合继承项目


文章目录

  • 系列文章目录
  • 一.打开nacos微服务注册中心
  • 一.父项目管理依赖
  • 二.子项目导入依赖
  • 三.配置文件
  • 四.启动类
  • 五.三层架构
    • 1.controller
    • 2.service
    • 3.dao
  • 六.后端测试


一.打开nacos微服务注册中心

找到nacos的bin目录下以单机模式打开

以单机模式运行nacos

一.父项目管理依赖


<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.0modelVersion>

    <groupId>com.woniuxygroupId>
    <artifactId>happy-lifeartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        
        <module>common-entitymodule>
        <module>activity-releasemodule>
        <module>activity-data-storemodule>
        <module>mall-goodsmodule>

    modules>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <mysql.version>5.1.35mysql.version>
        <mybatis.spring.boot.verison>2.1.0mybatis.spring.boot.verison>
        <druid.spring.boot.starter.version>1.1.10druid.spring.boot.starter.version>
        <spring.boot.version>2.3.12.RELEASEspring.boot.version>
        <spring.cloud.version>Hoxton.SR12spring.cloud.version>
    properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring.boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring.cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>${druid.spring.boot.starter.version}version>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>${mybatis.spring.boot.verison}version>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.2.7.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
project>

二.子项目导入依赖

代码如下(示例):


<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">
    <parent>
        <artifactId>happy-lifeartifactId>
        <groupId>com.woniuxygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>mall-goodsartifactId>

    <dependencies>
        <dependency>
            <groupId>com.woniuxygroupId>
            <artifactId>common-entityartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
    dependencies>

project>

三.配置文件

application.yml如下:

server:
  port: 1818
spring:
  application:
    name: mall-goods
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/happylife
    username: root
    password: 123
    #配置到nacos注册中心
  cloud:
    nacos:
      server-addr: http://localhost:8848

mybatis:
  #这里是配置mapper文件的位置,能够从主配置获取到mapper下的映射文件位置,再通过set方法映射到MapperLocations属性上
  mapper-locations: classpath:/mapper/*.xml
  #配置别名,可以在resultType中简写
  type-aliases-package: com.woniuxy.happylife.entity

四.启动类

@EnableDiscoveryClient
//扫描dao层下面的,生成接口对应的实现类
@MapperScan("com.woniuxy.happylife.dao")
@SpringBootApplication
public class MallGodsApplication {
    public static void main(String[] args) {
        SpringApplication.run(MallGodsApplication.class,args);
    }
}

五.三层架构

1.controller

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("/")
    public ResponseEntity<List<MallGoods>> getAll(){
        return new ResponseEntity<List<MallGoods>>(200,"查到所有商品数据", goodsService.findAll());
    }
}

2.service

public interface GoodsService {
    List<MallGoods> findAll();
}
//注册到容器中
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsDao goodsDao;
    @Override
    public List<MallGoods> findAll() {
        return goodsDao.findAll();
    }
}

3.dao

public interface GoodsDao {
    List<MallGoods> findAll();
}

生成的代理类文件如下

DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.woniuxy.happylife.dao.GoodsDao">
    <select id="findAll" resultType="com.woniuxy.happylife.entity.MallGoods">
        select * from mall_goods
    select>
mapper>

六.后端测试

可以用postman|postwoman测试,这里使用了http测试

http-client.env.json文件如下:

{
  "dev": {
    "host": "localhost",
    "port": "1818",
    "project": "",
    "name": "value"
  }
}

goods.http文件如下:

GET http://{{host}}:{{port}}{{project}}/goods/

你可能感兴趣的:(微服务,spring,boot,java)