微服务工程搭建

SpringCloud 2018

GitHub



1-总体概述

1.1-版本信息

  • SpringBoot 版本:1.5.9.RELEASE

  • SpringCloud 版本:Dalston.SR1

1.2-案例说明

以 Dept 部门模块做一个微服务的通用案例,Consumer 消费者(Client)通过 Rest 调用 Provider 提供者(Server)提供服务。

1.3-Maven 模块化

简单的 Maven 模块结构是这样的:父项目(app-parent)聚合很多子项目(app-util、app-dao、app-service、app-web)。

|------app-parent
	|---pom.xml(pom)
	
	|------app-util
		|---pom.xml(jar)
	
	|------app-dao
		|---pom.xml(jar)
	
	|------app-service
		|---pom.xml(jar)
	
	|------app-web
		|---pom.xml(war)

将案例拆分成如下模块:

microservicecloud:父工程(Project)下带着 3 个子模块(Module)

  • microservicecloud-api(封装整体 Entity、接口、公共配置等)

  • microservicecloud-provider-dept-8001(微服务服务提供者)

  • microservicecloud-consumer-dept-80(微服务服务消费者)


2-搭建步骤

2.1-父工程 microservicecloud

a、新建父工程 microservicecloud
在这里插入图片描述

<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>1.0-SNAPSHOT</version>

在这里插入图片描述
b、删除父工程 microservicecloud 中无用的 src 文件夹
在这里插入图片描述
c、修改父工程 microservicecloud 中 pom.xml

<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.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.0.4</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.31</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

切记 packaging 是 pom 模式:定义 pom 类型,将后续各个子模块公共的 jar 包等统一提取出来,类似抽象父类 pom 文件。
在这里插入图片描述

2.2-公共模块 microservicecloud-api

a、右击父工程 microservicecloud,新建子模块 microservicecloud-api
在这里插入图片描述
在这里插入图片描述
b、查看父工程 microservicecloud 中 pom.xml 变化
在这里插入图片描述
c、修改子模块 microservicecloud-api 中 pom.xml

<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.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservicecloud-api</artifactId>

    <!-- 当前 Module 所需 jar 包,按需求添加,若父类已包含可不写版本号 -->
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

d、修改子模块 microservicecloud-api,目录 /src/main/java 中新建包 com.atguigu.springcloud.entities

e、修改子模块 microservicecloud-api,包 com.atguigu.springcloud.entities 中新建类 Dept.java

配合 Lombok 使用

package com.atguigu.springcloud.entities;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@NoArgsConstructor
@Accessors(chain = true)
@SuppressWarnings("serial")
public class Dept implements Serializable {// 必须序列化
    private Long deptno;// 主键
    private String dname;// 部门名称
    // 来自哪个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库
    private String db_source;

    public Dept(String dname) {
        super();
        this.dname = dname;
    }
}

f、子模块 microservicecloud-api,执行 Maven 命令 mvn clean install
在这里插入图片描述
执行 mvn clean install 命令打包到 Maven 本地仓库,给其它模块引用达到通用目的。即需要用到部门实体的话,不用每个工程都定义一份,直接引用本模块即可。

2.3-提供者服务 microservicecloud-provider-dept-8001

a、新建 jdbc:mysql://localhost:3306/cloudDB01 数据库

子模块 microservicecloud-provider-dept-8001 连接 jdbc:mysql://localhost:3306/cloudDB01 数据库。

DROP DATABASE IF EXISTS cloudDB01;
CREATE DATABASE cloudDB01 CHARACTER SET UTF8;

USE cloudDB01;
CREATE TABLE dept (
	deptno BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
	dname VARCHAR(60),
	db_source VARCHAR(60)
);
 
