Springcloud
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
父项目建立父项目建立
我们开发项目,现在基本都用到maven,以及用父子项目,以及公共模块依赖,来构建方便扩展的项目体系;
首先我们建立父项目 microservice ,主要是一个pom,管理module,以及管理依赖,规范所有jar包版本等;
新建maven项目
我们Springcloud版本 用 Edgware.SR4
Springboot版本 用 1.5.13.RELEASE
修改pom.xml
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 39 |
<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.java1234.springcloud</groupId> <artifactId>microservice</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> <druid.version>1.1.10</druid.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.13.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> </dependencies> </dependencyManagement> </project> |
这里引入了 springcloud 以及springboot 包括 druid连接池 以及 属性 规范编译 目标版本,包括连接池版本 编码等信息;
公共模块项目搭建
我们新建module公共模块项目 microservice-common,主要是放一些其他项目公用的东西,比如实体类,工具类等等;
新建module,
勾选 create a simple project 取下module 名称 mircoservice-common
pom.xml 加下 jpa和 mysql驱动包依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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.java1234.springcloud</groupId> <artifactId>microservice</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>mircoservice-common</artifactId>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project> |
我们新建Student实体
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 |
package com.java1234.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * 学生信息实体 * @author Administrator * */ @Entity @Table(name="t_student") public class Student implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue private Integer id; // 编号
@Column(length=50) private String name; // 姓名
@Column(length=50) private String grade; // 年级
public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; }
} |
包括映射,当然我们数据库里也提前建一个db_springcloud数据库;
microservice-student-provider-1001服务提供者项目建立
microservice-student-provider-1001服务提供者项目建立
我们新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 microservice-student-provider-1001
pom.xml改成:
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 |
<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.java1234.springcloud</groupId> <artifactId>microservice</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservice-student-provider-1001</artifactId>
<dependencies> <dependency> <groupId>com.java1234.springcloud</groupId> <artifactId>microservice-common</artifactId> <version>${project.version}</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-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</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> |
加common项目依赖,以及 springboot的 web jpa 驱动包 tomcat 连接池 配置 包括自动热部署配置。
application.yml配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
server: port: 1001 context-path: / # 数据源配置 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db_springcloud username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true thymeleaf: cache: false |
StudentRepository接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.java1234.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import com.java1234.entity.Student; /** * 学生Repository接口 * @author Administrator * */ public interface StudentRepository extends JpaRepository<Student, Integer>,JpaSpecificationExecutor<Student>{ } |
StudentService接口:
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 |
package com.java1234.service; import java.util.List; import com.java1234.entity.Student; /** * 学生信息Service接口 * @author Administrator * */ public interface StudentService { /** * 添加或者修改学生信息 * @param student */ public void save(Student student);
/** * 根据id查找学生信息 * @param id * @return */ public Student findById(Integer id);
/** * 查询学生信息 * @return */ public List<Student> list();
/** * 根据id删除学生信息 * @param id */ public void delete(Integer id);
} |
StudentServiceImpl实现类:
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 |
package com.java1234.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.java1234.entity.Student; import com.java1234.repository.StudentRepository; import com.java1234.service.StudentService; /** * 学生信息Service实现类 * @author Administrator * */ @Service("studentService") public class StudentServiceImpl implements StudentService{ @Resource private StudentRepository studentRepository;
@Override public void save(Student student) { studentRepository.save(student); } @Override public Student findById(Integer id) { return studentRepository.findOne(id); } @Override public List<Student> list() { return studentRepository.findAll(); } @Override public void delete(Integer id) { studentRepository.delete(id); } } |
StudentProviderController类:
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 68 69 70 71 72 73 74 |
package com.java1234.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.java1234.entity.Student; import com.java1234.service.StudentService; /** * 服务提供者-学生信息控制器 * @author Administrator * */ @RestController @RequestMapping("/student") public class StudentProviderController { @Resource private StudentService studentService;
/** * 添加或者修改学生信息 * @param student * @return */ @PostMapping(value="/save") public boolean save(Student student){ try{ studentService.save(student); return true; }catch(Exception e){ return false; } }
/** * 查询学生信息 * @return */ @GetMapping(value="/list") public List<Student> list(){ return studentService.list(); }
/** * 根据id查询学生信息 * @return */ @GetMapping(value="/get/{id}") public Student get(@PathVariable("id") Integer id){ return studentService.findById(id); }
/** * 根据id删除学生信息 * @return */ @GetMapping(value="/delete/{id}") public boolean delete(@PathVariable("id") Integer id){ try{ studentService.delete(id); return true; }catch(Exception e){ return false; } } } |
启动类StudentProviderApplication_1001:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.java1234; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StudentProviderApplication_1001 { public static void main(String[] args) { SpringApplication.run(StudentProviderApplication_1001.class, args); } } |
我们运行启动类,自动生成t_student表;
我们加点数据,方便测试:
浏览器请求:http://localhost:1001/student/list
显示结果:
请求:http://localhost:1001/student/get/1
显示:
然后添加修改的话 我们借用下 postman插件;
具体插件包和文件,这里给下地址:谷歌浏览器postman插件及安装方法 下载_Java知识分享网-免费Java资源下载
具体postman测试演示,大伙可以结合我的视频,有详细的操作;
添加测试:
修改测试;
microservice-student-consumer-80服务消费者项目建立
我们新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 microservice-student-consumer-1001
pom.xml修改:
<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.java1234.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-consumer-80</artifactId>
<dependencies>
<dependency>
<groupId>com.java1234.springcloud</groupId>
<artifactId>microservice-common</artifactId>
<version>${project.version}</version</