1.是什么
springcloud是一系列框架的有序集合,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
2.有卵用
在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,
分布式有卵用:一口火锅一个人吃完要点时间,一桌子人一人一口就没了。一个应用一台服务器带不动,多来几台就好了。
3.怎么用
前置科技:maven,springboot,数据库
干货如下
消费者-生产者-仓储模型
仓储仓储是根本,仓储仓储是根本,仓储仓储是根本
1、生产者仅仅在仓储未满时候生产,仓满则停止生产。
2、消费者仅仅在仓储有产品时候才能消费,仓空则等待。
3、当消费者发现仓储没产品可消费时候会通知生产者生产。
4、生产者在生产出可消费产品时候,应该通知等待的消费者去消费。
项目基本结构
cloud://父项目
cloud-commons//公共对象或配置
cloud-provider//生产者
cloud-consumer//消费者
父项目建立
我们开发项目,现在基本都用到maven,以及用父子项目,以及公共模块依赖,来构建方便扩展的项目体系;首先我们建立父项目 cloud,主要是一个pom,管理module,以及管理依赖,规范所有jar包版本等;
新建maven项目
填写groud ip , artifact Id , Packaging 指定成pom
这里引入了 springcloud 以及springboot 包括 druid连接池 以及 属性 规范编译 目标版本,包括连接池版本 编码等信息
我们Springcloud版本 用 Edgware.SR4
修改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>
<groupId>com.hxzy.gz</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>cloud</name>
<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>
<modules>
<module>cloud-commons</module>
<module>cloud-provider-1001</module>
<module>cloud-consumer-80</module>
</modules>
</project>
cloud-commons依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
entity:
@Entity
@Table(name = "teacher")
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int tno;
private String tname;
}
provider依赖:
<dependency>
<groupId>com.hxzy.gz</groupId>
<artifactId>cloud-comms</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>
controller:
@RestController
public class TeacherProviderController_1001 {
@Resource(name = "TeacherProviderService_1001")
private TeacherProviderService_1001 teacherProviderService;
@GetMapping("del/{id}")
public String deleteById(@PathVariable int id) {
return teacherProviderService.delete(id)+"";
}
@GetMapping("get/list")
public List<Student> getList() {
return teacherProviderService.findAll();
}
}
Repository:
public interface StudentRepositroy extends JpaRepository<Student, Integer>, JpaSpecificationExecutor<Student> {
public boolean delete(int Integer);
public List<Student> findAll();
}
service:
@Component("StudentProviderService_1001")
public class StudentProviderService_1001_impl implements StudentProviderService_1001 {
@Resource
private StudentRepositroy studentRepositroy;
@Override
public List<Student> findAll() {
// TODO Auto-generated method stub
return studentRepositroy.findAll();
}
@Override
public boolean delete(Integer id) {
// TODO Auto-generated method stub
try {
studentRepositroy.delete(id);
} catch (Exception e) {
// TODO: handle exception
return false;
}
return true;
}
}
yml:
server:
context-path: /
port: 1001
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.108:3306/mysql
username: root
password: root
consumer依赖:
<dependency>
<groupId>com.hxzy.gz</groupId>
<artifactId>cloud-comms</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-tomcat</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
端口:
server:
context-path: /
port: 80
配置:
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
控制层:
@RestController
public class StudentConsumerController_80 {
@Resource
RestTemplate restTemplate;
private final String PROT_VALUE = "http://192.168.1.108:1001/";
@GetMapping("del/{id}")
public String deleteById(@PathVariable int id) {
return restTemplate.getForObject(PROT_VALUE + "del/" + id, String.class);
}
@GetMapping("get/list")
public List<Teacher> getList() {
return restTemplate.getForObject(PROT_VALUE+"get/list", List.class);
}
}
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class StudentConsumerApplication_80 {
public static void main(String[] args) {
SpringApplication.run(TeacherConsumerApplication_80.class, args);
}
}