INSERT INTO dept(dname,db_source) VALUES('开发部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('人事部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('财务部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('市场部',DATABASE());
INSERT INTO dept(dname,db_source) VALUES('运维部',DATABASE());
 
SELECT * FROM dept;

b、右击父工程 microservicecloud,新建子模块 microservicecloud-provider-dept-8001
在这里插入图片描述
c、修改子模块 microservicecloud-provider-dept-8001 中 pom.xml

<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.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservicecloud-provider-dept-8001</artifactId>

    <dependencies>
        <!-- 引入自定义模块 microservicecloud-api 包,可使用 Dept 部门 Entity -->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 热部署:修改后立即生效 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

d、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/resources 中新建 application.yml

server:
  port: 8001

spring:
  application:
    name: microservicecloud-dept
  datasource:
    # 数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # MySQL 驱动包
    driver-class-name: org.gjt.mm.mysql.Driver
    # 数据库名称
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: 123456
    dbcp2:
      # 数据库连接池的最小维持连接数
      min-idle: 5
      # 初始化连接数
      initial-size: 5
      # 最大连接数
      max-total: 5
      # 等待连接获取的最大超时时间
      max-wait-millis: 200

mybatis:
  # Mybatis 配置文件路径
  config-location: classpath:mybatis/mybatis.cfg.xml
  # 所有 Entity 别名类所在包
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations:
    # Mapper 映射文件
    - classpath:mybatis/mapper/**/*.xml

e、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/resources 中新建目录 mybatis

f、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/resources/mybatis 中新建 mybatis.cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 二级缓存开启 -->
        <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration>

g、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/java 中新建包 com.atguigu.springcloud.dao

h、修改子模块 microservicecloud-provider-dept-8001,包 com.atguigu.springcloud.dao 中新建接口 DeptDao.java

package com.atguigu.springcloud.dao;

import com.atguigu.springcloud.entities.Dept;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface DeptDao {
    public boolean addDept(Dept dept);

    public Dept findById(Long id);

    public List<Dept> findAll();
}

i、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/resources/mybatis 中新建目录 mapper

j、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/resources/mybatis/mapper 中新建 DeptMapper.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.springcloud.dao.DeptDao">
    <select id="findById" resultType="Dept" parameterType="Long">
        select deptno, dname, db_source from dept where deptno=#{deptno};
    </select>

    <select id="findAll" resultType="Dept">
        select deptno, dname, db_source from dept;
    </select>

    <insert id="addDept" parameterType="Dept">
        INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE());
    </insert>
</mapper>

k、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/java 中新建包 com.atguigu.springcloud.service

l、修改子模块 microservicecloud-provider-dept-8001,包 com.atguigu.springcloud.service 中新建接口 DeptService.java

package com.atguigu.springcloud.service;

import com.atguigu.springcloud.entities.Dept;

import java.util.List;

public interface DeptService {
    public boolean add(Dept dept);

    public Dept get(Long id);

    public List<Dept> list();
}

m、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/java 中新建包 com.atguigu.springcloud.service.impl

n、修改子模块 microservicecloud-provider-dept-8001,包 com.atguigu.springcloud.service.impl 中新建实现类 DeptServiceImpl.java

package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.DeptDao;
import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao dao;

    @Override
    public boolean add(Dept dept) {
        return dao.addDept(dept);
    }

    @Override
    public Dept get(Long id) {
        return dao.findById(id);
    }

    @Override
    public List<Dept> list() {
        return dao.findAll();
    }
}

o、修改子模块 microservicecloud-provider-dept-8001,目录 /src/main/java 中新建包 com.atguigu.springcloud.controller

p、修改子模块 microservicecloud-provider-dept-8001,包 com.atguigu.springcloud.controller 中新建类 DeptController.java

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService service;

    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return service.add(dept);
    }

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id) {
        return service.get(id);
    }

    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return service.list();
    }
}

q、修改子模块 microservicecloud-provider-dept-8001,包 com.atguigu.springcloud 中新建启动类 DeptProvider8001_App.java

package com.atguigu.springcloud;

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

@SpringBootApplication
public class DeptProvider8001_App {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider8001_App.class, args);
    }
}

r、测试子模块 microservicecloud-provider-dept-8001

2.4-消费者服务 microservicecloud-consumer-dept-80

a、右击父工程 microservicecloud,新建子模块 microservicecloud-consumer-dept-80
在这里插入图片描述
b、修改子模块 microservicecloud-consumer-dept-80 中 pom.xml

<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.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservicecloud-consumer-dept-80</artifactId>

    <dependencies>
        <!-- 引入自定义模块 -->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 热部署:修改后立即生效 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

c、修改子模块 microservicecloud-consumer-dept-80,目录 /src/main/resources 中新建 application.yml

server:
  port: 80

d、修改子模块 microservicecloud-consumer-dept-80,目录 /src/main/java 中新建包 com.atguigu.springcloud.cfgbeans

e、修改子模块 microservicecloud-consumer-dept-80,包 com.atguigu.springcloud.cfgbeans 中新建类 ConfigBean.java

类似 Spring 里面 applicationContext.xml 写的注入 Bean。

package com.atguigu.springcloud.cfgbeans;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean {
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

RestTemplate 是什么?

RestTemplate 提供了多种便捷访问远程 Http 服务的方法,是一种简单便捷的访问 Restful 服务模板类,是 Spring 提供的用于访问 Rest 服务的客户端模板工具集

RestTemplate 官网及使用

f、修改子模块 microservicecloud-consumer-dept-80,目录 /src/main/java 中新建包 com.atguigu.springcloud.controller

g、修改子模块 microservicecloud-consumer-dept-80,包 com.atguigu.springcloud.controller 中新建类 DeptController_Consumer.java

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptController_Consumer {
    private static final String REST_URL_PREFIX = "http://localhost:8001";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer/dept/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    @RequestMapping(value = "/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/consumer/dept/list")
    public List<Dept> list() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }
}

h、修改子模块 microservicecloud-consumer-dept-80,包 com.atguigu.springcloud 中新建启动类 DeptConsumer80_App.java

package com.atguigu.springcloud;

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

@SpringBootApplication
public class DeptConsumer80_App {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer80_App.class, args);
    }
}

i、测试子模块 microservicecloud-consumer-dept-80


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页