Spring Cloud微服务教程-第一篇

本文是Spring Cloud微服务教程的第一篇,介绍了微服务架构的概念和问题,以及如何使用Spring Boot搭建微服务。文章详细讲解了统一开发环境的配置,微服务架构的优缺点,特别是通过Spring Cloud实现服务注册与发现。首先,文章创建了一个商品微服务和订单微服务的示例,接着引入了Eureka作为服务注册中心,实现了服务的自动注册和发现,避免了硬编码的URL问题。
摘要由CSDN通过智能技术生成

Spring Cloud微服务教程-第一篇

1.教程大纲
2.统一开发环境
3.微服务架构
4.Spring Cloud简介
5.使用 Spring Boot 实现微服务
6.Spring Cloud快速入门

1.教程大纲
统一开发环境
了解微服务架构
了解Spring Cloud
Spring Cloud快速入门
Eureka服务注册中心
使用Ribbon实现负载均衡
使用Hystrix实现容错
2.统一开发环境
JDK: 1.8
IDE: IntelliJ IDEA
Maven:3.3.9
OS: Windows 10 10.0
Springboot版本:2.0+
3.微服务架构
目前微服务是非常火的架构或者说概念,也是在构建大型互联网项目时采用的架构方式。

3.1.单体架构
单体架构,是指将开发好的项目打成war包,然后发布到tomcat等容器中的应用。

假设你正准备开发一款与Uber和滴滴竞争的出租车调度软件,经过初步会议和需求分析,你可能会手动或者使用基于Spring Boot、Play或者Maven的生成器开始这个新项目,它的六边形架构是模块化的
应用核心是业务逻辑,由定义服务、领域对象和事件的模块完成。围绕着核心的是与外界打交道的适配器。适配器包括数据库访问组件、生产和处理消息的消息组件,以及提供API或者UI访问支持的web模块等。

尽管也是模块化逻辑,但是最终它还是会打包并部署为单体式应用。具体的格式依赖于应用语言和框架。例如,许多Java应用会被打包为WAR格式,部署在Tomcat或者Jetty上,而另外一些Java应用会被打包成自包含的JAR格式,类似的,Rails和Node.js会被打包成层级目录。

这种应用开发风格很常见,因为IDE和其它工具都擅长开发一个简单应用,这类应用也很易于调试,只需要简单运行此应用,用Selenium链接UI就可以完成端到端测试。单体式应用也易于部署,只需要把打包应用拷贝到服务器端,通过在负载均衡器后端运行多个拷贝就可以轻松实现应用扩展。在早期这类应用运行的很好。

3.2.单体架构存在的问题
在这里插入图片描述
在这里插入图片描述
如何解决以上问题呢? – 使用微服务架构。使得应用由重变轻。

3.3.什么是微服务?
在这里插入图片描述
微服务架构的特征
在这里插入图片描述
3.5.微服务架构示例
在这里插入图片描述
每一个应用功能区都使用微服务完成。

4.Spring Cloud简介
4.1.简介
Spring Cloud项目的官方网址:
http://projects.spring.io/spring-cloud/
4.2.Spring Cloud子项目
在这里插入图片描述
4.3.版本说明
在这里插入图片描述
4.4.Spring Cloud框架特点
在这里插入图片描述
5.使用 Spring Boot 实现微服务
在正式学习Spring Cloud之前我们先使用Spring Boot实现一个微服务。

业务非常简单:
1、商品微服务:通过商品id查询商品的服务;
2、订单微服务:创建订单时通时,通过调用商品的微服务进行查询商品数据;

图示:在这里插入图片描述
说明:
1、对于商品微服务而言,商品微服务是服务的提供者,订单微服务是服务的消费者;
2、对于订单微服务而言,订单微服务是服务的提供者,人是服务的消费者。

5.1.实现商品微服务
5.1.1.创建maven工程
地址git:
5.1.2.导入依赖
重点是导入Spring Boot的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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.zpc.microservice</groupId>
    <artifactId>microservice-item</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>${
   project.artifactId}</finalName>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    </project>
5.1.3.创建实体Item
package com.zpc.item.entity;

public class Item {
   

    private Long id;

    private String title;

    private String pic;

    private String desc;

    private Long price;

    public Item(){
   }

    public Item(long id, String title, String pic, String desc, Long price) {
   
        this.id=id;
        this.title=title;
        this.pic=pic;
        this.desc=desc;
        this.price=price;
    }

    public Long getId() {
   
        return id;
    }

    public void setId(Long id) {
   
        this.id = id;
    }

    public String getTitle() {
   
        return title;
    }

    public void setTitle(String title) {
   
        this.title = title;
    }

    public String getPic() {
   
        return pic;
    }

    public void setPic(String pic) {
   
        this.pic = pic;
    }

    public String getDesc() {
   
        return desc;
    }

    public void setDesc(String desc) {
   
        this.desc = desc;
    }

    public Long getPrice() {
   
        return price;
    }

    public void setPrice(Long price) {
   
        this.price = price;
    }

    @Override
    public String toString() {
   
        return "Item [id=" + id + ", title=" + title + ", pic=" + pic + ", desc=" + desc + ", price=" + price + "]";
    }

}

5.1.4.编写ItemService
编写ItemService用于实现具体的商品查询逻辑,为了演示方便,我们并不真正的连接数据库,而是做模拟实现。

package com.zpc.item.service;
import com.zpc.item.entity.Item;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class ItemService {
   

    private static final Map<Long, Item> ITEM_MAP = new HashMap<Long, Item>();

    static {
   // 准备一些静态数据,模拟数据库
        ITEM_MAP.put(1L, new Item(1L, "商品1", "http://图片1", "商品描述1", 1000L));
        ITEM_MAP.put(2L, new Item(2L, "商品2", "http://图片2", "商品描述2", 2000L));
        ITEM_MAP.put(3L, new Item(3L, "商品3", "http://图片3", "商品描述3", 3000L));
        ITEM_MAP.put(4L, new Item(4L, "商品4", "http://图片4", "商品描述4", 4000L));
        ITEM_MAP.put(5L, new Item(5L, "商品5", "http://图片5", "商品描述5", 5000L));
        ITEM_MAP.put(6L, new Item(6L, "商品6", "http://图片6", "商品描述6", 6000L));
        ITEM_MAP.put(7L, new Item(7L, "商品7", "http://图片7", "商品描述7", 7000L));
        ITEM_MAP.put(8L, new Item(8L, "商品8", "http://图片8", "商品描述8", 8000L));
        ITEM_MAP.put(8L, new Item(9L, "商品9", "http://图片9", "商品描述9", 9000L));
        ITEM_MAP.put(8L, new Item(10L, "商品10", "http://图片10", "商品描述10", 10000L));
    }

    /**
     * 模拟实现商品查询
     *
     * @param id
     * @return
     */
    public Item queryItemById(Long id) {
   
        return ITEM_MAP.get(id);
    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值