spring cloud microservice examples

2025-12-07 0 223

springcloudmicroserviceexamples

spring-cloud-microservice-examples

说明

目前该项目实现了 zuul(路由模块), config-server(配置管理), eureka server(服务注册和发现), zipkin(服务调用追踪),hystrix, turbine stream (熔断分析)
simple-service,simple-serviceB两个待发现的服务
simple-ui (一个用angular写的前端页面)

路由功能实现在 cloud-api-gateway 模块,注册到eureka server上面,所有的请求访问 http://l*oc*alh*ost:5555, 然后根据路由规则

zuul.routes.api-a.path: /cloud-simple-service/**
zuul.routes.api-a.serviceId: cloud-simple-service

zuul.routes.api-b.path: /cloud-simple-serviceB/**
zuul.routes.api-b.serviceId: cloud-simple-serviceB

zuul.routes.api-ui.path: /cloud-simple-ui/**
zuul.routes.api-ui.serviceId: cloud-simple-ui

分别请求到 注册到eureka server的cloud-simple-service 和 cloud-simple-serviceB服务。
服务的架构图:


使用指南

  • 先决条件
    本机安装rabbitmq,并启动
rabbitmq-server

本机安装mysql,并启动且创建dev和test数据库,并分别创建表

mysql.server start
mysql -uroot
  CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
 dev数据库的user表中插入数据
 INSERT INTO `user` VALUES (1,\'dev1\'),(2,\'dev2\'),(3,\'dev3\');
 test数据库的user表中插入数据
 INSERT INTO `user` VALUES (1,\'test1\'),(2,\'test2\'),(3,\'test3\');
  • 运行各模块
cd cloud-api-gateway
mvn spring-boot:run  #端口5555
cd cloud-config-server
mvn spring-boot:run  #端口8888
cd cloud-eureka-server
mvn spring-boot:run  #端口8761
cd cloud-simple-service
mvn spring-boot:run  #端口8081
cd cloud-simple-service
mvn spring-boot:run --server.port=8082  # cloud-simple-service 以8082端口再次启动服务
cd cloud-simple-ui
mvn spring-boot:run #端口8090
cd cloud-zipkin
mvn spring-boot:run #端口9966
  • 打开浏览器输入网址并浏览效果
 查看Eureka Server
 http://localh*o**st:8761 #查看eureka

请求simple service, simple service2, simple serviceB
http://localh*o**st:8081/user  #simple service
结果:
[
  {
      id: 1,
      username: \"dev1\"
  },
  {
      id: 2,
      username: \"dev2\"
  },
  {
      id: 3,
      username: \"dev3\"
  }
]
http://lo*ca*lhos*t:8082/user  #simple service2
结果:
[
  {
      id: 1,
      username: \"dev1\"
  },
  {
      id: 2,
      username: \"dev1\"
  },
  {
      id: 3,
      username: \"dev1\"
  }
]
http://lo**calh*ost:8091/user  #simple serviceB
结果:
Result from simpleserviceB

本项目实现了通过spring-cloud-bus, 传播config-server中config的变化.下面动手验证之.

  1. 下载配置git repository
    git clone git@github.com:zpng/spring-cloud-config-demo.git
    根目录下有个cloud-config-repo目录,该目录下有两个文件:
    cloud-simple-service-dev.properties
    cloud-simple-service-test.properties
    分别是cloud-simple-service在 dev和test环境下的配置信息
    cloud-simple-service-dev.properties内容:
    “`
    mysqldb.datasource.url=jdbc:mysql://localhost:3306/dev?useUnicode=true&characterEncoding=utf-8
mysqldb.datasource.username=root

mysqldb.datasource.password=

logging.level.org.springframework.web:DEBUG

```
cloud-simple-service-test.properties内容:
```
mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/test?useUnicode\\=true&characterEncoding\\=utf-8

mysqldb.datasource.username=root

mysqldb.datasource.password=

logging.level.org.springframework.web:DEBUG
```
  1. 修改 cloud-simple-service-dev.properties 内容,
 mysql url中使用的dev数据库变为test数据库
  mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/dev?useUnicode\\=true&characterEncoding\\=utf-8
  ->
  mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/test?useUnicode\\=true&characterEncoding\\=utf-8

  git add -A
  git commit -m \"MOD: update config\"
  git push origin master  #将修改push到git repositoy
  1. 此时并需要重启config-server或者simple-service,只需要发送一个POST请求到config-server,并通过bus传播到使用该配置文件的服务中.
    curl -X POST http://localhost***:8888/bus/refresh #(config-server启动在8888端口)
    此时刷新前端页面
    http://localh*o**st:8081/user #simple service http://lo*ca*lhos*t:8082/user #simple service2
    发现数据都已变成:
    [ { id: 1, username: \"test1\" }, { id: 2, username: \"test2\" }, { id: 3, username: \"test3\" } ]

    1. 验证路由逻辑
    cloud-api-gateway服务使用了zuul进行请求转发,转发规则如下:
    “`
    # routes to serviceId
    zuul.routes.api-a.path: /cloud-simple-service/**
    zuul.routes.api-a.serviceId: cloud-simple-service
zuul.routes.api-b.path: /cloud-simple-serviceB/**
zuul.routes.api-b.serviceId: cloud-simple-serviceB

zuul.routes.api-ui.path: /cloud-simple-ui/**
zuul.routes.api-ui.serviceId: cloud-simple-ui
```
并且zuul服务中进行了token验证,需要请求参数中包含accessToken,accessToken可以为任意值,如果不包含该参数则请求不能
转发过去.
```
http://local*host*:*5555/cloud-simple-ui/users?accessToken=test
结果:
[
    {
        id: 1,
        username: \"test1\"
    },
    {
        id: 2,
        username: \"test2\"
    },
    {
        id: 3,
        username: \"test3\"
    }
]

http://*loc*alhost:*5555/cloud-simple-serviceB/user?accessToken=tbbsxxxxd
结果: Result from simpleserviceB
http://*loca*l*host:5555/cloud-simple-service/user?accessToken=xxxdaew
结果:
[
    {
        id: 1,
        username: \"test1\"
    },
    {
        id: 2,
        username: \"test2\"
    },
    {
        id: 3,
        username: \"test3\"
    }
]
```
可见zuul已经完全发挥了它的路由作用.
  1. Hystrix
    Hystrix是熔断器, Hystrx Dashboard实现了监控单个Hystrix stream的功能.
    http://l*oc*alhos*t:8022/hystrix/
    打开后页面如下:

    在其中输入
    http://**localh*ost:8090/hystrix.stream
    (cloud-simple-ui服务),即可监控该服务的stream,如下图

    注意需要请求几次cloud-simple-ui服务,该图上才会有结果.

  2. Turbine
    本示例使用了turbine-amqp, 然后各个需要统计hystrix stream的微服务,包含依赖
    <!--for turbine stream--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency>
    即可将stream 发送到rabbitmq队列,然后turbine stream项目获取这些stream, 然后显示在图示上,这样跟之前的区别是可以监控所有
    的微服务,而不是单个主机的stream.
    同样打开
    http://l*oc*alhos*t:8022/hystrix/
    在其中输入
    http://lo*ca*lhost:*8989/turbine.stream
    则可以看到如下图所示

    如图所示可以看到 cloud-simple-service, cloud-simple-serviceB, cloud-simple-ui 共3个服务的Hystrix Stream.

  3. Zipkin
    zipkin可以跟踪微服务的调用以及,各个路径上面的时间,进而分析瓶颈.
    打开
    http://loc*al*h*ost:9966

trace如下图:

dependencies如下图:

下载源码

通过命令行克隆项目:

git clone https://github.com/zpng/spring-cloud-microservice-examples.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 spring cloud microservice examples https://www.zuozi.net/31778.html

angularjs eclipse
上一篇: angularjs eclipse
php apache tika
下一篇: php apache tika
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务