💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 1. 构建 Project 父工程 ### **1.1 创建一个空工程(microservice-cloud)** 1. 新建项目 ![](https://img.kancloud.cn/50/56/5056a94128f354c97b9a6c247d2dc5c5_983x461.png) 2. 选择空项目 ![](https://img.kancloud.cn/05/9b/059b83485e156456be0ceb8ea94e4247_976x545.png) 3. 点击finish ![](https://img.kancloud.cn/3d/5b/3d5b723202f0ed5b3f36a82ca6071aeb_734x605.png) ### **1.2 创建父工程** 1. 在空项目下创建父工程模块 ![](https://img.kancloud.cn/f5/16/f516012c370236c8d3575cb8102f79cb_732x306.png) 2. 选择maven项目 ![](https://img.kancloud.cn/49/57/4957eaacc51fde2dc723eb6420b48d2e_734x598.png) ![](https://img.kancloud.cn/19/88/198835ce951a55f2366893ed119b05f8_521x260.png) 3. 删除 src 目录及子目录,在pom.xml 中手动指定为 pom 类型(maven聚合), 如下所示: ![](https://img.kancloud.cn/a6/bc/a6bcd49d77c9d82e1e6263cd38333b85_893x429.png)、 4. 引入相关依赖 ~~~ <!-- spring boot 采用 2.0.7 版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> <relativePath/> </parent> <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> <!-- spring cloud 采用 Finchley.SR2 版本 --> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <!--依赖声明--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <!--maven不支持多继承,使用import来依赖管理配置--> <scope>import</scope> </dependency> <!--导入 mybatis 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> ~~~ ### 1.3 创建公共模块 1. 在父模块下创建一个公共模块(maven项目) ![](https://img.kancloud.cn/b3/b3/b3b3884e7ab4c910d63f48139ab0e770_709x521.png) 2 . 选择maven项目 ![](https://img.kancloud.cn/14/02/1402d4f1bd9a8ff8fb036fc12d099af4_734x598.png) ![](https://img.kancloud.cn/f5/7f/f57f6481192f3dc97a5947accf043a49_740x585.png) 选择和父工程同级目录 ![](https://img.kancloud.cn/4a/74/4a7477279a924094f1a7bf9ee5f986ed_734x598.png) ![](https://img.kancloud.cn/aa/64/aa6485e41c6e675450ab09edb3f5525c_387x221.png) 3. 提供实体类用于测试 ~~~ package com.tuna.springcloud.common.entities; import java.io.Serializable; public class Product implements Serializable { private Long pid; //主键 private String productName; //产品名称 // 来自那个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库 private String dbSource; public Product() { } public Product(String productName) { this.productName = productName; } public Product(Long pid, String productName, String dbSource) { this.pid = pid; this.productName = productName; this.dbSource = dbSource; } ... ~~~ ### **1.4 构建服务提供者** 1. 同上创建一个服务提供者模块 `microservice-cloud-server` ![](https://img.kancloud.cn/08/06/0806a41b4ec90c19a5cc3d4145e488c6_383x265.png) 2. 修改pom ``` <?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"> <parent> <artifactId>microservice-cloud-01</artifactId> <groupId>com.mengxuegu.springcloud</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../microservice-cloud-01/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-cloud-03-provider-product-8001</artifactId> <dependencies> <dependency> <groupId>com.tuna.springcloud</groupId> <artifactId>microservice-common</artifactId> <version>${project.version}</version> </dependency> <!--springboot web启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </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> </dependencies> </project> ``` **3. 配置 application.yml 文件** ~~~ server: port: 8001 spring: application: name: microservice-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包 url: jdbc:mysql://192.168.56.10:3306/springcloud_db01?serverTimezone=GMT%2B8 # 数据库名称 username: springcloud password: tuna dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 150 # 等待连接获取的最大超时时间 mybatis: configuration: map-underscore-to-camel-case: true type-aliases-package: com.tuna.springcloud.common.entities ~~~ **4. 运行 MySQL 数据库脚本 ** ``` DROP DATABASE IF EXISTS springcloud\_db01; CREATE DATABASE springcloud\_db01 CHARACTER SET UTF8; USE springcloud\_db01; CREATE TABLE product ( pid BIGINT NOT NULL PRIMARY KEY AUTO\_INCREMENT, product\_name VARCHAR(50), db\_source VARCHAR(50) ); INSERT INTO product(product\_name,db\_source) VALUES('格力空调',DATABASE()); INSERT INTO product(product\_name,db\_source) VALUES('海尔冰箱',DATABASE()); INSERT INTO product(product\_name,db\_source) VALUES('小短裙',DATABASE()); INSERT INTO product(product\_name,db\_source) VALUES('羽绒服',DATABASE()); INSERT INTO product(product\_name,db\_source) VALUES('韩版休闲鞋',DATABASE()); INSERT INTO product(product\_name,db\_source) VALUES('高贵鞋',DATABASE()); SELECT \* FROM product; ``` 5. 创建mapper和xml ~~~ package com.tuna.springcloud.server.mapper; import com.tuna.springcloud.common.entities.Product; import java.util.List; public interface ProductMapper { Product findById(Long pid); List<Product> findAll(); boolean addProduct(Product product); } ~~~ 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.tuna.springcloud.server.mapper.ProductMapper"> <select id="findById" resultType="Product" parameterType="Long"> select pid, product_name, db_source from product where pid=#{pid}; </select> <select id="findAll" resultType="Product"> select pid, product_name, db_source from product; </select> <insert id="addProduct" parameterType="Product"> INSERT INTO product(product_name, db_source) VALUES(#{productName}, DATABASE()); </insert> </mapper> ~~~ ## 2. 消费者 消费者restTemplate发送http请求 ~~~ @RequestMapping(value = "/consumer/product/get/{id}") public Product get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX + "/product/get/" + id, Product.class); } ~~~ **现在还是 ip + port 去请求服务**,访问前端地址得到如下 ![](https://img.kancloud.cn/76/30/76301794971845a1bef7a87f04e75626_462x134.png)