目录

微服务拆分-拆分商品服务

目录

微服务拆分-拆分商品服务

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

https://i-blog.csdnimg.cn/direct/858e99b18f4c458aa570bd27222955b2.png

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

复制hm-service模块pom.xml文件里面的依赖到item-service模块pom.xml文件。 https://i-blog.csdnimg.cn/direct/07fb84a974194002b98f5be39b3f9919.png

需要准备的依赖

<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>
    </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/11cc4da103b74bfa9e1e5cfe688c3a81.png

准备以下的包

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

准备一个启动类,或者拷贝hm-service模块里面的启动类进行修改。

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

将hm-service模块里面的配置文件拷贝到item-service模块进行修改。

https://i-blog.csdnimg.cn/direct/23a0a0936ed04363a7eebbfeedf0c547.png

https://i-blog.csdnimg.cn/direct/40ed44a9ac63449086d4a2ea47231342.png

我们每一个微服务都要做到独立,将来独立部署还要做到数据独立,也就是要做到自己有自己独立的数据库。我们需要去创建一个新的MySQL的实例,利用Docker重写创建一个,然后这个微服务独享一台MySQL,那么ip地址端口可能都会变化。但是这样的话成本太高了,假如说需要10个服务,就需要10台MySQL。我们采用了折中方案,我们不可能再用原来的这张表这个库了,我们用一台MySQL然后在这里面去创建不同的database,每个微服务创建一个database来做数据隔离。

把准备好的sql 放到MySQL里面去运行。

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

https://i-blog.csdnimg.cn/direct/4792b83c59414d4e9dbc4835f9093707.png https://i-blog.csdnimg.cn/direct/39ebc069505c402999e588ea2d96a7fd.png

https://i-blog.csdnimg.cn/direct/8fe3ac3c2e5d4448be898284bc594385.png https://i-blog.csdnimg.cn/direct/6a536b1cb4a041599d632e15aee8bb1c.png

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

将 hm-service模块里面有关商品的类拷贝到item-service模块里面去。

https://i-blog.csdnimg.cn/direct/556446776c164ec8814cdfb92f57223c.png

将报错的包删掉让它重新导入。

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

https://i-blog.csdnimg.cn/direct/7fed1945f6ae48158cf49a96a732a251.png

ALT+8所有的启动类都在这里,如果没有出现启动类就刷新Maven。

https://i-blog.csdnimg.cn/direct/867f019e17334763a389aa6525210425.png

https://i-blog.csdnimg.cn/direct/0c603ec659114313a94878762cdacdb8.png

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

启动成功

https://i-blog.csdnimg.cn/direct/d2d9d71bb8834a559cf371c9c54a5129.png ·测试

所有的服务都配置了Swagger所以我们可以直接访问localhost:8081/doc.html。

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

商品微服务的拆分完成。