【小宅按】 服务提供即被其他微服务的调用的微服务;服务消费者即调用其他服务的微服务,下面我们来聊一聊。
概念:
- 服务提供者:被其他微服务的调用的微服务。
- 服务消费者:调用其他服务的微服务。
如图所示:
![d84e727fff88ba7c46f3437dd0f3d8f4.png](https://img-blog.csdnimg.cn/img_convert/d84e727fff88ba7c46f3437dd0f3d8f4.png)
在上图中:电影微服务需要去调用用户信息 并做相关的操作(例如把这张票记入用户已经购买的数据中) 在这里 电影微服务就是服务消费者,而用户微服务就是服务消费者。
因为springcloud是基于springboot。所以童鞋们在学习之前先了解一下springboot,所谓 springboot就是用来简化spring项目的初始搭建和开发过程的一个框架。springboot和spring的区别就是,springboot可以开发单个微服务,而springcloud 就是在上面增加了一整套的微服务解决方案-服务注册和发现,服务保护和与熔断,网关,分布式调用追踪,分布式配置管理等等。
下面就来来简单写写能表示上图的服务提供者和消费者的代码:
1:打开
https://start.spring.io/
选择所需要的配置,创建服务提供者:
![17f7451f8435e46b91d683cfbcec375d.png](https://img-blog.csdnimg.cn/img_convert/17f7451f8435e46b91d683cfbcec375d.png)
![f9f4ef77a22dd2eefd6e9827d7935a4a.png](https://img-blog.csdnimg.cn/img_convert/f9f4ef77a22dd2eefd6e9827d7935a4a.png)
点击生成按钮,然后idea导入该工程。
![fe8557c573d747d70dbb71e9ce755489.png](https://img-blog.csdnimg.cn/img_convert/fe8557c573d747d70dbb71e9ce755489.png)
写好连接mysql的配置
![1fcebee234b0d7452db7cd0ee7eedd36.png](https://img-blog.csdnimg.cn/img_convert/1fcebee234b0d7452db7cd0ee7eedd36.png)
创建用户表
![95e4cd700f9154ba6a01cfbefc29f5d8.png](https://img-blog.csdnimg.cn/img_convert/95e4cd700f9154ba6a01cfbefc29f5d8.png)
创建实体类 并添加getter setter 方法
![c3f5ca6dc3209c29cb78d94caef9d0ea.png](https://img-blog.csdnimg.cn/img_convert/c3f5ca6dc3209c29cb78d94caef9d0ea.png)
写好DAO
![1cad804b2e0051f536d0b075e08b9c34.png](https://img-blog.csdnimg.cn/img_convert/1cad804b2e0051f536d0b075e08b9c34.png)
写好controller
![911cdf31e0fbdf275bcf72aa52e30a30.png](https://img-blog.csdnimg.cn/img_convert/911cdf31e0fbdf275bcf72aa52e30a30.png)
访问数据:
![8ff1c702a9b0207947a3405809867007.png](https://img-blog.csdnimg.cn/img_convert/8ff1c702a9b0207947a3405809867007.png)
以上就把一个简单的用户微服务写完了,下面继续来写服务消费者(即电影微服务)
在idea中新添加一个moudle
![630680cdf869dc59d08ef610bf2f62bf.png](https://img-blog.csdnimg.cn/img_convert/630680cdf869dc59d08ef610bf2f62bf.png)
选择spring Initializr(上面的服务提供者也可用此方式)-next
![1dc7115d5c9ab1f68b16cc1d648f7d4d.png](https://img-blog.csdnimg.cn/img_convert/1dc7115d5c9ab1f68b16cc1d648f7d4d.png)
![f3d3728ee293585a905e025f0364587c.png](https://img-blog.csdnimg.cn/img_convert/f3d3728ee293585a905e025f0364587c.png)
->next
![6e87580e5ea3bd5ec7afa2cce28216a0.png](https://img-blog.csdnimg.cn/img_convert/6e87580e5ea3bd5ec7afa2cce28216a0.png)
->next
![486ad012adbd1bb4056d8dd261ceff21.png](https://img-blog.csdnimg.cn/img_convert/486ad012adbd1bb4056d8dd261ceff21.png)
->finish
来写服务消费者(电影微服务)
配置
![2b2c0308dcef899937801a7c30ce079d.png](https://img-blog.csdnimg.cn/img_convert/2b2c0308dcef899937801a7c30ce079d.png)
用户实体类
![7587cea8bd61722c0f603dedc0cda7bc.png](https://img-blog.csdnimg.cn/img_convert/7587cea8bd61722c0f603dedc0cda7bc.png)
现在启动类中写好 RestTemplate的bean,因为这个类需要new才能拿来用 所以放在这里 项目一启动就自动new了。
![1e8a5cb40810c6ced6fc780ee04b6871.png](https://img-blog.csdnimg.cn/img_convert/1e8a5cb40810c6ced6fc780ee04b6871.png)
controller类
![dbf739f4aa5d870d3e2e3ae16796cf46.png](https://img-blog.csdnimg.cn/img_convert/dbf739f4aa5d870d3e2e3ae16796cf46.png)
最后电影微服务调用用户微服务
![94340508d5cd0361080caed6b6f87885.png](https://img-blog.csdnimg.cn/img_convert/94340508d5cd0361080caed6b6f87885.png)
注意这两个微服务都要启动
![0d72ce325c8657184116d08e2fac712c.png](https://img-blog.csdnimg.cn/img_convert/0d72ce325c8657184116d08e2fac712c.png)
源码地址:
https://github.com/INGUCoder/learning
更多精彩内容,请滑至顶部点击右上角关注小宅哦~
![f4cd42f93af07d63211d9d28b9320a56.gif](https://img-blog.csdnimg.cn/img_convert/f4cd42f93af07d63211d9d28b9320a56.gif)
来源:华为云社区原创 作者:INGUCoder