Springcloud是一系列框架的有序集合,它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,
分布式有卵用:一口火锅一个人吃完要点时间,一桌子人一人一口就没了。一个应用一台服务器带不动,多来几台就好了。
前置科技: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
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
| <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依赖:
1 2 3 4 5 6 7 8 9 10
| <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:
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| @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() { return studentRepositroy.findAll(); }
@Override public boolean delete(Integer id) { try { studentRepositroy.delete(id); } catch (Exception e) { 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: 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); } }
|