{"id":13600886,"url":"https://github.com/Allenxuxu/microservices","last_synced_at":"2025-04-11T01:30:32.138Z","repository":{"id":50634190,"uuid":"184550559","full_name":"Allenxuxu/microservices","owner":"Allenxuxu","description":"micro 微服务实例教程，包含JWT鉴权、熔断、监控、链路追踪、健康检查、跨域等","archived":false,"fork":false,"pushed_at":"2019-12-27T03:23:32.000Z","size":241,"stargazers_count":388,"open_issues_count":4,"forks_count":80,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-06T09:07:58.360Z","etag":null,"topics":["go-micro","micro","microservices","tutorial"],"latest_commit_sha":null,"homepage":"https://note.mogutou.xyz/category/go-micro","language":"Go","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/Allenxuxu.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}},"created_at":"2019-05-02T08:58:52.000Z","updated_at":"2025-01-03T21:48:02.000Z","dependencies_parsed_at":"2022-08-28T18:01:14.464Z","dependency_job_id":null,"html_url":"https://github.com/Allenxuxu/microservices","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Allenxuxu%2Fmicroservices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Allenxuxu%2Fmicroservices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Allenxuxu%2Fmicroservices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Allenxuxu%2Fmicroservices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Allenxuxu","download_url":"https://codeload.github.com/Allenxuxu/microservices/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248324967,"owners_count":21084840,"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":["go-micro","micro","microservices","tutorial"],"created_at":"2024-08-01T18:00:50.450Z","updated_at":"2025-04-11T01:30:31.693Z","avatar_url":"https://github.com/Allenxuxu.png","language":"Go","readme":"\n# microservices\n使用micro微服务框架的一些例子，包含微服务开发中必备的鉴权，熔断，监控，链路追踪，跨域等\n\n## 主要内容\n- 扩展micro的API网关功能\n  - 跨域\n  - JWT鉴权\n  - 熔断\n  - prometheus监控\n  - 链路追踪\n- gin开发微服务service\n  - 熔断\n  - prometheus监控\n  - 链路追踪\n- 内部服务采用grpc通信\n- 服务健康检查\n### 实现详解\n各部分实现，都在博客中有相应博文讲解\n[个人博客](https://note.mogutou.xyz/category/go-micro)\n\n## 跨域配置\n\n要实现跨域，只需要在 micro api（micro API 网关）注册相关插件，并且通过命令行指定跨域相关信息即可。\n\n```go\nplugin.Register(cors.NewPlugin())\n```\n\n```bash\nmicro api \n--cors-allowed-headers=X-Custom-Header,X-Header \n--cors-allowed-origins=someotherdomain.com,xx.com \n--cors-allowed-methods=POST\n```\n\n\u003e 本项目使用consul的K/V存储来模拟一个配置中心。在每一个使用到lib/token包的服务都会在main函数里传递consul地址和JWT私钥的加载路径。在srv/user服务中同样，使用consul K/V 来存储mysql数据库的配置。在启动服务之前，需要现在consul的K/V中设置好这些配置。\n\n![image](https://github.com/Allenxuxu/microservices/raw/master/.screenshots/jwt-config.png)\n![image](https://github.com/Allenxuxu/microservices/raw/master/.screenshots/mysql-config.png)\n\n## 使用到的其他软件\n- consul (服务发现,K/V配置)\n- prometheus (监控)\n- jaeger (链路追踪)\n- hystrix-dashboard (hystrix熔断仪表盘)\n- mysql \n\n\u003e prometheus 参考配置文件  \n```\nglobal:\n  scrape_interval: 15s\n  scrape_timeout: 10s\n  evaluation_interval: 15s\nalerting:\n  alertmanagers:\n  - static_configs:\n    - targets: []\n    scheme: http\n    timeout: 10s\nscrape_configs:\n- job_name: APIGW\n  honor_timestamps: true\n  scrape_interval: 15s\n  scrape_timeout: 10s\n  metrics_path: /metrics\n  scheme: http\n  static_configs:\n  - targets:\n    - 10.104.34.106:8080   #10.104.34.106为本机ip， 本机127.0.0.1在容器中无法访问到\n```\n\n### docker启动参考命令\n- consul\n  \u003e docker run --name consul -d -p 8500:8500/tcp consul agent -server -ui -bootstrap-expect=1 -client=0.0.0.0\n- prometheus  （启动时依赖本机配置文件 `/tmp/conf.yml` , 可更改命令自定义路径）\n  \u003e docker run --name prometheus  -d -p 0.0.0.0:9090:9090 -v /tmp/conf.yml:/etc/prometheus/prometheus.yml   prom/prometheus\n- jaeger\n  \u003e docker run -d --name jaeger \\\n  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \\\n  -p 5775:5775/udp \\\n  -p 6831:6831/udp \\\n  -p 6832:6832/udp \\\n  -p 5778:5778 \\\n  -p 16686:16686 \\\n  -p 14268:14268 \\\n  -p 9411:9411 \\\n  jaegertracing/all-in-one:1.6\n- hystrix-dashboard\n  \u003e docker run --name hystrix-dashboard -d -p 8081:9002 mlabouardy/hystrix-dashboard:latest\n\n  \u003e hystrix数据监控\n    http://localhost:8081/hystrix.stream\n\n- mysql\n  \u003e docker run --name mysql -e  MYSQL_ROOT_PASSWORD=123 -d -p 3306:3306 mysql\n\n\n## 快速体验\n- 使用docker 启动consul jaeger hystrix-dashboard (上面有参考命令，复制粘贴执行即可)\n\n- 打开浏览器，进入 http://localhost:8500，进入K/V存储设置JWT私钥配置(参考上面的截图)\n\n- jaeger UI http://localhost:16686\n\n- hystrix-dashboard UI http://localhost:8081/hystrix, 输入 http://{ip}:81/hystrix.stream , 此处ip为本机ip，因为hystrix-dashboard是容器启动的，无法直接访问本机127.0.0.1\n\n- 启动API网关： \n  ```\n  cd micro \u0026\u0026 make run\n  ```\n- 启动user API服务： \n  ```\n  cd api/user \u0026\u0026  make run\n  ```\n- 启动hello 服务： \n  ```\n  cd srv/hello \u0026\u0026 make run\n  ```\n- 浏览器访问 http://127.0.0.1:8080/user/test ，或者使用其他工具 GET 127.0.0.1:8080/user/test\n\n![image](https://github.com/Allenxuxu/microservices/raw/master/.screenshots/consul-service.png)\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAllenxuxu%2Fmicroservices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAllenxuxu%2Fmicroservices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAllenxuxu%2Fmicroservices/lists"}