端午小休,想写一个springcloud工程完整的demo流程,顺便复习下各种组件的配置。此博文适用于和我一样刚接触微服务框架,想自己动手搞一套完整的Demo程序的小白,本人保证所有流程全部经过测试,源码已上传github,需要的朋友自取。
版本信息:
SpringCloud:Finchley.RELEASE
Springboot:2.1.2.RELEASE
IDEA:IntelliJ IDEA 2018.1.6 x64
MySql:5.7
---------------------------------------------------------------------------------------------------------------------------
1.创建父工程-myspringcloud
1.1 创建maven工程-myspringcloud
1.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"><modelVersion>4.0.0</modelVersion><groupId>com.en.iot</groupId><artifactId>myspringcloud</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><properties><jdk.version>1.8</jdk.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.1.2.RELEASE</version><type>pom</type><scope>import</scope></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></dependencies></dependencyManagement><build><finalName>myspringcloud</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${jdk.version}</source><target>${jdk.version}</target></configuration></plugin></plugins></build></project>
2.创建实体模块 myspringcloud-api
2.1 创建实体模块 myspringcloud-api,用于存放项目中其他模块的实体对象
2.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>myspringcloud</artifactId><groupId>com.en</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><version>1.0.0</version><artifactId>myspringcloud-api</artifactId><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version></dependency></dependencies></project>
2.3 创建com.en.po路径,创建Product.java实体对象,用于服务提供者提供数据库数据
package com.en.po;import lombok.Data;
import java.io.Serializable;@Data
public class ProductPo implements Serializable {private Long productId;private String productName;private String productDesc;}
3.创建服务生产者 myspringcloud-provider-product
3.1 新建服务生产者数据库
CREATE TABLE `product` (`productId` int(11) NOT NULL AUTO_INCREMENT,`productName` VARCHAR(50) NOT NULL,`productDesc` VARCHAR(50) NOT NULL,PRIMARY KEY(`productId`)
);INSERT INTO product(productName,productDesc) VALUES ('java编程',DATABASE());
INSERT INTO product(productName,productDesc) VALUES ('C',DATABASE());
INSERT INTO product(productName,productDesc) VALUES ('C++',DATABASE());
INSERT INTO product(productName,productDesc) VALUES ('Springboot',DATABASE());
INSERT INTO product(productName,productDesc) VALUES ('springcloud',DATABASE());
3.2 创建服务生产者 myspringcloud-provider-product模块
3.3 导入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>myspringcloud</artifactId><groupId>com.en</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><version>1.0.0</version><artifactId>myspringcloud-provider-product</artifactId><dependencies><dependency><groupId>com.en</groupId><artifactId>myspringcloud-api</artifactId><version>1.0.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</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-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies></project>
3.4 配置文件
server:port: 8081
mybatis:mapper-locations: - classpath:mapping/*.xml
spring:datasource:type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=GMT%2B8 username: rootpassword: root
logging:level:com.en.mapper: debug
3.4 因为要查数据库,所以新建Controller、Service、Mapper、xml、application.yml和启动类。
ProductController
package com.en.controller;import com.en.po.ProductPo;
import com.en.service.IProductService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping("/prodcut")
public class ProductController {@Resourceprivate IProductService iProductService;@RequestMapping(value="/getInfoById/{id}")public Object get(@PathVariable("id") long id) {return this.iProductService.getInfoById(id) ;}@RequestMapping(value="/addInfo")public Object add(@RequestBody ProductPo productPo) {return this.iProductService.addInfo(productPo) ;}@RequestMapping(value="/getAllInfo")public Object list() {return this.iProductService.getAllInfo() ;}}
IProductService
package com.en.service;import com.en.po.ProductPo;import java.util.List;public interface IProductService {/*** @Description 根据ID获取信息* @Param [id]* @return com.en.po.ProductPo* @throws**/ProductPo getInfoById(long id);/*** @Description 添加数据* @Param [productPo]* @return boolean* @throws**/boolean addInfo(ProductPo productPo);/*** @Description 查询数据库所有信息* @Param []* @return java.util.List<com.en.po.ProductPo>* @throws**/List<ProductPo> getAllInfo();
}
ProductServiceImpl
package com.en.service.impl;import com.en.mapper.ProductMapper;
import com.en.po.ProductPo;
import com.en.service.IProductService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;@Service
public class ProductServiceImpl implements IProductService {@Resourceprivate ProductMapper productMapper;@Overridepublic ProductPo getInfoById(long id) {return productMapper.getInfoById(id);}@Overridepublic boolean addInfo(ProductPo productPo) {return productMapper.addInfo(productPo);}@Overridepublic List<ProductPo> getAllInfo() {return productMapper.getAllInfo();}
}
ProductMapper
package com.en.mapper;import com.en.po.ProductPo;
import java.util.List;public interface ProductMapper {/*** @Description 添加数据* @Param [productPo]* @return boolean* @throws**/boolean addInfo(ProductPo product);/*** @Description 根据ID获取信息* @Param [id]* @return com.en.po.ProductPo* @throws**/ProductPo getInfoById(Long id);/*** @Description 查询数据库所有信息* @Param []* @return java.util.List<com.en.po.ProductPo>* @throws**/List<ProductPo> getAllInfo();
}
ProductMapper.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.en.mapper.ProductMapper"><select id="getInfoById" resultType="com.en.po.ProductPo" parameterType="long">selectproductId,productName,productDescfromproductWHERE productId = #{id};</select><select id="getAllInfo" resultType="com.en.po.ProductPo">SELECTproductId,productName,productDescfromproduct;</select><insert id="addInfo" parameterType="com.en.po.ProductPo">INSERT INTOproduct(productName,productDesc)VALUES(#{productName},database());</insert></mapper>
ProductApplication
package com.en;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @ClassName ProductApplication* @Description 启动类* @Author liuxiaobai* @Date 2020年6月25日11:21:02* @Version 1.0**/
@SpringBootApplication
@MapperScan("com.en.mapper")
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class,args);}
}
生产者服务代码结构
服务启动确保前台可以调用成功
4.创建服务消费者 myspringcloud-consumer
4.1 导入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>myspringcloud</artifactId><groupId>com.en</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>microcloud-consumer</artifactId><dependencies><dependency><groupId>com.en</groupId><artifactId>myspringcloud-api</artifactId><version>1.0.0</version></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><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies></project>
4.2 创建com.en.config路径,创建RestConfig.java文件,提供接口调用模板
package com.en.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestConfig {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}}
4.3 创建com.en.controller路径,创建ConsumerProductController.java文件,提供前端调用接口
package com.en.controller;import com.en.po.ProductPo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;@RestController
@RequestMapping("/consumer")
public class ConsumerProductController {public static final String PRODUCT_GET_URL = "http://localhost:8081/prodcut/getInfoById/";public static final String PRODUCT_LIST_URL="http://localhost:8081/prodcut/getAllInfo/";public static final String PRODUCT_ADD_URL = "http://localhost:8081/prodcut/addInfo/";@Resourceprivate RestTemplate restTemplate;@RequestMapping("/product/getInfoById")public Object getProduct(long id) {return restTemplate.getForObject(PRODUCT_GET_URL + id, ProductPo.class);}@RequestMapping("/product/getAllInfo")public Object listProduct() {return restTemplate.getForObject(PRODUCT_LIST_URL, List.class);}@RequestMapping("/product/addInfo")public Object addPorduct(ProductPo productPo) {return restTemplate.postForObject(PRODUCT_ADD_URL, productPo, Boolean.class);}
}
4.4 创建启动类ConsumerApplication.java
package com.en;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class,args);}
}
4.5 创建启动类 application.yml
server:port: 80
4.6 先启服务生产者,再启消费者测试接口
源码自取:
github地址:https://github.com/L1021204735/myspringcloud1