概念

服务生产者:服务的被调用方(即:为其他服务提供服务的服务)
服务消费者:服务的调用方(即:依赖其他服务的服务)

以微商城系统为例:用户发起购买商品请求,调用商品信息微服务是否满足购买条件,如果满足那就去查用户信息,如下图所示:

商品微服务是服务消费者,用户微服务就是服务生产者。
接下来以“微商城”系统为例,编写服务生产者和消费者。

编写一个服务生产者

第一步:通过start.spring.io构建服务生产者项目

打开地址:https://start.spring.io
如下图所示,选择相应信息:

增加Dependencies模块支持:
1、web
2、jpa 访问持久层
3、h2 内嵌数据库(可以做一些数据的展示)
可以直接搜索添加即可,添加完成后点击Generate the project,将会生产代码的压缩包,解压导入idea开发工具即可。

第二步:编写sql脚本

由于用的是内嵌h2数据库,所以这里编写创建用户表的sql脚本和数据:
schema.sql

drop table user if exists;
create table user(id bigint generated by default as identity,user_name varchar(40),name varchar(20),age int(3),balance decimal(10,2),primary key(id)
)

data.sql

insert into user(id,user_name,name,age,balance) values (1,'tangseng','唐僧',20,98.00);
insert into user(id,user_name,name,age,balance) values (2,'wukong','齐天大圣',18,10000.00);
insert into user(id,user_name,name,age,balance) values (3,'bajie','二师兄',25,898.00);
insert into user(id,user_name,name,age,balance) values (4,'wujing','三师弟',35,998.00);
第三步:application.ml配置文件
server:port: 8080
spring:spa:generate-ddl: falseshow-sql: truehibernate:ddl-auto: nonedatasource:platform: h2schema: classpath:schema.sqldata: classpath:data.sql
logging:level:root: INFOorg.hibernate: INFOorg.hibernate.type.descriptor.sql.BasicBinder: TRACEorg.hibernate.type.descriptor.sql.BasicExtractor: TRACEcom.itunion: DEBUG
第三步:编写User实体类
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Long id;@Columnprivate String userName;@Columnprivate String name;@Columnprivate int age;@Columnprivate BigDecimal balance;......省略get/set方法
}
第四步:编写UserRepository
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}

这里继承了JpaRepository。

第五步:编写UserController
@RestController
public class UserController {@Autowiredprivate UserRepository userRepository;@GetMapping("/simple/{id}")public User findById(@PathVariable Long id){User user = this.userRepository.getOne(id);System.out.println(user.toString());return user;}
}
第六步:测试

浏览器输入地址:http://127.0.0.1:8080/simple/2

{"id":2,"userName":"wukong","name":"齐天大圣","age":18,"balance":10000.00}

好了,到这里一个简单的微服务服务生产者已经编写完成。
备注:代码结构如下

编写一个服务消费者

第一步:通过start.spring.io构建服务消费者项目

和上面步骤一样,Artifact需要修改,如下图所示:

增加Dependencies模块支持:
1、web
只需要web就可以了,添加完成后点击Generate the project,将会生产代码的压缩包,解压导入idea开发工具。

第二步:编写User实体类
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User {private Long id;private String userName;private String name;private int age;private BigDecimal balance;......省略其他get/set方法
}

这里只是做一个接收数据的映射,不需要jpa的注解。

第三步:编写GoodsController
@RestController
public class GoodsController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/goods/{id}")public User findById(@PathVariable Long id){return this.restTemplate.getForObject("http://127.0.0.1:8080/simple/"+id,User.class);}
}

根据传入的id通过restTemplate的方式去查询服务生产者获取用户信息。

第四步:配置application.yml文件
server:port: 8081
第五步:启动测试

服务生产者和消费者都需要启动。
访问服务消费者地址:http://127.0.0.1:8081/goods/2
报错信息:

Description:
Field restTemplate in com.itunion.cloud.web.controller.GoodsController required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

错误原因:RestTemplate 未实例化,MicroserviceSimpleConsumerGoodsApplication增加RestTemplate实例化代码

@Beanpublic RestTemplate restTemplate(){return new RestTemplate();};

再次启动并访问:http://127.0.0.1:8081/goods/2
返回结果

{"id":2,"userName":"wukong","name":"齐天大圣","age":18,"balance":10000.00}

总结

简单的服务生产者和服务消费者已经实现了,用户在购买商品的时候调用服务消费者的goods接口,然后服务消费者通过RestTemplate调用服务生产者simple查询用户信息。
文中为了快速实现生产者和消费者的关系,采取了一些硬编码的方式,在实际分布式架构中是不行的,不过没关系后面我们慢慢来使项目更加健壮。

