1. 工程结构
MicroServiceCloud
|— microservicecloud-api # 封装整体entity/接口/公共配置等
|— microservicecloud-provider-dept-8001 # 微服务落地的服务提供者
|— microservicecloud-consumer-dept-80 # 微服务调用的客户端
2. Spring Cloud和Spring Boot版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <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>
|
3. MicroServiceCloud父工程
3.1 新建工程, packageing选择pom
3.2 pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hellodev</groupId> <artifactId>atguigu-microservicecloud</artifactId> <version>0.0.1</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> <project.version>0.0.1</project.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.1.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</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>
|
4. microservicecloud-api
4.1 创建Module
4.2 父工程增加模块信息
1 2 3
| <modules> <module>microservicecloud-api</module> </modules>
|
4.3 pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hellodev</groupId> <artifactId>atguigu-microservicecloud</artifactId> <version>0.0.1</version> </parent> <artifactId>microservicecloud-api</artifactId> <version>0.0.1</version> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
|
4.4 新建entities包并创建Dept部门类
1 2 3 4 5 6 7 8 9 10 11 12 13
| @SuppressWarnings("serial") @NoArgsConstructor @Data @Accessors(chain = true) public class Dept implements Serializable { private Long deptno; private String dname; private String db_source; public Dept(String dname) { super(); this.dname = dname; } }
|
4.5 maven编译
1 2
| mvn clean install 模块编译后可供其他工程使用,达到通用目的.
|
5. microservicecloud-provider-dept-8001
5.1 创建Module
5.2 父工程增加模块信息
1 2 3
| <modules> <module>microservicecloud-provider-dept-8001</module> </modules>
|
5.3 pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hellodev</groupId> <artifactId>atguigu-microservicecloud</artifactId> <version>0.0.1</version> </parent> <artifactId>microservicecloud-provider-dept-8001</artifactId> <version>0.0.1</version> <dependencies> <dependency> <groupId>com.hellodev</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>
|
5.4 yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.hellodev.api.entities mapper-locations: - classpath:mybatis/mapper/**/*.xml spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB01 username: root password: 1234 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200
|
5.5 整合mybatis—mybatis.cfg.xml
在src/main/resources目录下新建mybatis文件夹,然后创建mybatis.cfg.xml
1 2 3 4 5 6 7 8
| <?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>
|
5.6 数据库初始化脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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;
|
5.7 创建Mapper.xml
在src/main/resources目录下新建mapper文件夹,然后创建DeptMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?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.hellodev.dao.DeptDao"> <select id="findById" resultType="com.hellodev.entities.Dept" parameterType="Long"> select deptno , dname ,db_source from dept where deptno =#{ deptno }; </select> <select id="findAll" resultType="com.hellodev.entities.Dept"> select deptno , dname ,db_source from dept ; </select> <insert id="addDept" parameterType="com.hellodev.entities.Dept"> INSERT INTO dept ( dname ,db_source) VALUES(#{ dname }, DATABASE() ); </insert> </mapper>
|
5.8 编写Dao
创建dao包,并创建DeptDao.java
1 2 3 4 5 6
| @Mapper public interface DeptDao { public boolean addDept(Dept dept); public Dept findById(Long id); public List<Dept> findAll(); }
|
5.9 编写Service及实现类
创建service包,并创建DeptService.java和DeptServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public interface DeptService { public boolean add(Dept dept); public Dept get(Long id); public List<Dept> 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(); } }
|
5.10 编写Controller
创建controller包,并创建DeptController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @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(); } }
|
5.11 验证
启动工程,并使用curl命令验证
1 2 3 4 5 6
| curl http://localhost:8001/dept/get/2 {"deptno":2,"dname":"人事部","db_source":"cloudDB01”} curl http://localhost:8001/dept/list [{"deptno":1,"dname":"开发部","db_source":"cloudDB01"},{"deptno":2,"dname":"人事部","db_source":"cloudDB01"}, {"deptno":3,"dname":"财务部","db_source":"cloudDB01"},{"deptno":4,"dname":"市场部","db_source":"cloudDB01"}, {"deptno":5,"dname":"运维部","db_source":"cloudDB01"}]
|
6. microservicecloud-consumer-dept-80
6.1 创建Module
6.2 父工程增加模块信息
1 2 3
| <modules> <module>microservicecloud-consumer-dept-80</module> </modules>
|
6.3 pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?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> <parent> <groupId>com.hellodev</groupId> <artifactId>atguigu-microservicecloud</artifactId> <version>0.0.1</version> </parent> <artifactId>microservicecloud-consumer-dept-80</artifactId> <dependencies> <dependency> <groupId>com.hellodev</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>
|
6.4 yml文件
6.5 配置RestTemplate
RestTemplate : RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
(https://docs.spring.io/spring-framework/docs/4.3.7.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html)
创建cfgbean包,并创建ConfigBean.java
1 2 3 4 5 6 7
| @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
|
6.6 使用RestTemplate
创建controller包,并创建DeptController_Consumer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @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.7 验证
分别启动microservicecloud-provider-dept-8081和microservicecloud-consumer-dept-80
1 2 3 4 5 6
| curl http://localhost/consumer/dept/get/2 {"deptno":2,"dname":"人事部","db_source":"cloudDB01”} curl http://localhost/consumer/dept/list [{"deptno":1,"dname":"开发部","db_source":"cloudDB01"},{"deptno":2,"dname":"人事部","db_source":"cloudDB01"}, {"deptno":3,"dname":"财务部","db_source":"cloudDB01"},{"deptno":4,"dname":"市场部","db_source":"cloudDB01"}, {"deptno":5,"dname":"运维部","db_source":"cloudDB01"}]
|
两个接口返回内容与上一个工程一致,这表明我们工程正确。
资源获取
公众号回复 : Rest微服务构建案例 获取本节代码
公众号回复 : SpringCloud思维导图
Tips
欢迎收藏和转发,感谢你的支持!(๑•̀ㅂ•́)و✧
欢迎关注我:后端小哥,专注后端开发,希望和你一起进步!