目录

微服务拆分-拆分购物车服务

目录

微服务拆分-拆分购物车服务

新建一个cart-service模块,实现购物车服务。

https://i-blog.csdnimg.cn/direct/c52626182c1348279583a50485c4106b.png

导入item-service模块的依赖。

    <dependencies>
        <!--common-->
        <dependency>
            <groupId>com.heima</groupId>
            <artifactId>hm-common</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.1.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.1.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

新建包并且准备一个启动类。

https://i-blog.csdnimg.cn/direct/b801e41d2ff947b89b014efbe4928433.png

拷贝item-service模块的配置文件进行修改,每一个微服务都有自己的数据库。

https://i-blog.csdnimg.cn/direct/c3571aa361ce4a958278af08c6b8bab6.png

https://i-blog.csdnimg.cn/direct/92a67ef3d8c0492fbca80ad49ebaa459.png

https://i-blog.csdnimg.cn/direct/c5db81beb66141b182a1d9893b56cecd.png

https://i-blog.csdnimg.cn/direct/d16d4d6781404560a5e68a74af97ee8c.png

将hm-service模块里面有关购物车的代码拷贝到cart-service模块中去。 做了拆分该模块获取不到其他模块的数据库信息,所以将该模块的功能注释起来,加上TODO以便后期处理。

https://i-blog.csdnimg.cn/direct/72e06c41402e4dadbd6ea25cfa8651b5.png

https://i-blog.csdnimg.cn/direct/7ca9dcf216504135b3f34cc42e9ca39b.png https://i-blog.csdnimg.cn/direct/20e8ea405b4f41da86024b61f3addc71.png

虽然业务不完整但是可以运行,我们希望它激活的是local不是dev配置文件,因为这样它读取的才是我们虚拟机MySQL的ip地址。

https://i-blog.csdnimg.cn/direct/3b391c75dd424192b9df6da1d57ddf23.png

https://i-blog.csdnimg.cn/direct/4db0dff26a884586b9edbcac326b1b99.png

https://i-blog.csdnimg.cn/direct/be88ce0e846140aea96e9dcc241ff808.png

https://i-blog.csdnimg.cn/direct/1f699a2678d34ea69273a60f64eee1a4.png

https://i-blog.csdnimg.cn/direct/3829344d810a45c4a6196339a1e62251.png