问题

1、生产者和消费者应用如何进行监控?并且也没有画板,啥指标都没得。没办法监控系统压力、QPS、内存、CPU和日活的可视化面板,是不是很low?
2、上面提到的硬编码问题,微服务地址和端口都是固定的,在实际项目场景中每次地址发生变更都需要去修改代码(如果用Docker容器化部署那就更酸爽了)。
3、负载均衡怎么办?
4、服务直接的容错机制如何处理?
5、用户的认证和授权呢?
6、应用发生故障,如何能够进行问题追踪快速定位?

Spring Cloud(Greenwich版)-01-服务生产者与服务消费者相关推荐

  1. Spring Cloud Greenwich 新特性和F版升级分享

    来源:https://dwz.cn/LkwPsmut 前几天介绍了,关于Spring Cloud Greenwich版本发布的官方博客翻译:Spring Cloud Greenwich.RELEASE ...

  2. Spring Cloud Greenwich版本已发布!

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 就在1月23日,spring的官方博客发布了Spring Cloud Greenwich版本正式发 ...

  3. Spring Cloud Alibaba基础教程:几种服务消费方式(RestTemplate、WebClient、Feign)

    热门:Spring Cloud Greenwich.RELEASE 正式发布!一个非常有看头的版本! 通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现&g ...

  4. Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝!

    Spring Cloud Greenwich 正式版在 01/23/2019 这天正式发布了,下面我们来看下有哪些更新内容. 生命周期终止提醒 Spring Cloud Edgware Edgware ...

  5. Spring Cloud Greenwich 新特性和F升级分享

    2019.01.23 期待已久的Spring Cloud Greenwich 发布了release版本,作为我们团队也第一时间把RC版本替换为release,以下为总结,希望对你使用Spring Cl ...

  6. 《Spring Cloud Netflix官方文档》2. 服务发现:Eureka服务器

    2. 服务发现:Eureka服务器 2.1 如何创建Eureka服务器 引用org.springframework.cloud的spring-cloud-starter-eureka-server就可 ...

  7. 《Spring Cloud Netflix官方文档》1.服务发现:Eureka客户端

    1.     服务发现:Eureka客户端 服务发现是微服务架构的关键原则之一.使用手动配置或一些约定方式来处理多服务多实例的方式是非常困难,并且十分脆弱的.Eureka同时是Netflix服务发现的 ...

  8. 阿里新框架发布!干掉Spring Cloud,换下Dubbo,微服务王者来了!

    目前,但凡谈及微服务技术选型,就必然会遇到一个两难的抉择,到底该采用Dubbo,还是该选择Spring Cloud呢? 当初阿里于2017年宣布重新开源Dubbo,近年来Dubbo发展速度和势头可谓是 ...

  9. Spring Cloud Greenwich 最后一个计划版本发布!

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 局长 来源 | https://www.osc ...

  10. Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway

    转载自  Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使 ...

最新文章

  1. jquery图片播放切换插件
  2. 干货 | 吴恩达亲自为这份深度学习专项课程精炼图笔记点了赞!(附下载)
  3. 前端项目课程5 登录界面如何做
  4. 图像处理之添加文字水印
  5. Linux进程通信之文件
  6. storm生产环境部署问题
  7. 简单的ios网络数据交互
  8. mybatis 多表关联查询_Java修行第041天--MyBatis框架(下)--多表查询
  9. 树莓派教程 - 1.6 树莓派GPIO库wiringPi 外接USB串口ttyUSB ch340 cp2102
  10. RBM,DBM和DBN之间有什么区别?
  11. 计算机制图缺点,CAD与其它制图软件相比较的优缺点
  12. udp端口转发 Linux,Linux iptables 端口转发
  13. 匿名方法和Lambda表达式-天轰穿
  14. 大数据分析难不难好学吗?
  15. OpenGL超级宝典(第7版)笔记11 帧缓存运算 计算着色器 清单 3.13
  16. 算法之递归回溯(四)
  17. python打印日志(控制台内容输出)
  18. C语言入门篇----system命令
  19. springboot热启动与热部署
  20. jmeter正则表达式提取器

热门文章

  1. Android 9.0 HIDL接口添加
  2. CentOS 6系统FreeSwitch和RTMP服务 安装及演示(二)
  3. Linux内核中的atoi,itoa等函数
  4. cgroup学习(三)——伪文件
  5. The Eternal Immortality(CodeForces - 869B)同余定理
  6. sklearn 中GBDT的损失函数
  7. java 中== equals hashcode源码剖析
  8. Hadoop Configuration 源码详解
  9. Asp.net服务器端控件替换客户端控件
  10. 图论500道题--评测平台+算法标签