SpringCloud学习之基础环境搭建
作者:互联网
前提:SpringBoot可以离开SpringCloud独立使用开发项目,但是SpringCloud离不开SpringBoot,属于依赖的关系.
所以此处搭建一个基础的SpringBoot 多模块的项目
一: 创建父模块
1.创建一个maven项目,填写groupid和artifactId,并修改packing为pom
2. 添加相应的依赖
<?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.ligong.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>microservicecloud-api</module> <module>microservicecloud-provider</module> </modules> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.compiler.source>1.8</project.compiler.source> <project.compiler.target>1.8</project.compiler.target> <junit.verion>4.12</junit.verion> <log4j.version>1.2.17</log4j.version> <lombok.version>1.6.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>2.1.3.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.15</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.3</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.verion}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
二: 创建子模块
1.新建microservicecloud-api子模块
在父工程下右键new->Module 创建一个子模块,这里选择maven/Spring initializr都可以
此处我采用的是maven创建,填写artifactId finish即可
microservicecloud-api的pom文件,注意,打包方式为jar
<?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>microservicecloud</artifactId> <groupId>com.ligong.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>microservicecloud-api</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> </dependencies> </project>
在api子模块下新建一个entities包,创建一个dept class。
此处采用jpa作为持久层,所以api的pom文件中需要导入相关jpa的依赖
@Data @Entity @Table(name = "t_dept") @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(value={"hibernateLazyInitializer","handler", "fieldHandler"}) @Accessors(chain = true) public class Dept implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer deptNo; @Column(name = "d_name") private String deptName; /** * 来源于哪个数据库 */ @Column(name = "db_source") private String dbSource; }
2.创建provider子模块,用于对外提供服务。
创建过程与上面类似,就不在赘述
provider的pom文件,注意打包方式为war
<?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>microservicecloud</artifactId> <groupId>com.ligong.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <artifactId>microservicecloud-provider</artifactId> <dependencies> <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> <version>1.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>com.ligong.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>1.0-SNAPSHOT</version> <!--<scope>compile</scope>--> </dependency> </dependencies> </project>
1. 创建DeptRepository
@Repository public interface DeptRepository extends JpaRepository<Dept,Integer> { }
2. 创建DeptService
public interface DeptService { boolean add(Dept dept); Dept get(Integer id); List<Dept> list(); }
3. 创建DeptServiceImpl
@Service @Slf4j public class DeptServiceImpl implements DeptService { @Autowired private DeptRepository deptRepository; public Dept get(Integer id){ return deptRepository.getOne(id); } public boolean add(Dept dept){ try{ deptRepository.save(dept); }catch (Exception e){ log.error("添加dept 失败!"+e.getMessage()); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return false; } return true; } public List<Dept> list(){ return deptRepository.findAll(); } }
4. 创建DeptController,(定义rest风格的请求方式)
ps:
@RestController public class DeptController { @Autowired private DeptService deptService; @PostMapping("/dept") public boolean add(@RequestBody Dept dept){ return deptService.add(dept); } @GetMapping("/dept/{id}") public Dept get(@PathVariable("id") Integer id){ return deptService.get(id); } @GetMapping("/dept/list") public List<Dept> list(){ return deptService.list(); } }
5. 定义SpringBoot启动类(如果采用Spring initializr创建的子module可以跳过)
@SpringBootApplication public class DeptProvider8080App { public static void main(String[] args) { SpringApplication.run(DeptProvider8080App.class,args); } }
6. 编写application.yml文件
server: port: 8080 #Server port spring: application: name: microservercloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # alibaba 数据源 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动 url: jdbc:mysql://xx.xx.xx.xx:3306/cloudDB01 # jdbc url username: root password: root dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 jpa: hibernate: ddl-auto: update # create/update table properties: hibernate: format_sql: true #格式化sql enable_lazy_load_no_trans: true dislect: org.hibernate.dialect.MySQL5Dialect #定义mysql方言 open-in-view: true show-sql: true #显示sql
三: 测试环境搭建
运行SpringBoot启动类,查看启动状态
采用postman测试
测试/dept 添加接口,body添加
测试/dept/{id}接口
最后/dept/list查看数据是否添加成功
至此,SpringCloud的基础环境搭建完毕!
标签:SpringCloud,boot,学习,dept,microservicecloud,spring,org,public,搭建 来源: https://www.cnblogs.com/zhanyifan/p/10693869.html