技术储备

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(二)-最简单的实战的更多相关文章

  1. 设计模式(二)简单工厂模式(Simple Factory Pattern)

    一.引言 这个系列也是自己对设计模式的一些学习笔记,希望对一些初学设计模式的人有所帮助的,在上一个专题中介绍了单例模式,在这个专题中继续为大家介绍一个比较容易理解的模式——简单工厂模式. 二.简单工厂 ...

  2. spring事务详解(二)简单样例

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...

  3. mininet(二)简单的路由实验

    mininet(一)实验环境搭建 mininet(二)简单的路由实验 mininet(三)简单的NAT实验 在网上找了 好几个代码都是不能直接复现成功,这里把自己实现成功的代码给大家演示一下. 实验的 ...

  4. C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解

    系列目录     [已更新最新开发文章,点击查看详细] 在我的博客<C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解>最后列出了 Fil ...

  5. jquery Mobile应用第2课《构建跨平台APP:jQuery Mobile移动应用实战》连载二(简单的QWER键盘)

    在jQuery Mobile的布局中,控件大多都是单独占据页面中的一行,按钮自然也不例外,但是仍然有一些方法能够让多个按钮组成一行,比如说在范例6-5中就利用按钮分组的方法使4个按钮并列在一行中,如图 ...

  6. 2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(二)简单示例

    学习资料:<Activiti实战> 第一章 搭建Activiti开发环境及简单示例 2.5 简单流程图及其执行过程 (1)leave.bpmn 后缀名必须是bpmn.安装了activiti ...

  7. 基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例

    QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. 每个业务服务采用独立的M ...

  8. springcloud(二):注册中心Eureka

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  9. 微服务SpringCloud+Docker入门到高级实战(教程详情)

    第一章 课程介绍和学习路线 1.微服务架构SpringCloud课程介绍 简介:课程介绍和课程大纲讲解,讲课风格和重点内容理解技巧 2.技术选型和学后水平 简介:课程所需基础和技术选型讲解,学完课程可 ...

随机推荐

  1. [SoapUI] 在某个测试步骤下面增加Script Assertion,运用 messageExchange 获取response content

    import com.eviware.soapui.support.GroovyUtils import com.eviware.soapui.support.XmlHolder import org ...

  2. Demo—标题左右两侧的对等横线

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. C# JackLib系列之自定义鼠标风格的实现

    在我们开发的过程中,有时需要我们来自定义鼠标的形状和大小,刚巧前一阵子正好用到了这个技术,找了好多资料,基本上都是黑白色的鼠标风格实现,而我要的则是自定义大小和彩色风格的光标样式.百度上的资源又太少, ...

  4. 【前端布局】px与rpx的简述

    本文只以移动设备为例说明. 注意:设计师以iphone6为标准出设计稿的话,1rpx=0.5px=1物理像素.Photoshop里面量出来的尺寸为物理像素点.所以可以直接使用标注尺寸数据. [像素Pi ...

  5. Ubuntu : 解决更新时出现 Unable to locate package update

    当用apt-get更新软件包时常出现错误提示Unable to locate package update, 尤其是在ubuntu server上,解决方法是:     先更新apt-get      ...

  6. Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件

    一下代码是自己结合教材,并结合以往用到的实例编写的代码,可以做为参考 import smtplib from email.mime.text import MIMEText from email.mi ...

  7. Integer.MIN_VALUE

    static int MAX_VALUE           值为 2^31-1 的常量,它表示 int 类型能够表示的最大值. static int MIN_VALUE           值为 - ...

  8. airpods2代连接macbook失败

    更新至最新系统(10.14.4),更新完毕,重启电脑再次连接即可. 参考连接: http://dq.tieba.com/p/6082366443

  9. putty连接fedora

    putty提示“Connection refused” 但可以ping通 经查,解决方法如下: 首先判断是否安装ssh rpm -qa openssh rpm -qa openssh-server 之 ...

  10. uwsgi启动提示:probably another instance of uWSGI is running on the same address (:8002). bind(): Address already in use [core/socket.c line 769]

    提示8002端口被占用,可以这样终止掉后再启动 sudo fuser -k 8002/tcp