springcloud(二)-最简单的实战
技术储备
Spring cloud并不是面向零基础开发人员,它有一定的学习曲线。
- 语言基础:spring cloud是一个基于Java语言的工具套件,所以学习它需要一定的Java基础。当然,spring cloud同样也支持使用Scala,Groovy等语言进行开发。
- Spring boot:Spring cloud是基于spring boot构建的,因此它延续了spring boot的契约模式以及开发模式。学习spring cloud之前需要学习spring boot,至少先入门。
- 项目管理与构建工具:目前业界比较主流的项目管理与构建工具有maven和Gradle等,我想大家最常用的,或者说用的最多的还是maven吧。
工具及软件版本
- JDK:springboot官方推荐使用的是JDK1.8,当然Spring cloud推荐的也是JDK1.8。所以你懂的呀,不用纠结低版本的了呀。
- Spring boot:使用Spring Boot 1.5.9.RELEASE。虽然现在Spring boot2.0出来了,但是需要结合Spring cloud的版本兼容。
- Spring cloud:使用 Spring Cloud Edgware RELEASE.(你要想用最新的也没问题,但是要注意springboot兼容性)
- maven:我用的是3.5.0啊。无所谓
- 开发工具:我独爱eclipse。虽然我知道IntelliJ IDEA 可能更优秀。
服务的提供者与服务的消费者
使用微服务构建的是分布式系统,微服务之间通过网络进行通信。我们使用服务提供者与服务消费者来描述为服务之间的调用关系。
名词 | 定义 |
服务提供者 | 服务的被调用方(即:为其他服务提供服务的服务) |
服务消费者 | 服务的调用方(即:依赖其他服务的服务) |
举个生活中的例子:我们去超市买东西,商品又是由厂商提供给超市的。在这一过程中,厂商就是服务的提高者,超市是服务的消费者。
我们只需要到超市消费就行,不用到厂商去了。
编写服务提供者
先准备下数据库和数据
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(40) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`balance` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'account1', '张三', '20', '98.23');
INSERT INTO `user` VALUES ('2', 'account2', '李四', '24', '23.12');
INSERT INTO `user` VALUES ('3', 'account3', '王五', '32', '42.12');
创建一个springboot项目,ArtifactId为microservice-simple-provider-user
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.itmuch.cloud</groupId>
<artifactId>microservice-simple-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>microservice-simple-provider-user</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
<!-- jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies> <!-- 引入spring cloud 的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <!-- 添加spring-boot 的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
点击展开
用户实体类User.java
package com.itmuch.cloud.entity; import java.math.BigDecimal; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public BigDecimal getBalance() {
return balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
} }
点击展开
DAO
package com.itmuch.cloud.dao; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import com.itmuch.cloud.entity.User; @Repository
public interface UserRepository extends JpaRepository<User, Long> { }
创建Controller
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.itmuch.cloud.dao.UserRepository;
import com.itmuch.cloud.entity.User; @RestController
public class UserController {
@Autowired
private UserRepository userRepository; @GetMapping("/{id}")
public User findById(@PathVariable Long id) {
User findOne = userRepository.findOne(id);
return findOne;
}
}
最后还有一个启动类不用说了吧。
配置文件application.yml
server:
port: 8084
spring:
application:
name: microservice-provider-user
jpa:
generate-ddl: false
show-sql: true
database-platform: org.hibernate.dialect.MySQL5Dialect
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8
username: root
password: 1234
driver-class-name: com.mysql.jdbc.Driver
http:
multipart:
maxFileSize: 100Mb
maxRequestSize: 100Mb
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
测试
启动访问localhost:8084/1. 试下结果。
{"id":1,"username":"account1","name":"张三","age":20,"balance":98.23}
哦了。
编写服务消费者
创建一个maven项目,ArtifactId是microservice-simple-consumer-movie.
pom.xml
一样一样的。
POJO类User.java
package com.itmuch.cloud.pojo; import java.math.BigDecimal; public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public BigDecimal getBalance() {
return balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
} }
点击展开
Controller类
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.itmuch.cloud.pojo.User; @RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate; @GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return restTemplate.getForObject("http://localhost:8084/" + id, User.class);
}
}
点击展开
启动类
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
public class ConsumerMovieApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerMovieApplication.class, args);
}
}
点击展开
配置文件application.yml
server:
port: 8082
测试
好了,你把两个服务都启动,访问http://127.0.0.1:8082/user/1, 看结果。
{"id":1,"username":"account1","name":"张三","age":20,"balance":98.23}
OK了。
springcloud(二)-最简单的实战的更多相关文章
- 设计模式(二)简单工厂模式(Simple Factory Pattern)
一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理解的模式——简单工厂模式. 二.简单工厂 ...
- spring事务详解(二)简单样例
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- mininet(二)简单的路由实验
mininet(一)实验环境搭建 mininet(二)简单的路由实验 mininet(三)简单的NAT实验 在网上找了 好几个代码都是不能直接复现成功,这里把自己实现成功的代码给大家演示一下. 实验的 ...
- C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解
系列目录 [已更新最新开发文章,点击查看详细] 在我的博客<C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解>最后列出了 Fil ...
- jquery Mobile应用第2课《构建跨平台APP:jQuery Mobile移动应用实战》连载二(简单的QWER键盘)
在jQuery Mobile的布局中,控件大多都是单独占据页面中的一行,按钮自然也不例外,但是仍然有一些方法能够让多个按钮组成一行,比如说在范例6-5中就利用按钮分组的方法使4个按钮并列在一行中,如图 ...
- 2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(二)简单示例
学习资料:<Activiti实战> 第一章 搭建Activiti开发环境及简单示例 2.5 简单流程图及其执行过程 (1)leave.bpmn 后缀名必须是bpmn.安装了activiti ...
- 基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例
QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. 每个业务服务采用独立的M ...
- springcloud(二):注册中心Eureka
Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...
- 微服务SpringCloud+Docker入门到高级实战(教程详情)
第一章 课程介绍和学习路线 1.微服务架构SpringCloud课程介绍 简介:课程介绍和课程大纲讲解,讲课风格和重点内容理解技巧 2.技术选型和学后水平 简介:课程所需基础和技术选型讲解,学完课程可 ...
随机推荐
- $_SERVER['HTTP_REFERER']
$_SERVER['HTTP_REFERER']//获取前一个页面的url地址
- Spring Boot利用poi导出Excel
至于poi的用法就不多说了,网上多得很,但是发现spring boot结合poi的就不多了,而且大多也有各种各样的问题. public class ExcelData implements Seria ...
- [GO]字符串的使用
package main import ( "fmt" "strings" ) func main() { //判断字符串1是否包含字符串2,如果包含则返回tr ...
- jquery 常用工具方法
inArray(value, array [, fromIndex ])方法类似于原生javascript的indexOf()方法,没有找到匹配元素时它返回-1.如果数组第一个元素匹配参数,那么$.i ...
- php 用csv文件导出大量数据初方案
背景:接手的项目中支持导出一批数据,全数量在50W左右.在接手的时候看代码是直接一次查询MySQL获得数据,然后用header函数直接写入csv,用户开始导出则自动下载.但是,在全导出的时候,功能出现 ...
- java GC机制(转)
http://blog.csdn.net/zsuguangh/article/details/6429592 1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之 ...
- 敏捷开发之XP
敏捷方法论有一个共同的特点,那就是都将矛头指向了“文档”,它们认为传统的软件工程方法文档量太“重”了,称为“重量级”方法,而相应的敏捷方法则是“轻量级”方法.正是因为“轻量级”感觉没有什么力量,不但不 ...
- Java 栈与堆简介
一.前言 长久以来,一直被Java的内存分配问题,堆和栈问题困扰好久,面试的时候也非常心虚,这几天好好通过看书和技术博客来整理了一下,希望能找到我自己的理解方式. 二.内存 内存分物理内存和虚拟内存, ...
- 第一课 了解SQL
1.1数据库基础 数据库:数据库是一个以某种有组织的方式存储的数据集合,可以想象是一个文件柜 数据库管理软件:DBMS用来操做创建数据库的软件 表:某种特定类型数据的结构化清单,数据库的下一层就是表 ...
- Reporting Service服务SharePoint集成模式安装配置(6、安装Reporting services Add-in for SharePoint 外接程序)
第五步骤 : 安装Reporting services Add-in for SharePoint 外接程序 RS 外接程序是在 SharePoint 服务器上运行用于 SharePoint 产品的 ...