{"id":14973165,"url":"https://github.com/zpng/spring-cloud-microservice-examples","last_synced_at":"2025-04-06T03:07:27.910Z","repository":{"id":90087970,"uuid":"72173466","full_name":"zpng/spring-cloud-microservice-examples","owner":"zpng","description":"spring-cloud-microservice-examples","archived":false,"fork":false,"pushed_at":"2017-03-24T02:18:20.000Z","size":162,"stargazers_count":360,"open_issues_count":7,"forks_count":236,"subscribers_count":55,"default_branch":"master","last_synced_at":"2025-03-30T01:13:19.828Z","etag":null,"topics":["angularjs","cloud","eureka-server","finagle","hystrix","java","spring-cloud-microservice","springboot","thrift","zipkin"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zpng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-28T04:31:24.000Z","updated_at":"2025-02-21T10:39:40.000Z","dependencies_parsed_at":"2023-03-02T23:00:18.468Z","dependency_job_id":null,"html_url":"https://github.com/zpng/spring-cloud-microservice-examples","commit_stats":{"total_commits":35,"total_committers":2,"mean_commits":17.5,"dds":0.02857142857142858,"last_synced_commit":"6938297335e924f8066f5558b79ee82fa204c4ee"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zpng%2Fspring-cloud-microservice-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zpng%2Fspring-cloud-microservice-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zpng%2Fspring-cloud-microservice-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zpng%2Fspring-cloud-microservice-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zpng","download_url":"https://codeload.github.com/zpng/spring-cloud-microservice-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427006,"owners_count":20937201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angularjs","cloud","eureka-server","finagle","hystrix","java","spring-cloud-microservice","springboot","thrift","zipkin"],"created_at":"2024-09-24T13:48:15.216Z","updated_at":"2025-04-06T03:07:27.887Z","avatar_url":"https://github.com/zpng.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spring-cloud-microservice-examples\nspring-cloud-microservice-examples\n\n## 说明\n  目前该项目实现了 zuul(路由模块), config-server(配置管理), eureka server（服务注册和发现）, zipkin（服务调用追踪）,hystrix, turbine stream (熔断分析)\n  simple-service,simple-serviceB两个待发现的服务\n  simple-ui (一个用angular写的前端页面)\n  \n  路由功能实现在 cloud-api-gateway 模块，注册到eureka server上面，所有的请求访问 http://localhost:5555, 然后根据路由规则\n  ```\nzuul.routes.api-a.path: /cloud-simple-service/**\nzuul.routes.api-a.serviceId: cloud-simple-service\n\nzuul.routes.api-b.path: /cloud-simple-serviceB/**\nzuul.routes.api-b.serviceId: cloud-simple-serviceB\n\nzuul.routes.api-ui.path: /cloud-simple-ui/**\nzuul.routes.api-ui.serviceId: cloud-simple-ui\n ```\n 分别请求到  注册到eureka server的cloud-simple-service 和 cloud-simple-serviceB服务。\n 服务的架构图:\n ![流程图](https://docs.google.com/drawings/d/1kb_2cLW-KcwhWfmu-iburNTCKKuH7HGUEdQCKCZMgZE/pub?w=960\u0026h=720)\n\n---\n## 使用指南\n  * 先决条件\n  本机安装rabbitmq,并启动\n  ```\n  rabbitmq-server\n  ```\n  本机安装mysql,并启动且创建dev和test数据库,并分别创建表\n  ```\n  mysql.server start\n  mysql -uroot\n    CREATE TABLE `user` (\n  `id` int(11) NOT NULL,\n  `username` varchar(255) NOT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8\n   dev数据库的user表中插入数据\n   INSERT INTO `user` VALUES (1,'dev1'),(2,'dev2'),(3,'dev3');\n   test数据库的user表中插入数据\n   INSERT INTO `user` VALUES (1,'test1'),(2,'test2'),(3,'test3');\n  ```\n\n * 运行各模块\n  ```\n  cd cloud-api-gateway\n  mvn spring-boot:run  #端口5555\n  cd cloud-config-server\n  mvn spring-boot:run  #端口8888\n  cd cloud-eureka-server\n  mvn spring-boot:run  #端口8761\n  cd cloud-simple-service\n  mvn spring-boot:run  #端口8081\n  cd cloud-simple-service\n  mvn spring-boot:run --server.port=8082  # cloud-simple-service 以8082端口再次启动服务\n  cd cloud-simple-ui\n  mvn spring-boot:run #端口8090\n  cd cloud-zipkin\n  mvn spring-boot:run #端口9966\n  ```\n * 打开浏览器输入网址并浏览效果\n ```\n  查看Eureka Server\n  http://localhost:8761 #查看eureka\n ```\n  ![Eureka Server](https://drive.google.com/uc?id=0BxyRSlBgU-ShX1dEdG5YSi10OEE)\n\n  ---\n  ```\n  请求simple service, simple service2, simple serviceB\n  http://localhost:8081/user  #simple service\n  结果:\n  [\n    {\n        id: 1,\n        username: \"dev1\"\n    },\n    {\n        id: 2,\n        username: \"dev2\"\n    },\n    {\n        id: 3,\n        username: \"dev3\"\n    }\n  ]\n  http://localhost:8082/user  #simple service2\n  结果:\n  [\n    {\n        id: 1,\n        username: \"dev1\"\n    },\n    {\n        id: 2,\n        username: \"dev1\"\n    },\n    {\n        id: 3,\n        username: \"dev1\"\n    }\n  ]\n  http://localhost:8091/user  #simple serviceB\n  结果:\n  Result from simpleserviceB\n  ```\n  本项目实现了通过spring-cloud-bus, 传播config-server中config的变化.下面动手验证之.\n  1. 下载配置git repository\n    ```\n    git clone git@github.com:zpng/spring-cloud-config-demo.git\n    ```\n    根目录下有个cloud-config-repo目录,该目录下有两个文件:\n    cloud-simple-service-dev.properties\n    cloud-simple-service-test.properties\n    分别是cloud-simple-service在 dev和test环境下的配置信息\n    cloud-simple-service-dev.properties内容:\n    ```\n    mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/dev?useUnicode\\=true\u0026characterEncoding\\=utf-8\n\n    mysqldb.datasource.username=root\n\n    mysqldb.datasource.password=\n\n    logging.level.org.springframework.web:DEBUG\n\n    ```\n    cloud-simple-service-test.properties内容:\n    ```\n    mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/test?useUnicode\\=true\u0026characterEncoding\\=utf-8\n\n    mysqldb.datasource.username=root\n\n    mysqldb.datasource.password=\n\n    logging.level.org.springframework.web:DEBUG\n    ```\n  1. 修改 cloud-simple-service-dev.properties 内容,\n   ```\n    mysql url中使用的dev数据库变为test数据库\n     mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/dev?useUnicode\\=true\u0026characterEncoding\\=utf-8\n     -\u003e\n     mysqldb.datasource.url=jdbc\\:mysql\\://localhost\\:3306/test?useUnicode\\=true\u0026characterEncoding\\=utf-8\n\n     git add -A\n     git commit -m \"MOD: update config\"\n     git push origin master  #将修改push到git repositoy\n   ```\n  1. 此时并需要重启config-server或者simple-service,只需要发送一个POST请求到config-server,并通过bus传播到使用该配置文件的服务中.\n    ```\n    curl -X POST http://localhost:8888/bus/refresh  #(config-server启动在8888端口)\n    ```\n    此时刷新前端页面\n    ```\n    http://localhost:8081/user  #simple service\n    http://localhost:8082/user  #simple service2\n    ```\n    发现数据都已变成:\n    ```\n    [\n        {\n            id: 1,\n            username: \"test1\"\n        },\n        {\n            id: 2,\n            username: \"test2\"\n        },\n        {\n            id: 3,\n            username: \"test3\"\n        }\n    ]\n    ```\n    ---\n    1. 验证路由逻辑\n    cloud-api-gateway服务使用了zuul进行请求转发,转发规则如下:\n    ```\n    # routes to serviceId\n    zuul.routes.api-a.path: /cloud-simple-service/**\n    zuul.routes.api-a.serviceId: cloud-simple-service\n\n    zuul.routes.api-b.path: /cloud-simple-serviceB/**\n    zuul.routes.api-b.serviceId: cloud-simple-serviceB\n\n    zuul.routes.api-ui.path: /cloud-simple-ui/**\n    zuul.routes.api-ui.serviceId: cloud-simple-ui\n    ```\n    并且zuul服务中进行了token验证,需要请求参数中包含accessToken,accessToken可以为任意值,如果不包含该参数则请求不能\n    转发过去.\n    ```\n    http://localhost:5555/cloud-simple-ui/users?accessToken=test\n    结果:\n    [\n        {\n            id: 1,\n            username: \"test1\"\n        },\n        {\n            id: 2,\n            username: \"test2\"\n        },\n        {\n            id: 3,\n            username: \"test3\"\n        }\n    ]\n\n    http://localhost:5555/cloud-simple-serviceB/user?accessToken=tbbsxxxxd\n    结果: Result from simpleserviceB\n    http://localhost:5555/cloud-simple-service/user?accessToken=xxxdaew\n    结果:\n    [\n        {\n            id: 1,\n            username: \"test1\"\n        },\n        {\n            id: 2,\n            username: \"test2\"\n        },\n        {\n            id: 3,\n            username: \"test3\"\n        }\n    ]\n    ```\n    可见zuul已经完全发挥了它的路由作用.\n\n   1. Hystrix\n    Hystrix是熔断器, Hystrx Dashboard实现了监控单个Hystrix stream的功能.\n    ```\n    http://localhost:8022/hystrix/\n    ```\n    打开后页面如下:\n    ![hystrix stream页面](https://drive.google.com/uc?id=0BxyRSlBgU-ShTG1QZUpSc1hCV2c)\n    在其中输入\n    ```\n    http://localhost:8090/hystrix.stream\n    ```\n    (cloud-simple-ui服务),即可监控该服务的stream,如下图\n    ![simple-ui-hystrix-stream](https://drive.google.com/uc?id=0BxyRSlBgU-ShUmRwNzluRWhxNmM)\n    注意需要请求几次cloud-simple-ui服务,该图上才会有结果.\n   1. Turbine\n    本示例使用了turbine-amqp, 然后各个需要统计hystrix stream的微服务,包含依赖\n    ```\n            \u003c!--for turbine stream--\u003e\n            \u003cdependency\u003e\n                \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n                \u003cartifactId\u003espring-cloud-netflix-hystrix-stream\u003c/artifactId\u003e\n            \u003c/dependency\u003e\n            \u003cdependency\u003e\n                \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n                \u003cartifactId\u003espring-cloud-starter-stream-rabbit\u003c/artifactId\u003e\n            \u003c/dependency\u003e\n    ```\n    即可将stream 发送到rabbitmq队列,然后turbine stream项目获取这些stream, 然后显示在图示上,这样跟之前的区别是可以监控所有\n    的微服务,而不是单个主机的stream.\n    同样打开\n    ```\n    http://localhost:8022/hystrix/\n    ```\n    在其中输入\n    ```\n    http://localhost:8989/turbine.stream\n    ```\n    则可以看到如下图所示\n    ![turbine stream](https://drive.google.com/uc?id=0BxyRSlBgU-ShSmFsdzY1ZWIxdGc)\n    如图所示可以看到 cloud-simple-service, cloud-simple-serviceB, cloud-simple-ui 共3个服务的Hystrix Stream.\n\n   1. Zipkin\n    zipkin可以跟踪微服务的调用以及,各个路径上面的时间,进而分析瓶颈.\n    打开\n    ```\n    http://localhost:9966\n    ```\n\n  trace如下图:\n    ![zipkin-simple-ui-trace](https://drive.google.com/uc?id=0BxyRSlBgU-Shb3Bab2sxN1lUSE0)\n    dependencies如下图:\n    ![zipkin-dependencies](https://drive.google.com/uc?id=0BxyRSlBgU-ShLWJDRGd1VUhMWHc)\n\n\n  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzpng%2Fspring-cloud-microservice-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzpng%2Fspring-cloud-microservice-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzpng%2Fspring-cloud-microservice-examples/lists"}