2、SpringCloud

SpringCloud的概念

SpringCloud的组件

1597213783265.png

image-20210814144109379

Eureka注册中心

Feign服务间通信

Hystrix服务降级

Zuul网关组件

Configuration配置中心

Bus消息总线

现在的分布式技术

Spring Cloud和Spring Boot之间的关系

Spring Cloud Spring Boot
Angel版本 兼容Spring Boot 1.2.x
Brixton版本 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x
Camden版本 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x
Dalston版本、Edgware版本 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x
Finchley版本 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x
Greenwich版本 兼容Spring Boot 2.1.x
Spring Boot Spring Cloud
1.5.2.RELEASE Dalston.RC1
1.5.9.RELEASE Edgware.RELEASE
2.0.2.RELEASE Finchley.BUILD-SNAPSHOT
2.0.3.RELEASE Finchley.RELEASE
2.1.0.RELEASE Greenwich.SR1
2.3.0.RELEASE Hoxton.SR4

微服务环境搭建

1、技术选型

2、模块设计

工程名 描述 端口
star-cloud 父工程 -
star-common 公共模块,放置实体类和工具类 -
star-product 商品微服务 808x
star-order 订单微服务 809x

3、创建父工程(Maven项目)

作为父工程,是不需要编写源代码的,所以注解把父工程中的src目录删除掉,然后编辑父工程的pom.xml,做依赖版本控制

<?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>top.ygang</groupId>
    <artifactId>star-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 依赖版本锁定 -->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version>
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <lombok.version>1.18.16</lombok.version>
        <mysql.version>5.1.38</mysql.version>
        <mybatis.version>1.3.2</mybatis.version>
    </properties>

    <!--  版本仲裁中心,管理子模块中使用的依赖版本  -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 安装Spring Boot Maven插件,可以简化Maven和Spring Boot之间的交互 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

4、创建公共模块star-common(Maven项目)

1、在该模块pom.xml中,加入需要的依赖

<?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">
    <parent>
        <artifactId>star-cloud</artifactId>
        <groupId>top.ygang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>star-common</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

</project>

2、编写实体类以及工具类

image-20210813115629922

5、创建微服务

5.1、创建商品微服务

引入依赖

<?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">
    <parent>
        <artifactId>star-cloud</artifactId>
        <groupId>top.ygang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>star-product</artifactId>

    <dependencies>
        <dependency>
            <groupId>top.ygang</groupId>
            <artifactId>star-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
</project>

由于引入的公共模块已经引入了lombok和mysql依赖,所以不需要再引入

image-20210813152822558

编写代码

image-20210813152634400

5.2、创建订单微服务

和商品微服务一样的步骤

6、订单微服务访问商品微服务

6.1、在需要的微服务spring容器中添加RestTemplate

@Bean
public RestTemplate getRestTemplate(){
    return new RestTemplate();
}

6.2、使用RestTemplate实例方法进行访问商品微服务

@PostMapping("/insert/{pid}/{number}")
public ResultVO insert(@PathVariable Integer pid,@PathVariable Integer number){
    try {
        ResultVO resultVO = restTemplate.getForObject("http://localhost:8080/product/findByPid/" + pid, ResultVO.class);
        //此处返回的是一个LinkedHashMap
        LinkedHashMap map = (LinkedHashMap)resultVO.getE();
        //使用hutool工具,将map中的值封装进对象
        Product product = BeanUtil.fillBeanWithMap(map,new Product(),true);
        //将商品中的id和名称封装进订单对象
        Orders orders = new Orders();
        BeanUtil.copyProperties(product,orders,true);
        orders.setNumber(number);
        orders.setPrice(product.getPrice() * number);
        orders.setUsername("lucy");
        //插入数据库
        ordersService.insert(orders);
        return ResultVO.success("新增订单成功");
    }catch (Exception e){
        e.printStackTrace();
        return ResultVO.fail("新增订单异常");
    }
}