SpringCloud准备--Rest微服务项目搭建
Rest微服务项目搭建:
以Dept部门模块做一个微服务通用案例:Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务
1.创建整体父工程
1.在做案例之前,先在eclipse/IDEA里面创建一个maven的整体父工程项目名为:microservicecloud
备注:在创建整体父工程的时候Packaging要选择pom而不是选择jar
2.将需要用到的mavenjar包配置好
<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.hhf.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-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> <build> <finalName>microservicecloud</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build> </project>
2.创建第一个子工程(公共子模块)
3.创建好父工程并且将相应的maven配置好以后,开始创建第一个子工程的项目。
1.在已经创建好的父工程下创建公共子模块工程名为:microservicecloud-api(maven Module)
备注:创建子工程时Packaging要选择jar的打包方式。
2.在子工程中添加mavenjar包
<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.hhf.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- 当前Module我自己叫什么名字 --> <artifactId>microservicecloud-api</artifactId> <!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 --> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> </project>
4.然后在microservicecloud-api这个子项目的src/main/java下创建一个aip公共模块名为Dept的类,并且在这个类里面添加以下代码。
备注:如果无参/有参注解设置了,但最终如果无效的话,那就请用老办法方式一个个方法搞出来吧!
//@AllArgsConstructor //全参构造注解 @Data //set设置值/get获取值注解 @NoArgsConstructor //无参数构造注解 @Accessors(chain=true) //链式访问 public class Dept { //主键 private Long deptno; //部门名称 private String dname; //来自那个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库。 private String db_source; public Dept(String dname) { super(); this.dname = dname; }
5.编写完以上代码以后,在microservicecloud-api这个子项目右键点击Run As里面列表下的maven clean和maven install,让我们的工程重新在本地库生成最新的jar包使其给其他模块引用,达到通用之目的。
3.创建第二个子工程(部门微服务提供者)
6.我们在父项目microservicecloud中再创建一个maven的module名为:microservicecloud-provider-dept-8001
1.这个microservicecloud-provider-dept-8001用来做部门服务提供者
2.在microservicecloud-provider-dept-8001这个项目的pom文件中加入以下jar包:
<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.hhf.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-provider-dept-8001</artifactId> <dependencies> <!-- 引入自己定义的api通用包,可以使用Dept部门Entity --> <dependency> <groupId>com.hhf.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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> <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>
6.然后在microservicecloud-provider-dept-8001这个项目的resources文件下创建一个application.yml文件
将如下代码添加到application.yml中
server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.hhf.springcloud.entities # 所有Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称 username: root password: 520hhf dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间
7.然后在resources文件夹里再创建一个mybatis文件夹,在mybatis文件夹里面创建一个mybatis.cfg.xml文件。
8.在mysql中创建部门数据库脚本
备注:得提前在mysql中创建好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());
9.然后在microservicecloud-provider-dept-8001这个项目下的dao包下创建一个dao接口名为:DeptDao
import com.hhf.springcloud.entities.Dept; import org.apache.ibatis.annotations.Mapper; import java.util.List; //整合mybatis @Mapper public interface DeptDao { public boolean addDept(Dept dept); public Dept findById(Long id); public List<Dept> findAll(); }
10.然后在从resources文件夹里再mybatis文件夹里面再创建一个mapper文件夹,并在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.hhf.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>
11.然后在service包下创建DeptService部门服务接口类
package com.hhf.springcloud.service;
import com.hhf.springcloud.entities.Dept;
import java.util.List;
public interface DeptService {
public boolean add(Dept dept);
public Dept get(Long id);
public List<Dept> list();
}
12.当然,有了创建DeptService部门服务接口,肯定也要有它的实现类,那么在service包下在创建一个包名为:impl,在这个impl包下创建DeptServiceimpl实现类
package com.hhf.springcloud.service.impl;
import com.hhf.springcloud.dao.DeptDao;
import com.hhf.springcloud.entities.Dept;
import com.hhf.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();
}
}
13.好,接下来我们在controller包下创建一个DeptController的类来实现页面数据显示
package com.hhf.springcloud.controller;
import com.hhf.springcloud.entities.Dept;
import com.hhf.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();
}
}
14.接下来我们进入一个测试阶段,看看能不能把数据显示到页面中
在主包下创建一个名为:DeptProvider8001_App的启动类进行测试
package com.hhf.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);
}
}
15.好,激动人心的时刻到来了!如果无误,那么我们从数据库读取的数据能够全部在页面中进行一个显示。
在浏览器输入:http://localhost:8001/dept/list 进行页面测试!
测试效果图:
4.创建第三个子工程(部门微服务消费者)
1.在整体父工程下再创建一个子工程,名为:microservicecloud-consumer-dept-80
2.在microservicecloud-consumer-dept-80子工程pom文件下添加jar包
<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.hhf.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-consumer-dept-80</artifactId>
<description>部门微服务消费者</description>
<dependencies>
<dependency>
<!-- 自己定义的api -->
<groupId>com.hhf.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>
3.然后在resources资源文件下创建一个application.yml文件,并添加如下内容:
server:
port: 80
4.然后在java包下创建一个com.hhf.springcloud.cfgbeans包,并在此包创建一个ConfigBean类
package com.hhf.springcloud.cfgbeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
/**
* RestTemplate提供了多种便捷访问远程Http服务的方法,
* 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集。
*/
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5.并在com.hhf.springcloud包下创建一个controller包,在controller包下创建一个名为:DeptController_Consumer的类
package com.hhf.springcloud.controller;
import com.hhf.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);
}
}
6.创建一个主启动类名为:DeptConsumer80_App
package com.hhf.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);
}
}
7.激动人心的时候又到啦!!!
我们先启动microservicecloud-provider-dept-8001这个子工程下的主启动类:DeptProvider8001_App。
8.启动了DeptProvider8001_App成功以后,我们再启动microservicecloud-consumer-dept-80这个子工程下的主启动类:DeptConsumer80_App
9.然后在浏览器中进行测试:
1).在浏览器输入:http://localhost/consumer/dept/get/2
2).在浏览器输入:http://localhost/consumer/dept/add?dname=AI
3).然后查看你自己的mysql数据库里面有没有添加到AI这个数据
4).然后我们最后来看看浏览器中会不会显示我们新添加的数据
在浏览器输入:http://localhost/consumer/dept/list
- 点赞
- 收藏
- 关注作者
评论(0)