
@TOC
写在前面
由于刚开始创建第一个项目的时候,出现了版本冲突问题,所以最后没有启动下来,但是我大部分的图片都是用的第一次的,所以大家可以主观的把图片中父项目SpringCloud看成springcloud-demo2,因为持续的时间不算短,涉及的东西又比较多,如果有什么纰漏,希望大家可以在评论区指出来,如果您觉得本篇博客,对您有所帮助,也欢迎您点赞收藏!
一、新建一个项目
使用idea新建一个Maven项目

填写两处的信息,完成创建

src在父项目中没有发挥作用,可以删掉

二、服务提供者
1、编写父项目
pom.xml文件配置,主要有spring-cloud的配置、springboot的启动依赖,mysql的依赖、数据源、Lombok、日志、junit、log4j的依赖。其中对于易改变版本的依赖,可以进行统一的版本管理,放到properties里面,这样可以清晰方便的修改版本号。
注意这是自己项目的名字

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.you</groupId>
<artifactId>springcloud-demo2</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloud-api</module>
<module>springcloud-provider-dept-8001</module>
</modules>
<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-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springCloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--SpringBoot 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</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>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、编写子项目
新建一个module
右键-新建一个module

填写module名,点击Finish,完成创建

创建完成,可以看到父工程是springcloud-demo2。

但是这时候我们看到maven里并无依赖,原因是因为** 是一个项目管理器,只有子项目使用的时候,才会被导入进来**

当我们在子项目中,使用依赖时,这时候可以看到子项目的maven里有了依赖,这也印证了父工程只是一个依赖管理,并不引入依赖。

3、连接Mysql
连接mysql时,出现“Server returns invalid timezone. Need to set 'serverTimezone' property.”错误

将UTC修改为GMT,点击Apply。不要点ok

重新检测连接,如下证明数据库连接成功,但是这时候还没有选择自己创建的数据库

点击Schemas,选择自己要使用的数据库(下面写成表了,其实是数据库),这时候可以点击ok了。

这时候可以在Database里面看到自己选择的数据库,如果该数据库里面存在表,那么你也可以看到自己创建的表。

使用idea创建数据库表
1、找到数据库,右键-new-table

2、填写表名、新建字段(字段名、属性)

3、填写完成之后,点击Execute,就可以看到自己新创建的表了

4、插入数据,其中DataBase()指的是数据库的来源

5、添加数据成功

4、编写实体类
实体类与数据库中的表对应,链表关系映射

(上面写的是dept,为了好看一些,改成了大写的Dept)
Dept.java
package com.you.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Dept implements Serializable {
private Long deptno;
private String deptname;
private String db_source;
/*Serializable 支持链式写法
* Dept.setDeptno(1).setDeptName("开发部");
* */
}
5、新建一个子项目
再新建一个module

过程如2编写子项目一样,创建完后如下

配置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">
<parent>
<artifactId>springcloud-demo2</artifactId>
<groupId>com.you</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-provider-dept-8001</artifactId>
<dependencies>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Spring web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.3</version>
</dependency>
<!--spring boot 启动-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.isis.viewer</groupId>
<artifactId>junit</artifactId>
<version>0.2.0-incubating</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.you</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
创建yaml文件
在springcloud-provider-dept-8001 resources

右键-new-file 输入application.yaml或者application.yml

配置application.yaml
#端口号
server:
port: 8001
#配置数据库
spring:
datasource:
url: jdbc:mysql:///dp80?serverTimezone=UTC
username: root
password: 你的数据库密码
driver-class-name: com.mysql.jdbc.Driver

编写Mapper
在you文件下,新建一个mapper文件夹,里面创建一个DeptMapper接口文件

DeptMapper.interface
package com.you.mapper;
import com.you.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface DeptMapper {
@Select("select * from Dept where deptno = #{id}")
public Dept finaDeptOfId(Long id);
}
编写Controller
在you文件夹下,新建一个Controller文件夹,里面创建DeptController.java文件

DeptController.java
package com.you.Controller;
import com.you.mapper.DeptMapper;
import com.you.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@ResponseBody
@Component
public class DeptController {
@Autowired
DeptMapper deptMapper;
@GetMapping("/dept/aDept/{id}")
public Dept getDeptOfId(@PathVariable("id") Long id)
{
return deptMapper.finaDeptOfId(id);
}
}
创建SpringBoot启动类
在you文件夹下,创建DeptApplication.java文件,注意是you文件夹下,也就是DeptApplication.java和Mapper文件夹和Controller文件夹平级

6、启动
在启动类中点击启动按钮

打开浏览器,输入http://localhost:8001/dept/aDept/1 回车

获得数据成功

三、服务消费者
新建一个module

取名springcloud-comsumer-dept-80

配置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">
<parent>
<artifactId>springcloud-demo2</artifactId>
<groupId>com.you</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-comsumer-dept-80</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.you</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
创建application.yaml文件
取名applicatin.yml(和上面创建yaml一样)

配置端口号为801(80的被占用了)

创建控制类
在java包里创建文件夹com/you/controller 在controller文件夹里创建DeptComsumerController.java

创建完成

注册RestTemplate
在you文件夹下创建config文件夹,在config文件夹中创建ConfigBean.java

写入以下代码,将RestTemplate注入
package com.you.config;
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();
}
}
编写控制类
写入以下代码
package com.you.controller;
import com.you.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@ResponseBody
public class DeptComsumerController {
@Autowired
RestTemplate restTemplate;
public static final String REST_URL_PREFIX = "http://localhost:8001";
@GetMapping("/consumer/dept/getDept/{id}")
public Dept getDeptOfId(@PathVariable("id") Long id)
{
System.out.println(REST_URL_PREFIX+"/dept"+"/aDept/"+id);
return restTemplate.getForObject(REST_URL_PREFIX + "/dept" + "/aDept/"+id, Dept.class);
}
}
编写启动类
在you文件夹下编写启动类,DeptConsumer_80.java,代码如下
package com.you;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}
四、提供端和消费端同启动
1、启动提供端

2、启动服务端

启动成功

3、提供端自身获取
网址栏输入http://localhost:8001/dept/aDept/1

回车,可以拿到数据

4、服务端获取数据
地址栏输入http://localhost:801/consumer/dept/getDept/1回车,拿到数据

