├── ABOUT.md ├── CHANGELOG.md ├── CONTRIBUTING.md ├── CONTRIBUTORS.md ├── LICENSE.md ├── README.md ├── README_CN.md ├── READMORE ├── deploy-via-script.md ├── install-docker.md ├── netflix-eureka.md ├── netflix-feign.md ├── netflix-hystrix.md ├── netflix-ribbon.md ├── netflix-zuul.md └── spring-cloud-config.md ├── READMORE_CN ├── deploy-via-script.md ├── install-docker.md ├── netflix-eureka.md ├── netflix-feign.md ├── netflix-hystrix.md ├── netflix-ribbon.md ├── netflix-zuul.md └── spring-cloud-config.md ├── image ├── app-lifecycle.png ├── business-architecture.png ├── components-architecture.png ├── config-ha.png ├── fault-tolerance.png ├── logs-collection.png ├── monitoring-system.png ├── piggymetrics-k8s.png ├── pm-spring-cloud-config.png ├── production-environment.png └── register-and-discovery-mechanism.png └── yaml ├── deployment ├── account-mongodb-deployment.yaml ├── account-service-deployment.yaml ├── auth-mongodb-deployment.yaml ├── auth-service-deployment.yaml ├── config-deployment.yaml ├── gateway-deployment.yaml ├── monitoring-deployment.yaml ├── notification-mongodb-deployment.yaml ├── notification-service-deployment.yaml ├── rabbitmq-deployment.yaml ├── registry-deployment.yaml ├── statistics-mongodb-deployment.yaml └── statistics-service-deployment.yaml ├── dns └── dns-addon.yaml └── svc ├── account-mongodb-svc.yaml ├── account-service-svc.yaml ├── auth-mongodb-svc.yaml ├── auth-service-svc.yaml ├── config-svc.yaml ├── gateway-svc.yaml ├── monitoring-svc.yaml ├── notification-mongodb-svc.yaml ├── notification-service-svc.yaml ├── rabbitmq-svc.yaml ├── registry-svc.yaml ├── statistics-mongodb-svc.yaml └── statistics-service-svc.yaml /ABOUT.md: -------------------------------------------------------------------------------- 1 | ![20170421149274059411420.png](http://oe5ahutux.bkt.clouddn.com/20170421149274059411420.png) 2 | 3 | * [Changelog EN](#EN) 4 | * [Changelog 中文](#CN) 5 | 6 | # About Cloudframeworks 7 | 8 | Cloudframeworks (云框架) is 'plug&play'able cloud-native frameworks, combine knowledge structures with typical **examples** to help developers quickly master new technologies. Just replace some code of Cloudframeworks project to apply best practices in production environment and generate value immediately. 9 | 10 | Cloudframework is designed to help the learning and using of new technologies, and put what you have learn to good use. With Cloudframeworks, developers who do not understand the technology can quickly grasp the technical principles and know the using; developers who already know the technology can apply the technology in production environment with minimum cost. 11 | 12 | Cloudframeworks provides: 13 | 14 | * Learning path - show technical points and knowledge systematically 15 | * Best Practice - solving the practical problems of the production environment 16 | * Good experience - learn code and documentation through real example 17 | 18 | ### Directory of Repo 19 | 20 | ``` 21 | Cloudframeworks 22 | |-- user-guide 23 |    |--README.md 24 |    |--CHANGELOG.md 25 |    |--LICENSE.md 26 |    |--CONTRIBUTING.md 27 |    |--CONTRIBUTORS.md 28 |    |--ABOUT.md 29 |    |--images 30 |    |--READMORE 31 | |-- code 1 32 | |-- code 2 33 | |-- code 3 34 | `-- code n     35 | ``` 36 | 37 | ### user-guide 38 | 39 | Every Cloudframeworks topic is an organization, includes "user-guide-xx" and one or multiple code repositories. 40 | 41 | You can find documents you need in user-guide-xxx repository, and quick start with README. 42 | 43 | ## Contributing 44 | 45 | [How to contribute](CONTRIBUTING.md) 46 | 47 | ## License 48 | 49 | Released under [Apache License 2.0](LICENSE.md) 50 | 51 | ---------- 52 | 53 | # 关于云框架 54 | 55 | 云框架(Cloudframeworks)是即插即用的云端技术框架,通过典型实例梳理知识结构,帮助开发者快速掌握新技术,仅需替换部分业务代码,即可将最佳实践应用于生产环境并立即产生价值。 56 | 57 | 云框架旨在解决新技术**学习**、**使用**、**学以致用**的问题,帮助不懂技术想学习的人快速掌握技术原理并能把技术用起来,协助懂技术想要使用的人花费最小代价将技术应用于生产环境。为用户提供: 58 | 59 | * 学习路径——打通技术要点和知识体系 60 | * 最佳实践——解决生产环境的实际问题 61 | * 良好体验——通过例子串联代码和文档 62 | 63 | ### 目录结构 64 | 65 | ``` 66 | Cloudframeworks 67 | |-- user-guide 68 |    |--README.md 69 |    |--CHANGELOG.md 70 |    |--LICENSE.md 71 |    |--CONTRIBUTING.md 72 |    |--CONTRIBUTORS.md 73 |    |--ABOUT.md 74 |    |--images 75 |    |--READMORE 76 | |-- code 1 77 | |-- code 2 78 | |-- code 3 79 | `-- code n     80 | ``` 81 | 82 | ### user-guide 83 | 84 | 云框架主题展现为Github上的组织(organization),组织中仓库(repository)包括“user-guide-xxx”及一个或多个“组件”。 85 | 86 | 在user-guide仓库中,你可以很方便的找到帮助了解和使用云框架的文档,并根据“[README](README.md)”快速开始。 87 | 88 | ## 参与贡献 89 | 90 | [如何成为云框架贡献者?](CONTRIBUTING.md) 91 | 92 | ## 版权信息 93 | 94 | 云框架遵循APACHE LICENSE 2.0协议发布,并提供免费使用。 95 | 96 | 细节参阅 [LICENSE](LICENSE.md) 97 | 98 | 99 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | * [Changelog EN](#EN) 2 | * [Changelog 中文](#CN) 3 | 4 | # [Cloud Frameworks]Microservices Architecture with Spring Cloud 5 | 6 | ## v1.5 (2017.06.23) 7 | 8 | + `NEW` One click development with Goodrain Cloudbang 9 | 10 | ## v1.5 (2017.06.16) 11 | 12 | + `NEW` README in English available 13 | 14 | ## v1.5 (2017.06.06) 15 | 16 | + `NEW` step by step development script (step 4 of local developemnt) 17 | 18 | ## v1.5 (2017.05.25) 19 | 20 | **best practices in production environment available** 21 | 22 | + `NEW` K8s deployment 23 | 24 | + `NEW` configuration center high availability 25 | 26 | + `NEW` Service registration discovery mechanism 27 | 28 | + `NEW` Fault tolerance mechanism 29 | 30 | + `NEW` Logs collection 31 | 32 | + `NEW` Monitoring 33 | 34 | + `NEW` Performance Optimization 35 | 36 | + `NEW` more related reading links 37 | 38 | ## v1.5 pre-release (2017.05.24) 39 | 40 | + `Production` K8s deployment available 41 | 42 | + `Production` Configuration center high availability, Service registration discovery mechanism, Fault tolerance mechanism, Logs collection, Monitoring 43 | 44 | + `Production` Performance Optimization 45 | 46 | ## v1.0 (2017.05.10) 47 | 48 | + `docs` docs revised 49 | 50 | ## v1.0 (2017.05.02) 51 | 52 | + `UPGRADE` 1.4.5 and cam 53 | 54 | ## v1.0 (2017.04.25) 55 | 56 | + `NEW` v1.0 released 57 | 58 | ## v0.1 (2017.04.17) 59 | 60 | + `NEW` v0.1 released 61 | 62 | ---------- 63 | 64 | # [云框架]基于Spring Cloud的微服务架构 65 | 66 | ## v1.5 (2017.06.23) 67 | 68 | + `NEW` 新增一键部署(好雨云帮) 69 | 70 | ## v1.5 (2017.06.16) 71 | 72 | + `NEW` 新增英文版README 73 | 74 | ## v1.5 (2017.06.06) 75 | 76 | + `NEW` 增加通过脚本分别部署每个组件命令(快速部署第4步) 77 | 78 | ## v1.5 (2017.05.25) 79 | 80 | **本次更新重点加入了Spring Cloud生产环境下最佳实践相关内容和经验** 81 | 82 | + `NEW` Kubernetes部署(包含说明、步骤及yaml文件) 83 | 84 | + `NEW` 配置中心高可用实现及说明 85 | 86 | + `NEW` 注册服务发现机制实现及说明 87 | 88 | + `NEW` 服务容错机制实现及说明 89 | 90 | + `NEW` 日志采集实现及说明 91 | 92 | + `NEW` 监控体系实现及说明 93 | 94 | + `NEW` 生产环境下性能优化实现及说明 95 | 96 | + `NEW` 部分文档外链及扩展阅读 97 | 98 | ## v1.5 pre-release (2017.05.24) 99 | 100 | + `生产环境` 增加K8s部署 101 | 102 | + `生产环境` 增加配置中心高可用、服务注册发现机制、服务容错机制、日志采集、监控体系 103 | 104 | + `生产环境` 增加性能优化 105 | 106 | ## v1.0 (2017.05.10) 107 | 108 | + `docs` 精简文档 109 | 110 | ## v1.0 (2017.05.02) 111 | 112 | + `UPGRADE` 更新至1.4.5 and cam版本 113 | 114 | ## v1.0 (2017.04.25) 115 | 116 | + `NEW` v1.0 发布 117 | 118 | ## v0.1 (2017.04.17) 119 | 120 | + `NEW` v0.1 发布 121 | 122 | 123 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | * [EN](#EN) 2 | * [中文](#CN) 3 | 4 | # How to contribute 5 | 6 | Read [Contributing to Open Source on GitHub](https://guides.github.com/activities/contributing-to-open-source/) before contribute. 7 | 8 | ## Bug 9 | 10 | When you find a bug, or have questions about code, documents and project, use **ISSUE** to report and discuss. 11 | 12 | ## Feature 13 | 14 | If you are able to fix bugs or add feature for Cloudframeworks, your **Pull Request** are welcome! 15 | 16 | Check Pull Request is another way to contribute. 17 | 18 | ## Documents 19 | 20 | When you find any typo or you have awesome contents to supplement, you can also use **Pull Request**. 21 | 22 | ## Simple way to contribute 23 | 24 | The fragrance always stays in the hand that gives the rose. 25 | 26 | Help people solve their problems is the easiest way to contribute. 27 | 28 | ## To be a Cloudframeworks author 29 | 30 | If you want to be a Cloudframeworks author, responsible for the creation and operation of Cloudframeworks topics, you can contact us via [Mail](mailto:info@goodrain.com) 31 | 32 | ---------- 33 | 34 | # 如何成为云框架贡献者? 35 | 36 | 如果你没有相关经验,建议花几分钟阅读一下[Contributing to Open Source on GitHub](https://guides.github.com/activities/contributing-to-open-source/)。 37 | 38 | ## Bug 39 | 40 | 如果你发现了bug,或是对代码、文档、项目有任何疑问,可以通过Issue系统来提出。 41 | 42 | ## Feature 43 | 44 | 如果你有能力修复bug或是想要为云框架增加feature,可以通过Pull Request来实现。 45 | 46 | 检查其他人的Pull Request,也是非常有益的贡献方式! 47 | 48 | ## 文档 49 | 50 | 如果你在文档中发现了笔误或是你有很棒的内容来补充文档,可以编辑并通过Pull Request提交。 51 | 52 | ## 最简单的贡献方式 53 | 54 | 赠人玫瑰,手有余香。 55 | 56 | 帮助他人解决在学习和使用云框架过程中出现的疑问,是云框架最简单的贡献方式。 57 | 58 | 当你在社群中遇到与云框架相关的问题和讨论,请不吝赐教吧! 59 | 60 | ## 成为云框架作者 61 | 62 | 如果你想要成为云框架作者,负责某一技术主题的创作和运营,可以通过[邮件](mailto:info@goodrain.com)联系我们。 63 | 64 | 65 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Contributors 2 | 3 | [How to contribute](CONTRIBUTING.md) 4 | 5 | ## Author 6 | 7 | **[elvis2002](https://github.com/elvis2002)** 8 | 9 | ## Contributor 10 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 Beijing Goodrain Technology Co.,Ltd. All right reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Cloudframeworks]Microservices Architecture with Spring Cloud v1.5 2 | 3 | ![](https://img.shields.io/badge/Release-v1.5-green.svg) 4 | [![](https://img.shields.io/badge/Producer-elvis2002-orange.svg)](CONTRIBUTORS.md) 5 | ![](https://img.shields.io/badge/License-Apache_2.0-blue.svg) 6 | 7 | **查看中文文档[README_CN](README_CN.md)** 8 | 9 | Compared with the traditional architecture model, [microservices](https://martinfowler.com/articles/microservices.html) enjoy advantages on the following aspects:language independence, independent process communication, high decoupling, fixed task boundary and on-demand expansion. It's a perfect tool for internet business which requires fast delivery, quick response and constantly testing. Tech gaints such as Twitter, Netflix, Amazon and eBay are all loyal customers of the mircoservices architecture model. 10 | 11 | At present, three mainstream microservice frameworks are named Spring Cloud, Dubbo and API Gateway. [Spring Cloud](http://projects.spring.io/spring-cloud/) is a set of tools for rapid construction of distributed systems. Thanks to Spring Boot's development convenience, Spring Cloud provides JVM cloud application which could provide a simple way forconfiguration, service discovery, circuit breakers, intelligent routing, micro-agents, control buses, global locks, decision-making campaigns, distributed sessions, cluster state management and many other functions. 12 | 13 | Compared to Dubbo and other RPC frameworks, Spring Cloud is a pretty new choice among microservice architecture frameworks. Version 1.0 was released in 2016, integrity of the Spring Cloud's programis very high, sub-projects of the program could cover almost every part of micro-service architecture. Considering its high degree of attention received and frequency of activities, Spring Cloud is likely to become the micro service architecture standard. ([Spring Cloud技术分析](http://tech.lede.com/2017/03/15/rd/server/SpringCloud0/)) 14 | 15 | This [CloudFramework](ABOUT.md) summarizes the successful experiences of dozens of micro service architecture projects in the past. Combined with a typical case [PiggyMetrics](https://github.com/cloudframeworks-springcloud/PiggyMetrics) (a personal financial management application), Spring Cloud is promised to provide best practices of Microservices Architecture. 16 | 17 | * Beginners can master Spring Cloud and microservices techniques quickly through this project, and open discussions are welcome in this community 18 | 19 | * Developers with preknowledge and relavent backgrounddo not have to start from scratch, you only need to replace some business code to apply the best practice to the production environment and value will be generated immediately 20 | 21 | Related CloudFramework: [[CloudFrameworks]KONG API Gateway](https://github.com/cloudframeworks-apigateway/user-guide-apigateway) 22 | 23 | # Overview 24 | 25 | * [Quick Deploy](#quick-deploy) 26 | * [One Click Deployment](#one-click-deployment) 27 | * [Local Deployment](#local-deployment) 28 | * [Framework details - Business](#framework-details-business) 29 | * [Framework details - Components](#framework-details-components) 30 | * [Components Architecture](#components-architecture) 31 | * [Spring Cloud Config Configuration](#spring-cloud-config) 32 | * [Netflix Eureka Configuration](#netflix-eureka) 33 | * [Netflix Zuul Configurarion](#netflix-zuul) 34 | * [Netflix Ribbon Configuration](#netflix-ribbon) 35 | * [Netflix Hystrix Configuration](#netflix-hystrix) 36 | * [Netflix Feign Configuration](#netflix-feign) 37 | * [Make It Your Own Project](#make-it-your-own-project) 38 | * [Production Environment](#production-environment) 39 | * [Roadmap](#roadmap) 40 | * [Community & Contribution](#community-and-contribution) 41 | 42 | # Quick Deploy 43 | 44 | ## One Click Deployment 45 | 46 | [One Click deployment with Rainbond](https://www.goodrain.com/#/app/detail/15) 47 | 48 | ## Local Deployment 49 | 50 | 1. [Docker preparation](./READMORE/install-docker.md) 51 | 52 | 2. Clone code 53 | 54 | ``` 55 | git clone https://github.com/cloudframeworks-springcloud/PiggyMetrics 56 | ``` 57 | 58 | 3. Environment variable configuration 59 | 60 | ``` 61 | export CONFIG_SERVICE_PASSWORD=root 62 | export NOTIFICATION_SERVICE_PASSWORD=root 63 | export STATISTICS_SERVICE_PASSWORD=root 64 | export ACCOUNT_SERVICE_PASSWORD=root 65 | export MONGODB_PASSWORD=root ## MUST 66 | ``` 67 | 68 | 4. Run the following command with [docker-compose](https://docs.docker.com/compose/install/)([docker-compose.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/docker-compose.yml))(or check [Deploy via script](./READMORE/deploy-via-script.md)) 69 | 70 | ``` 71 | docker-compose -f docker-compose.yml up -d 72 | ``` 73 | 74 | 5. Access paths 75 | 76 | http://DOCKER-HOST:80 - Gateway 77 | 78 | http://DOCKER-HOST:8761 - Eureka Dashboard 79 | 80 | http://DOCKER-HOST:9000/hystrix - Hystrix Dashboard 81 | 82 | http://DOCKER-HOST:8989 - Turbine stream (source for the Hystrix Dashboard) 83 | 84 | http://DOCKER-HOST:15672 - RabbitMq management (default account: guest/default pwd: guest) 85 | 86 | # Framework Details - Business 87 | 88 | Piggymetrics implement microservices architecture with Spring Cloud, and the application is divided into three micro-services, [ACCOUNT SERVICE](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/account-service)、[STATISTICS SERVICE](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/statistics-service)、[NOTIFICATION SERVICE](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/notification-service). Each micro-service is an independently deployable application with separate database that is organized around business capabilities, and uses synchronized [REST API](http://www.restapitutorial.com/) to communicate between micro-services. 89 | 90 | PiggyMetrics's business architecture: 91 | 92 |
93 | 94 | The account service module contains general user input logic and validation: revenue / expense items, savings, and account settings. 95 | 96 | Method | Path | Description | User Authenticated | Available from UI 97 | ------------- | ------------------------- | ------------- |:-------------:|:----------------:| 98 | GET | /accounts/{account} | Get specified account data | | 99 | GET | /accounts/current | Get current account data | × | × 100 | GET | /accounts/demo | Get demo account data (pre-filled incomes/expenses items, etc) |   | × 101 | PUT | /accounts/current | Save current account data | × | × 102 | POST | /accounts/ | Register new account |   | × 103 | 104 | The statistics service module performs the calculation of the main statistical parameters and captures the time series of each account. 105 | 106 | Method | Path | Description | User Authenticated | Available from UI 107 | ------------- | ------------------------- | ------------- |:-------------:|:----------------:| 108 | GET | /statistics/{account} | Get specified account statistics         | | 109 | GET | /statistics/current | Get current account statistics | × | × 110 | GET | /statistics/demo | Get demo account statistics |   | × 111 | PUT | /statistics/{account} | Create or update time series datapoint for specified account | | 112 | 113 | The notification service module stores user contact information and notification settings (such as reminders and backup frequencies), and the program staff collects the required information from other services and sends an e-mail to the subscribed customers. 114 | 115 | Method | Path | Description | User Authenticated | Available 116 | ------------- | ------------------------- | ------------- |:-------------:|:----------------:| 117 | GET | /notifications/settings/current | Get current account notification settings | × | × 118 | PUT | /notifications/settings/current | Save current account notification settings | × | × 119 | 120 | # Framework Details Components 121 | 122 | Piggymetrics uses Spring Cloud Config、Netflix Eureka、Netflix Hystrix、Netflix Zuul、Netflix Ribbon、Netflix Feign in its infrastructure service, these are also core components of Spring Cloud distributed development. 123 | 124 | Components architecture is shown below: 125 | 126 |
127 | 128 | * The account service invokes the statistics service and notification service through the remote client (Feign), implements load balancing through the Ribbon, and adds the function of the circuit breaker (Hystrix) during the call; 129 | 130 | * Because the service can be called after the discovery, so account services, statistics services, notification services discover (mutual discovery) each other through the registration center (Eureka); 131 | 132 | * API Gateway (Zuul) provides a unified service gateway, it get corresponding service from the registration center (Eureka), and then call the real business logic according to the service; 133 | 134 | * The service call process unifies all break information through the Turbine; 135 | 136 | * The entire business process of all services in the configuration file through the Spring Cloud Config to manage, such as what port or what parameters to config; 137 | 138 | * The authentication mechanism is implemented through the Auth service to provide basic authentication services. 139 | 140 | * Spring Cloud Config, Eureka, Ribbon, Hystrix, Feign, and Turbine are standard components, and there is no strong relationship between them and business, not related to business code, simply configure to work. 141 | 142 | ## Spring Cloud Config Configuration 143 | 144 | In PiggyMetrics,[config_server](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/config) load configuration file from local classpath: 145 | 146 |
147 | 148 | We can check Shard directory resource in [config service](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/config/src/main/resources/shared), where `application.yml` is shared by all client applications, for example, when Notification-service request configuration, use `shared/notification-service.yml` and `shared/application.yml` to config service responses. 149 | 150 | To use Spring Cloud config, we need add spring-cloud-starter-config (it will get configurations from config center automatically) into [pom.xml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/config/pom.xml) and add following code into the resource directory bootstrap.yml, for example add into [monitoring bootstrap.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/monitoring/src/main/resources/bootstrap.yml): 151 | 152 | ``` 153 | spring: 154 | application: 155 | name: service name 156 | cloud: 157 | config: 158 | uri: http://config:8888 159 | fail-fast: true 160 | ``` 161 | 162 | After that, we can refresh configurations through http://DOCKER-HOST:DOCKER-PORT/xxx/refresh (xxx is service root path) without restart. 163 | 164 | **[Read more about Spring Cloud Config](./READMORE/spring-cloud-config.md)** 165 | 166 | ## Netflix Eureka Configuration 167 | 168 | PiggyMetrics uses Eureka server for [registy](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/registry), code logic is simple and standard, we don't need to make any changes, but should be noted to add configuration center service address information into [bootstrap.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/registry/src/main/resources/bootstrap.yml). 169 | 170 | ``` 171 | spring: 172 | cloud: 173 | config: 174 | uri: http://config:8888 175 | fail-fast: true 176 | password: ${CONFIG_SERVICE_PASSWORD} 177 | username: user 178 | ``` 179 | 180 | **[Read more about Netflix Eureka](./READMORE/netflix-eureka.md)** 181 | 182 | ## Netflix Zuul Configuration 183 | 184 | PiggyMetrics implements [gateway](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/gateway), proxy authorization service, account service, statistics service and notification service, with Netflix Zuul. The code is simple and standard, no need to make any changes. 185 | 186 | In development, replace the corresponding service in [GatewayApplication.java](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/gateway/src/main/java/com/piggymetrics/gateway/GatewayApplication.java). 187 | 188 | ``` 189 | @EnableZuulProxy ## add zuul proxy 190 | public class GatewayApplication { 191 | public static void main(String[] args) { 192 | SpringApplication.run(GatewayApplication.class, args); 193 | } 194 | } 195 | ``` 196 | 197 | Add [static](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/gateway/src/main/resources/static) in resources directory to restore yout statics resources (html、css、images etc.). 198 | 199 | Add proxy service configuration in Zuul's configuration file [gateway.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/config/src/main/resources/shared/gateway.yml). 200 | 201 | ``` 202 | zuul: 203 | ignoredServices: '*' 204 | host: 205 | connect-timeout-millis: 20000 ## overtime 206 | ocket-timeout-millis: 20000 207 | routes: 208 | auth-service: ## authroization serive 209 | path: /uaa/** ## mathcing path 210 | url: http://auth-service:5000 ## service path(http) 211 | stripPrefix: false ## with prefix or not 212 | sensitiveHeaders: 213 | account-service: 214 | path: /accounts/** 215 | serviceId: account-service ## dynamic search through service ID 216 | stripPrefix: false 217 | sensitiveHeaders: 218 | statistics-service: 219 | path: /statistics/** 220 | serviceId: statistics-service 221 | stripPrefix: false 222 | sensitiveHeaders: 223 | notification-service: 224 | path: /notifications/** 225 | serviceId: notification-service 226 | stripPrefix: false 227 | sensitiveHeaders: 228 | ``` 229 | 230 | **[Read more about Netflix Zuul](./READMORE/netflix-zuul.md)** 231 | 232 | ## Netflix Ribbon Configuration 233 | 234 | PiggyMetrics does not explicitly define the use of Netflix Ribbon, but in Zuul, Feign and other components use Ribbon implicitly. In actual development, no need to deliberately define the Ribbon. 235 | 236 | **[Read more about Netflix Ribbon](./READMORE/netflix-ribbon.md)** 237 | 238 | ## Netflix Hystrix Configuration 239 | 240 | The project defined a unity of circuit breaker mechanism (without code intrusion): 241 | 242 | ``` 243 | hystrix: 244 | command: 245 | default: 246 | execution: 247 | isolation: 248 | thread: 249 | timeoutInMilliseconds: 10000 ## 10000ms overtime limiting 250 | ``` 251 | 252 | Since Hystrix's monitoring is only for single node, PiggyMetrics monitors the Hystrix metrics situation under the cluster through Netflix Turbine. 253 | 254 | Add following code into client, client will be able to push Hystrix command to Turbine, for example [/notification-service/pom.xml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/notification-service/pom.xml#L79). 255 | 256 | ``` 257 | 258 | org.springframework.cloud 259 | spring-cloud-netflix-hystrix-stream 260 | 261 | ``` 262 | 263 | **[Read more about Netflix Hystrix](./READMORE/netflix-hystrix.md)** 264 | 265 | ## Netflix Feign Configuration 266 | 267 | PiggyMetrics uses Feign many times by adding following code into client, for example [StatisticsServiceClient.java](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/account-service/src/main/java/com/piggymetrics/account/client/StatisticsServiceClient.java). 268 | 269 | ``` 270 |   @FeignClient(name = "auth-service")     ## declare a client authentication service client, find auth-service through the registry 271 | public interface AuthServiceClient { 272 | 273 | @RequestMapping(method = RequestMethod.POST, value = "/uaa/users", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) 274 | void createUser(User user); 275 | 276 | } 277 | 278 | ``` 279 | 280 | Feign can also appoint external services, for example in statistics module, Feign appointed an [ExchangeRatesClient.java](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/statistics-service/src/main/java/com/piggymetrics/statistics/client/ExchangeRatesClient.java). 281 | 282 | ``` 283 | @FeignClient(url = "${rates.url}", name = "rates-client") ## declare a exchange rate client, based on specific url (can be external service) 284 | public interface ExchangeRatesClient { 285 | 286 | @RequestMapping(method = RequestMethod.GET, value = "/latest") 287 | ExchangeRatesContainer getRates(@RequestParam("base") Currency base); 288 | 289 | } 290 | ``` 291 | 292 | **[Read more about Netflix Feign](./READMORE/netflix-feign.md)** 293 | 294 | # Make It Your Own Project 295 | 296 | 1. git clone, and create mvn project based on this CloudFramework 297 | 298 | 2. replace auth-service、account-service、notification-service、statistics-service with your own services 299 | 300 | * *no need to modify code of config、registry、gateway and monitoring* 301 | 302 | 3. Modify configuration in config, such as service name and port 303 | 304 | 4. Generate mvn and build image 305 | 306 | 5. run all images [Quick Deploy](#quick-deploy) 307 | 308 | # Production Environment 309 | 310 | The application and services in the production environment need to meet the following basic characteristics: 311 | 312 |   1. Part of the software, hardware or network anomalies, the application is still able to work reliably 313 | 314 |   2. Multi-user support and applications continue to work 315 | 316 |   3. Can add or remove resources to adapt to different needs and changes 317 | 318 |   4. Easy to deploy and monitor 319 | 320 | In other words, in the production environment, we need to consider more and more complex factors to meet the actual business and support business characteristics. 321 | 322 | For the micro service architecture, it is recommended to use `Docker`+`Kubernetes` PaaS, reasonable structure is as follows: 323 | 324 |
325 | 326 | [Why Docker?](https://yeasy.gitbooks.io/docker_practice/content/introduction/why.html) 327 | 328 | [Why Kubernetes?](https://blog.gcp.expert/kubernetes-gke-introduction/) 329 | 330 | [Container, Docker, and Kubernetes](https://collectiveidea.com/blog/archives/2017/01/27/containers-docker-and-kubernetes-part-1) 331 | 332 | ## Install Kubernetes 333 | 334 | Kubernetes offers a variety of detailed installation methods, proposed reference: 335 | 336 | * [Kubernetes:Picking the Right Solution](https://kubernetes.io/docs/setup/pick-right-solution/) 337 | 338 | * [Kubernetes學習筆記](https://gcpug-tw.gitbooks.io/kuberbetes-in-action/content/) 339 | 340 | ## Deploy application on Kubernetes 341 | 342 | Service Orchestration is the process of designing, creating and delivering end-to-end services, often discuss with service-oriented architecture, virtualization, configuration, converged infrastructure, dynamic data centers, and so on. The most popular service Orchestration tool is Kubernetes (K8s). 343 | 344 | Kubernetes combines the containers that make up the application into logical units for easy management and discovery. It provides resource scheduling, service discovery, operational monitoring, expansion and contraction, load balancing, gray scale upgrade, failure redundancy, disaster recovery, DevOps and a series of options to help achieve large-scale, distributed, Highly available Docker cluster, to solve the business of distributed architecture, service design, a complete definition of the business system to build a standardized architecture layer, that is, Cluster, Node, Pod, Label and a series of abstract are defined, for the service The choreography provides a simple, lightweight way. [Kubernetes: a platform for automating deployment, scaling, and operations](https://www.slideshare.net/BrianGrant11/wso2con-us-2015-kubernetes-a-platform-for-automating-deployment-scaling-and-operations) 345 | 346 | Before deploying application on Kubernetes, we need to understand the lifecycle of application. 347 | 348 |
349 | 350 | * Use Git to manage code version (with Git being distributed, [Git vs SVN](http://stackoverflow.com/questions/871/why-is-git-better-than-subversion)) 351 | 352 | * Clear the construction rules 353 | 354 | * Use Image or Binary for application package management 355 | 356 | * Make a service deployment rule that includes development, testing, production, and auditing 357 | 358 | * Choose the appropriate release mechanism according to the actual business needs.([gray release, AB test, blue and green deployment, canary deployment](http://blog.christianposta.com/deploy/blue-green-deployments-a-b-testing-and-canary-releases/)) 359 | 360 | **Deploy PiggyMetrics on Kubernetes** 361 | 362 | [Check PiggyMetrics application architecture](./image/piggymetrics-k8s.png) 363 | 364 | [Check PiggyMetrics Yaml files](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml) 365 | 366 | **Steps:** 367 | 368 | 1. install Kubernetes and Docker 369 | 370 | 2. create namespace 371 | 372 | ``` 373 | kubectl -s 127.0.0.1:8080 create namespace springcloud 374 | ``` 375 | 376 | 3. configure containers' DNS ([Check dns/dns-addon.yaml](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml/dns)) 377 | 378 | ``` 379 | kubectl -s 127.0.0.1:8080 create -f dns/dns-addon.yaml file --namespace=springcloud 380 | ``` 381 | 382 | 4. create services ([Check svc/yaml file](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml/svc)) 383 | 384 | ``` 385 | kubectl -s 127.0.0.1:8080 create -f svc/yaml file --namespace=springcloud 386 | ``` 387 | 388 | 5. create application deployment ([Check deployment/yaml file](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml/deployment)) 389 | 390 | ``` 391 | kubectl -s 127.0.0.1:8080 create -f deployment/yaml file --namespace=springcloud 392 | ``` 393 | 394 | Remarks: 395 | 396 | 127.0.0.1:8080----kubernetes api server 397 | 398 | svc/yaml file----svc deploy yaml file 399 | 400 | deployment/yaml file----deployment deploy yaml file 401 | 402 | ## Implementation of features 403 | 404 | ### Configuration center high availability 405 | 406 | In the production environment, services read configuration file from the configuration center, and the configuration center reads the configuration file from Gitlab, making the configuration center a clustered microservice to achieve high availability and meet the needs of a large number of services. 407 | 408 | The structure is as follows: 409 | 410 |
411 | 412 | ### Service registration discovery mechanism 413 | 414 | In the production environment, service registration discovery management is carried out using Eureka Server, and **three peer nodes are used to register both for high availability**. 415 | 416 | The structure is as follows: 417 | 418 |
419 | 420 | ### Fault tolerance mechanism 421 | 422 | Implement circuit breaker with Hystrix, and get graphical dispaly with Hystrix Dashboard, and use RabbitMQ to change the default "pull" into MQ message queue through the "push" mode to ensure simple structure and real-time efficiency. 423 | 424 | The structure is as follows: 425 | 426 |
427 | 428 | ### Logs collection 429 | 430 | Logs are collected and displayed via EFKA (elasticsearch fluentd kibana kafka) in production environment. 431 | 432 | The structure is as follows: 433 | 434 |
435 | 436 | ### Monitoring 437 | 438 | Get log infomations through the real-time log collection system, and use time window technology for log analysis. 439 | 440 | The structure is as follows: 441 | 442 |
443 | 444 | ## Spring Cloud Performance Optimization 445 | 446 | In production environment, follow the following tips to do performance optimization: 447 | 448 | 1、configuration center optimization 449 | 450 | eureka: 451 | instance: 452 | prefer-ip-address: true 453 | client: 454 | registerWithEureka: false 455 | fetchRegistry: false 456 | server: 457 | waitTimeInMsWhenSyncEmpty: 0 458 | server 459 | eviction-interval-timer-in-ms: 4000 460 | enableSelfPreservation: false 461 | renewalPercentThreshold: 0.9 462 | 463 | 2、zuul configuration optimization 464 | 465 | ribbon: 466 | ReadTimeout: 20000 467 | ConnectTimeout: 20000 468 | MaxAutoRetries: 1 469 | 470 | zuul: 471 | host: 472 | connect-timeout-millis: 20000 473 | socket-timeout-millis: 20000 474 | 475 | 3、Feign configuration optimization 476 | 477 | #request and response GZIP compress 478 | feign.compression.request.enabled=true 479 | feign.compression.response.enabled=true 480 | #compress supportive mime types 481 | feign.compression.request.enabled=true 482 | feign.compression.request.mime-types=text/xml,application/xml,application/json 483 | feign.compression.request.min-request-size=2048 484 | 485 | # Roadmap 486 | 487 | * `README` add related information links 488 | * `Component` add detailed information for components 489 | * `FAQ` add questions and answers 490 | 491 | [CHANGELOG](CHANGELOG.md) 492 | 493 | # Community & Contribution 494 | 495 | + QQ GROUP: 531980120 496 | + [CONTRIBUTING](CONTRIBUTING.md) 497 | + [CONTACT US](mailto:info@goodrain.com) 498 | 499 | ------- 500 | 501 | A [CloudFrameworks](ABOUT.md) project, released under the [APACHE LICENSE 2.0](LICENSE.md) 502 | 503 | 504 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # [云框架]基于Spring Cloud的微服务架构 v1.5 2 | 3 | ![](https://img.shields.io/badge/Release-v1.5-green.svg) 4 | [![](https://img.shields.io/badge/Producer-elvis2002-orange.svg)](CONTRIBUTORS.md) 5 | ![](https://img.shields.io/badge/License-Apache_2.0-blue.svg) 6 | 7 | **README in English available, see [README_EN](README.md)** 8 | 9 | [微服务](https://martinfowler.com/articles/microservices.html)与传统架构模式相比,具有语言无关性、独立进程通讯、高度解耦、任务边界固定、按需扩展等特点,非常适合互联网公司快速交付、响应变化、不断试错的需求,也因此受到了像Twitter、Netflix、Amazon、eBay这样的科技巨头的青睐。 10 | 11 | 目前主流微服务框架包括Spring Cloud、Dubbo、API Gateway等,其中[Spring Cloud](http://projects.spring.io/spring-cloud/)是一套快速构建分布式系统中常见工具的集合,利用Spring Boot的开发便利性,Spring Cloud为JVM云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的实现方式。 12 | 13 | 相比Dubbo等RPC框架,Spring Cloud是一个比较新的微服务基础框架选择,2016年才推出的1.0 release版本,不过Spring Cloud的方案完整度非常高,各个子项目几乎覆盖了微服务架构的方方面面。从目前的关注度和活跃度来看,Spring Cloud很可能会成为微服务架构的标准。([Spring Cloud技术分析](http://tech.lede.com/2017/03/15/rd/server/SpringCloud0/)) 14 | 15 | 本篇[云框架](ABOUT.md)总结过去数十个微服务架构项目的成功经验,并结合典型案例[PiggyMetrics](https://github.com/cloudframeworks-springcloud/PiggyMetrics)(一款个人财务管理应用),为开发者提供基于Spring Cloud的微服务架构落地最佳实践。 16 | 17 | * 初学者可通过实例代码、文档快速学习Spring Cloud及微服务,并在社群中交流讨论 18 | * 已有一定了解的开发者,不必从零开始开发,仅需在云框架基础上替换部分业务代码,即可将最佳实践应用于生产环境并立即产生价值 19 | 20 | 相关云框架:[[云框架]KONG API Gateway](https://github.com/cloudframeworks-apigateway/user-guide-apigateway) 21 | 22 | # 内容概览 23 | 24 | * [快速部署](#快速部署) 25 | * [一键部署](#一键部署) 26 | * [本地部署](#本地部署) 27 | * [框架说明-业务](#框架说明-业务) 28 | * [框架说明-组件](#框架说明-组件) 29 | * [组件架构](#组件架构) 30 | * [配置Spring Cloud Config](#Spring-Cloud-Config) 31 | * [配置Netflix Eureka](#Netflix-Eureka) 32 | * [配置Netflix Zuul](#Netflix-Zuul) 33 | * [配置Netflix Ribbon](#Netflix-Ribbon) 34 | * [配置Netflix Hystrix](#Netflix-Hystrix) 35 | * [配置Netflix Feign](#Netflix-Feign) 36 | * [如何变成自己的项目](#如何变成自己的项目) 37 | * [生产环境](#生产环境) 38 | * [更新计划](#更新计划) 39 | * [社群贡献](#社群贡献) 40 | 41 | # 快速部署 42 | 43 | ## 一键部署 44 | 45 | [一键部署至Rainbond](https://www.goodrain.com/#/app/detail/15) 46 | 47 | ## 本地部署 48 | 49 | 1. [准备Docker环境](./READMORE_CN/install-docker.md) 50 | 51 | 2. 克隆完整代码 52 | 53 | ``` 54 | git clone https://github.com/cloudframeworks-springcloud/PiggyMetrics 55 | ``` 56 | 57 | 3. 设置环境变量 58 | 59 | ``` 60 | export CONFIG_SERVICE_PASSWORD=root 61 | export NOTIFICATION_SERVICE_PASSWORD=root 62 | export STATISTICS_SERVICE_PASSWORD=root 63 | export ACCOUNT_SERVICE_PASSWORD=root 64 | export MONGODB_PASSWORD=root ## 必填,其他变量可不设置 65 | ``` 66 | 67 | 4. 使用[docker-compose](https://docs.docker.com/compose/install/)运行如下命令([docker-compose.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/docker-compose.yml))(或查看[通过脚本分别部署每个组件](./READMORE_CN/deploy-via-script.md)) 68 | 69 | ``` 70 | docker-compose -f docker-compose.yml up -d 71 | ``` 72 | 73 | 5. 访问路径 74 | 75 | http://DOCKER-HOST:80 - Gateway 76 | 77 | http://DOCKER-HOST:8761 - Eureka Dashboard 78 | 79 | http://DOCKER-HOST:9000/hystrix - Hystrix Dashboard 80 | 81 | http://DOCKER-HOST:8989 - Turbine stream (source for the Hystrix Dashboard) 82 | 83 | http://DOCKER-HOST:15672 - RabbitMq management (默认账号guest/默认密码guest) 84 | 85 | # 框架说明-业务 86 | 87 | Piggymetrics通过Spring Cloud实现微服务架构,应用被分解为**账户服务**([ACCOUNT SERVICE](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/account-service))、**统计服务**([STATISTICS SERVICE](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/statistics-service))、**通知服务**([NOTIFICATION SERVICE](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/notification-service))等三个核心微服务。每个微服务都是围绕业务能力组织的可独立部署的应用程序,拥有独立的数据库并使用同步的[REST API](http://www.restapitutorial.com/)实现微服务与微服务之间的通信。 88 | 89 | PiggyMetrics业务架构如下图所示: 90 | 91 |
92 | 93 | 账户服务模块包含一般用户输入逻辑和验证:收入/费用项目,储蓄和帐户设置。 94 | 95 | 方法 | 路径 | 描述 | 用户验证 | UI可用 96 | ------------- | ------------------------- | ------------- |:-------------:|:----------------:| 97 | GET | /accounts/{account} | 获取特定账户数据 | | 98 | GET | /accounts/current | 获取当前账户数据 | × | × 99 | GET | /accounts/demo | 获取demo账户数据 (预填充收入/支出项目等) |   | × 100 | PUT | /accounts/current | 保存当前账户数据 | × | × 101 | POST | /accounts/ | 注册新账户 |   | × 102 | 103 | 统计服务模块执行主要统计参数的计算,并捕获每个帐户的时间序列。 104 | 105 | 方法 | 路径 | 描述 | 用户验证 | UI可用 106 | ------------- | ------------------------- | ------------- |:-------------:|:----------------:| 107 | GET | /statistics/{account} | 获取特定账户统计         | | 108 | GET | /statistics/current | 获取当前账户统计 | × | × 109 | GET | /statistics/demo | 获取demo账户统计 |   | × 110 | PUT | /statistics/{account} | 创建或更新时间系列数据点指定的帐户 | | 111 | 112 | 通知服务模块存储用户联系信息和通知设置(如提醒和备份频率),计划工作人员从其他服务收集所需的信息,并向订阅的客户发送电子邮件。 113 | 114 | 方法 | 路径 | 描述 | 用户验证 | UI可用 115 | ------------- | ------------------------- | ------------- |:-------------:|:----------------:| 116 | GET | /notifications/settings/current | 获取当前账户通知设置 | × | × 117 | PUT | /notifications/settings/current | 保存当前账户通知设置 | × | × 118 | 119 | # 框架说明-组件 120 | 121 | Piggymetrics基础服务设施中用到了Spring Cloud Config、Netflix Eureka、Netflix Hystrix、Netflix Zuul、Netflix Ribbon、Netflix Feign等组件,而这也正是Spring Cloud分布式开发中最核心的组件。 122 | 123 | 组件架构如下图所示: 124 | 125 |
126 | 127 | * 账户服务通过远程客户端(Feign)调用统计服务及通知服务,通过Ribbon实现负载均衡,并在调用过程中增加了断路器(Hystrix)的功能; 128 | 129 | * 由于服务发现后才能调用,因此账户服务、统计服务、通知服务通过注册中心(Eureka)实现互相发现; 130 | 131 | * API Gateway(Zuul)提供对外统一的服务网关,首先从注册中心(Eureka)处获取相应服务,再根据服务调用各个服务的真实业务逻辑; 132 | 133 | * 服务调用过程通过聚合器(Turbine)统一所有断路信息; 134 | 135 | * 整个业务过程中所有服务的配置文件通过Spring Cloud Config来管理,即起什么端口、配置什么参数等; 136 | 137 | * 认证机制通过Auth service实现,提供基本认证服务; 138 | 139 | * Spring Cloud Config、Eureka、Ribbon、Hystrix、Feign以及Turbine均为标准组件,与业务之间没有强关系,不涉及到业务代码,仅需简单配置即可工作。 140 | 141 | ## 配置Spring Cloud Config 142 | 143 | 在PiggyMetrics项目中,[config_server](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/config)从本地类路径加载配置文件: 144 | 145 |
146 | 147 | 我们可以在[config service](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/config/src/main/resources/shared)中查看shard目录资源,其中`application.yml`被所有客户端应用共享,比如当Notification-service请求配置时,使用`shared/notification-service.yml`和`shared/application.yml`配置服务响应。 148 | 149 | 使用Spring Cloud config需要在[pom.xml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/config/pom.xml)中添加spring-cloud-starter-config(它将从配置中心自动获取配置),并在各服务资源目录bootstrap.yml中,例如[moinitoring的bootstrap.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/monitoring/src/main/resources/bootstrap.yml)中添加如下代码: 150 | 151 | ``` 152 | spring: 153 | application: 154 | name: 服务名 155 | cloud: 156 | config: 157 | uri: http://config:8888 158 | fail-fast: true 159 | ``` 160 | 161 | 配置文件修改后可通过 http://DOCKER-HOST:DOCKER-PORT/xxx/refresh 刷新配置(xxx表示服务根路径),无需重启服务。 162 | 163 | **[进一步了解Spring Cloud Config](./READMORE_CN/spring-cloud-config.md)** 164 | 165 | ## 配置Netflix Eureka 166 | 167 | PiggyMetrics通过Eureka server实现[registy](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/registry), 代码逻辑比较简单和标准,不用做任何修改,需要注意的是在[bootstrap.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/registry/src/main/resources/bootstrap.yml)加入配置中心服务地址信息。 168 | 169 | ``` 170 | spring: 171 | cloud: 172 | config: 173 | uri: http://config:8888 174 | fail-fast: true 175 | password: ${CONFIG_SERVICE_PASSWORD} 176 | username: user 177 | ``` 178 | 179 | **[进一步了解Netflix Eureka](./READMORE_CN/netflix-eureka.md)** 180 | 181 | ## 配置Netflix Zuul 182 | 183 | PiggyMetrics借助Netflix Zuul实现[gateway](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/gateway),代理授权服务、账户服务、统计服务和通知服务,这里的代码比较简单,基本上是标准的,不需要修改。 184 | 185 | 我们在实际业务的开发中,在[GatewayApplication.java](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/gateway/src/main/java/com/piggymetrics/gateway/GatewayApplication.java)用具体业务替换相应的服务即可。 186 | 187 | ``` 188 | @EnableZuulProxy ## 增加zuul proxy代理功能 189 | public class GatewayApplication { 190 | public static void main(String[] args) { 191 | SpringApplication.run(GatewayApplication.class, args); 192 | } 193 | } 194 | ``` 195 | 196 | 在resources目录下增加[static](https://github.com/cloudframeworks-springcloud/PiggyMetrics/tree/master/gateway/src/main/resources/static)存放你的静态资源(如html、css、images等) 197 | 198 | 在zuul的配置文件[gateway.yml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/config/src/main/resources/shared/gateway.yml)中增加代理服务的配置 199 | 200 | ``` 201 | zuul: 202 | ignoredServices: '*' 203 | host: 204 | connect-timeout-millis: 20000 ## 超时时间 205 | ocket-timeout-millis: 20000 206 | routes: 207 | auth-service: ## 认证服务 208 | path: /uaa/** ## 匹配路径 209 | url: http://auth-service:5000 ## 服务路径(http方式) 210 | stripPrefix: false ## 是否包括前缀 211 | sensitiveHeaders: 212 | account-service: 213 | path: /accounts/** 214 | serviceId: account-service ## 通过服务ID动态查找 215 | stripPrefix: false 216 | sensitiveHeaders: 217 | statistics-service: 218 | path: /statistics/** 219 | serviceId: statistics-service 220 | stripPrefix: false 221 | sensitiveHeaders: 222 | notification-service: 223 | path: /notifications/** 224 | serviceId: notification-service 225 | stripPrefix: false 226 | sensitiveHeaders: 227 | ``` 228 | 229 | **[进一步了解Netflix Zuul](./READMORE_CN/netflix-zuul.md)** 230 | 231 | ## 配置Netflix Ribbon 232 | 233 | PiggyMetrics并没有显式的去定义Netflix Ribbon的使用,但是在Zuul、Feign等组件中隐式的使用到了Ribbon,我们在实际的业务开发中,也不需要刻意定义Ribbon。 234 | 235 | **[进一步了解Netflix Ribbon](./READMORE_CN/netflix-ribbon.md)** 236 | 237 | ## 配置Netflix Hystrix 238 | 239 | 项目中统一定义了熔断策略(不涉及代码侵入): 240 | 241 | ``` 242 | hystrix: 243 | command: 244 | default: 245 | execution: 246 | isolation: 247 | thread: 248 | timeoutInMilliseconds: 10000 ## 10000ms 超时限制 249 | ``` 250 | 251 | 由于Hystrix的监控只针对单个节点,因此PiggyMetrics通过**Netflix Turbine**来监控集群下Hystrix的metrics情况。 252 | 253 | 实现客户端将Hystrix命令推送到Turbine,只需要在客户端添加如下代码即可,例如[/notification-service/pom.xml](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/notification-service/pom.xml#L79)。 254 | 255 | ``` 256 | 257 | org.springframework.cloud 258 | spring-cloud-netflix-hystrix-stream 259 | 260 | ``` 261 | 262 | **[进一步了解Netflix Hystrix](./READMORE_CN/netflix-hystrix.md)** 263 | 264 | ## 配置Netflix Feign 265 | 266 | PiggyMetrics多次用到了Feign,使用方法为在客户端中添加如下代码,例如[StatisticsServiceClient.java](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/account-service/src/main/java/com/piggymetrics/account/client/StatisticsServiceClient.java)。 267 | 268 | ``` 269 |   @FeignClient(name = "auth-service")     ## 声明一个认证服务的一个客户端,通过注册中心去查找auth-service 270 | public interface AuthServiceClient { 271 | 272 | @RequestMapping(method = RequestMethod.POST, value = "/uaa/users", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) 273 | void createUser(User user); 274 | 275 | } 276 | 277 | ``` 278 | 279 | Feign同时可以引用注册中心以外的服务,例如在统计服务模块,Feign引入了一个汇率客户端[ExchangeRatesClient.java](https://github.com/cloudframeworks-springcloud/PiggyMetrics/blob/master/statistics-service/src/main/java/com/piggymetrics/statistics/client/ExchangeRatesClient.java)。 280 | 281 | ``` 282 | @FeignClient(url = "${rates.url}", name = "rates-client") ## 声明一个汇率客户端,根据具体的url(这个可以是外部的服务) 283 | public interface ExchangeRatesClient { 284 | 285 | @RequestMapping(method = RequestMethod.GET, value = "/latest") 286 | ExchangeRatesContainer getRates(@RequestParam("base") Currency base); 287 | 288 | } 289 | ``` 290 | 291 | **[进一步了解Netflix Feign](./READMORE_CN/netflix-feign.md)** 292 | 293 | # 如何变成自己的项目 294 | 295 | 1. git clone项目到本地,并基于该项目创建自己的mvn项目 296 | 297 | 2. 将auth-service、account-service、notification-service、statistics-service替换成自己的服务 298 | 299 | * *config、registry、gateway、monitoring代码无需修改* 300 | 301 | 3. 在config中修改统一的配置文件,比如新增服务的服务名、端口等 302 | 303 | 4. 通过mvn构建后生成镜像 304 | 305 | 5. 运行所有的镜像,参考[快速部署](#快速部署) 306 | 307 | # 生产环境 308 | 309 | 生产环境中的应用服务需要满足以下基本特征: 310 | 311 |   1. 部分软件、硬件或网络异常后,应用依然能够可靠工作 312 | 313 |   2. 多用户支持下应用继续工作 314 | 315 |   3. 可添加或删除资源来适应不同需求变化 316 | 317 |   4. 便于部署和监控 318 | 319 | 换句话说,在生产环境下,我们需要考虑更多、更复杂的因素以满足实际业务及支持业务的特性。 320 | 321 | 对于微服务架构,推荐使用`Docker`+`Kubernetes`PaaS平台搭建,合理结构如下: 322 | 323 |
324 | 325 | [为什么使用Docker?](https://yeasy.gitbooks.io/docker_practice/content/introduction/why.html) 326 | 327 | [为什么使用Kubernetes?](https://blog.gcp.expert/kubernetes-gke-introduction/) 328 | 329 | [Container, Docker, and Kubernetes](https://collectiveidea.com/blog/archives/2017/01/27/containers-docker-and-kubernetes-part-1) 330 | 331 | ## 安装Kubernetes 332 | 333 | Kubernetes提供了多种详尽的安装方式,此处不再赘述,建议参考: 334 | 335 | * [Kubernetes官方文档:Picking the Right Solution](https://kubernetes.io/docs/setup/pick-right-solution/) 336 | 337 | * [Kubernetes學習筆記](https://gcpug-tw.gitbooks.io/kuberbetes-in-action/content/) 338 | 339 | ## 部署应用至Kubernetes 340 | 341 | 服务编排(Service Orchestration)是**设计、创建和提供端到端服务的过程**,常常出现在面向服务架构、虚拟化、配置、融合基础架构、动态数据中心等等相关内容中,目前最流行的服务编排工具非Kubernetes(K8s)莫属。 342 | 343 | Kubernetes将组成应用的容器组合为逻辑单元,以便于管理和发现。它的功能十分完善,提供包括资源调度、服务发现、运行监控、扩容缩容、负载均衡、灰度升级、失败冗余、容灾恢复、DevOps等一系列选项,帮助实现大规模、分布式、高可用的Docker集群,为解决业务的分布式架构、服务化设计,完整定义了构建业务系统的标准化架构层,即Cluster、Node、Pod、Label等一系列的抽象都是定义好的,为服务编排提供了一个简单、轻量级的方式。[Kubernetes: a platform for automating deployment, scaling, and operations](https://www.slideshare.net/BrianGrant11/wso2con-us-2015-kubernetes-a-platform-for-automating-deployment-scaling-and-operations) 344 | 345 | 在部署应用至Kubernetes之前,我们需要对应用的生命周期有一定了解。 346 | 347 |
348 | 349 | * 使用Git进行代码版本管理(重点在于Git是分布式,[Git vs SVN](http://stackoverflow.com/questions/871/why-is-git-better-than-subversion)) 350 | 351 | * 明确服务的构建规则 352 | 353 | * 采用Image或Binary应用包管理 354 | 355 | * 制定包括开发、测试、生产及审核在内的服务部署规则 356 | 357 | * 根据实际业务需求选择适合的发布机制([灰度发布、AB测试、蓝绿部署、金丝雀部署](http://blog.christianposta.com/deploy/blue-green-deployments-a-b-testing-and-canary-releases/)) 358 | 359 | **部署PiggyMetrics至Kubernetes** 360 | 361 | [查看PiggyMetrics应用结构图](./image/piggymetrics-k8s.png) 362 | 363 | [查看PiggyMetrics完整Yaml文件](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml) 364 | 365 | **步骤:** 366 | 367 | 1. 安装Kubernetes、Docker环境 368 | 369 | 2. 创建命名空间 370 | 371 | ``` 372 | kubectl -s 127.0.0.1:8080 create namespace springcloud 373 | ``` 374 | 375 | 3. 配置容器DNS ([查看dns/dns-addon.yaml文件](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml/dns)) 376 | 377 | ``` 378 | kubectl -s 127.0.0.1:8080 create -f dns/dns-addon.yaml文件 --namespace=springcloud 379 | ``` 380 | 381 | 4. 创建服务 ([查看svc/yaml文件](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml/svc)) 382 | 383 | ``` 384 | kubectl -s 127.0.0.1:8080 create -f svc/yaml文件 --namespace=springcloud 385 | ``` 386 | 387 | 5. 创建应用部署 ([查看deployment/yaml文件](https://github.com/cloudframeworks-springcloud/user-guide-springcloud/tree/master/yaml/deployment)) 388 | 389 | ``` 390 | kubectl -s 127.0.0.1:8080 create -f deployment/yaml文件 --namespace=springcloud 391 | ``` 392 | 393 | 备注: 394 | 395 | 127.0.0.1:8080----kubernetes api server 396 | 397 | svc/yaml文件----svc部署yaml文件 398 | 399 | deployment/yaml文件----deployment部署yaml文件 400 | 401 | ## 功能特性实现 402 | 403 | ### 配置中心高可用 404 | 405 | 在生产环境中服务从配置中心读取文件,而配置中心从Gitlab读取配置文件,将配置中心做成一个集群化微服务即可实现高可用,满足大量服务的需求。 406 | 407 | 结构图如下: 408 | 409 |
410 | 411 | ### 服务注册发现机制 412 | 413 | 在生产环境中服务注册发现管理采用Eureka Server进行,并**采用3个对等节点进行两两注册以实现高可用**。 414 | 415 | 结构图如下: 416 | 417 |
418 | 419 | ### 服务容错机制 420 | 421 | 通过Hystrix进行熔断处理,同时通过Hystrix Dashboard实现图形化展示,并**加入RabbitMQ使其由默认的主动“拉”的方式,变为通过MQ消息队列进行“推”的模式**,以保证简单结构和实时效率。 422 | 423 | 结构如下图所示: 424 | 425 |
426 | 427 | ### 日志采集 428 | 429 | 在生产环境中通过**EFKA**(elasticsearch fluentd kibana kafka)进行日志收集和展示。 430 | 431 | 结构如下图所示: 432 | 433 |
434 | 435 | ### 监控体系 436 | 437 | 通过实时的日志收集系统获取日志信息,并利用时间窗口的技术进行日志分析。 438 | 439 | 结构如下图所示: 440 | 441 |
442 | 443 | ## Spring Cloud性能优化 444 | 445 | 在生产环境中,我们可按照以下几点进行性能优化: 446 | 447 | 1、注册中心配置优化 448 | 449 | eureka: 450 | instance: 451 | prefer-ip-address: true 452 | client: 453 | registerWithEureka: false 454 | fetchRegistry: false 455 | server: 456 | waitTimeInMsWhenSyncEmpty: 0 457 | server: 458 | eviction-interval-timer-in-ms: 4000 459 | enableSelfPreservation: false 460 | renewalPercentThreshold: 0.9 461 | 462 | 2、zuul配置优化 463 | 464 | ribbon: 465 | ReadTimeout: 20000 466 | ConnectTimeout: 20000 467 | MaxAutoRetries: 1 468 | 469 | zuul: 470 | host: 471 | connect-timeout-millis: 20000 472 | socket-timeout-millis: 20000 473 | 474 | 3、Feign配置 475 | 476 | #请求和响应GZIP压缩支持 477 | feign.compression.request.enabled=true 478 | feign.compression.response.enabled=true 479 | #支持压缩的mime types 480 | feign.compression.request.enabled=true 481 | feign.compression.request.mime-types=text/xml,application/xml,application/json 482 | feign.compression.request.min-request-size=2048 483 | 484 | # 更新计划 485 | 486 | * `文档` 增加&完善文档外链 487 | * `组件` 单个组件深入讲解 488 | * `常见问题` 问题汇总 489 | 490 | 点击查看[历史更新](CHANGELOG.md) 491 | 492 | # 社群贡献 493 | 494 | + QQ群: 531980120 495 | + [参与贡献](CONTRIBUTING.md) 496 | + [联系我们](mailto:info@goodrain.com) 497 | 498 | ------- 499 | 500 | [云框架](ABOUT.md)系列主题,遵循[APACHE LICENSE 2.0](LICENSE.md)协议发布。 501 | 502 | 503 | -------------------------------------------------------------------------------- /READMORE/deploy-via-script.md: -------------------------------------------------------------------------------- 1 | ## Deploy via script 2 | 3 | ``` 4 | docker run -d -p15672:15672 --name=rabbitmq rabbitmq:3-management 5 | 6 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -p 8888:8888 --name=config goodraincloudframeworks/piggymetrics-config 7 | 8 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} --link config:config --name=registry -p 8761:8761 goodraincloudframeworks/piggymetrics-registry 9 | 10 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name auth-mongodb goodraincloudframeworks/piggymetrics-mongodb 11 | 12 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e NOTIFICATION_SERVICE_PASSWORD=${NOTIFICATION_SERVICE_PASSWORD} -e STATISTICS_SERVICE_PASSWORD=${STATISTICS_SERVICE_PASSWORD} -e ACCOUNT_SERVICE_PASSWORD=${ACCOUNT_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link auth-mongodb:auth-mongodb --link registry:registry --name=auth-service goodraincloudframeworks/piggymetrics-auth-service 13 | 14 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name account-mongodb goodraincloudframeworks/piggymetrics-mongodb 15 | 16 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e ACCOUNT_SERVICE_PASSWORD=${ACCOUNT_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link account-mongodb:account-mongodb --link registry:registry --link auth-service:auth-service --link rabbitmq:rabbitmq --name=account-service goodraincloudframeworks/piggymetrics-account-service 17 | 18 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name statistics-mongodb goodraincloudframeworks/piggymetrics-mongodb 19 | 20 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e STATISTICS_SERVICE_PASSWORD=${STATISTICS_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link statistics-mongodb:statistics-mongodb --link registry:registry --link auth-service:auth-service --link rabbitmq:rabbitmq --name=statistics-service goodraincloudframeworks/piggymetrics-statistics-service 21 | 22 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name notification-mongodb goodraincloudframeworks/piggymetrics-mongodb 23 | 24 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e NOTIFICATION_SERVICE_PASSWORD=${NOTIFICATION_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link statistics-mongodb:statistics-mongodb --link registry:registry --link auth-service:auth-service --link rabbitmq:rabbitmq --name=notification-service goodraincloudframeworks/piggymetrics-notification-service 25 | 26 |   docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} --link config:config --link registry:registry --link rabbitmq:rabbitmq --name=monitoring -p 9000:8080 -p 8989:8989 goodraincloudframeworks/piggymetrics-monitoring 27 | 28 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} --link config:config --link registry:registry --link auth-service:auth-service --name=gateway -p 80:4000 goodraincloudframeworks/piggymetrics-gateway 29 | ``` 30 | 31 | 32 | -------------------------------------------------------------------------------- /READMORE/install-docker.md: -------------------------------------------------------------------------------- 1 | ## install docker 2 | 3 | #### centos 4 | 5 | ``` 6 | 1.Remove old version of docker 7 | 8 | rpm -qa |grep docker 9 | yum -y remove docker* 10 | 11 | 2.install the new version of docker 12 | 13 | yum install -y docker-engine 14 | 15 | 3.systemctl start docker 16 | 17 | 4.docker info check docker status 18 | ``` 19 | 20 | #### ubuntu 21 | 22 | ``` 23 | 1.update apt 24 | 25 | sudo apt-get update 26 | 27 | 2.install docker 28 | 29 | sudo apt-get install docker-engine 30 | 31 | 3.sudo service docker start 32 | 33 | 4.docker info check docker status 34 | ``` 35 | 36 | #### mac 37 | 38 | see [https://docs.docker.com/docker-for-mac/](https://docs.docker.com/docker-for-mac/) 39 | 40 | 41 | -------------------------------------------------------------------------------- /READMORE/netflix-eureka.md: -------------------------------------------------------------------------------- 1 | ## Read more about Netflix Eureka 2 | 3 | Service granularity of microservice architecture is smaller than SOA architecture, and the amount of services is bigger, then the concept of service registration has merged. The service registration of microservice architecture is 1) easy to use, transparent to users; 2) high availability, to meet the CAP theory; 3) support multi-language. 4 | 5 | Microservice architecture with Spring Cloud usually use Netflix Eureka as its registry, and its ease of use is reflected in: 6 | 7 | * combine with Spring Boot to achieve the effect of deploying and launching services with annotations and Maven dependencies only 8 | * Netflix Eureka comes with the Client package that we don't need to consider the communication mechanism with Eureka 9 | 10 | Netflix is highly available through the "partner" mechanism, each Eureka need to specify another Eureka address as a partner in the configuration. Eureka will get the current registration list from its partner node, and when adding new machines in Eureka cluster, we don't need worry about the incomplete of registration list, that is, the AP principle is satisfied in the CAP theory. 11 | 12 | In addition, Netflix Eureka supports the concept of Region and Zone, and a Region can contains multiple zones. Eureka need to specify a Zone name when launching, this indicate the Eureka belongs to which Zone (defaultZone if not specified). We also need to specify a Zone to Eureka Client. 13 | 14 | Netflix Eureka is written in JAVA, but it exposes all registration and heartbeat connections to HTTP REST interface. The Client communicates with Server by HTTP request, which means Client can be written with any other language, just let the Client call registration service, logout service, get the service list and heartbeat request HTTP REST interface in real time. 15 | 16 | #### Build Eureka Server 17 | 18 | * Build mvn and name it eureka-server, core dependencies: 19 | 20 | ``` 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-eureka-server 24 | 25 | 26 | ``` 27 | 28 | * Start the configuration server by adding @EnableEurekaServer in the Application class of application entry 29 | 30 | ``` 31 | @SpringBootApplication 32 | @EnableEurekaServer 33 | public class EurekaApplication { 34 | 35 | public static void main(String[] args) { 36 | SpringApplication.run(EurekaApplication.class, args); 37 | } 38 | } 39 | 40 | ``` 41 | 42 | * Configuration 43 | 44 | ``` 45 | server: 46 | port: 8761 47 | spring: 48 | application: 49 | name: eureka-server 50 | eureka: 51 | instance: 52 | prefer-ip-address: true 53 | client: 54 | registerWithEureka: false 55 | fetchRegistry: false 56 | serviceUrl: 57 | defaultZone: http://127.0.0.1:8761/eureka/ 58 | server: 59 | waitTimeInMsWhenSyncEmpty: 0 60 | server 61 | eviction-interval-timer-in-ms: 4000 62 | enableSelfPreservation: false 63 | renewalPercentThreshold: 0.9 64 | ``` 65 | 66 | #### Build Eureka service 67 | 68 | * Build mvn and name it eureka-service, core dependencies: 69 | 70 | ``` 71 | 72 | org.springframework.cloud 73 | spring-cloud-starter-eureka 74 | 75 | ``` 76 | 77 | * Start the configuration server by adding @EnableDiscoveryClient in the Application class of application entry 78 | 79 | ``` 80 | @SpringBootApplication 81 | @EnableDiscoveryClient 82 | public class DemoServiceApplication { 83 | 84 | public static void main(String[] args) { 85 | SpringApplication.run(DemoServiceApplication.class, args); 86 | } 87 | } 88 | ``` 89 | 90 | * Create 2 restful interfaces 91 | 92 | normal demo application with /demo/show and /demo/index interfaces 93 | 94 | ``` 95 | @RequestMapping("/demo") 96 | @RestController 97 | public class DemoController { 98 | 99 | @RequestMapping("/show") 100 | public String show() { 101 | return "demo show"; 102 | } 103 | 104 | @RequestMapping("/index") 105 | public String index() { 106 | return "demo index"; 107 | } 108 | } 109 | ``` 110 | 111 | user application with /user/online and /user/offline interfaces, EurekaDiscoveryClientConfiguration manage the service in registry's declaration cycle (offline and online) 112 | 113 | ``` 114 | @RequestMapping("/user") 115 | @RestController 116 | public class UserController { 117 | @Autowired 118 | private EurekaDiscoveryClientConfiguration lifecycle; 119 | 120 | @RequestMapping("/online") 121 | public String online() { 122 | this.lifecycle.start(); 123 | return "user online method"; 124 | } 125 | 126 | @RequestMapping("/offline") 127 | public String offline() { 128 | this.lifecycle.stop(); 129 | return "user offline method"; 130 | } 131 | } 132 | ``` 133 | 134 | * Configuration 135 | 136 | ``` 137 | spring.application.name=eureka-service 138 | server.port=5000 139 | eureka.region=default 140 | eureka.preferSameZone=false 141 | eureka.shouldUseDns=false 142 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 143 | eureka.instance.preferIpAddress=true 144 | eureka.instance.leaseRenewalIntervalInSeconds=10 145 | eureka.instance.leaseExpirationDurationInSeconds=20 146 | ``` 147 | 148 | EUREKA_HOST:registry ip 149 | 150 | EUREKA_PORT:registry interface 151 | 152 | * Address 153 | 154 | http://DOCKER_HOST:DOCKER_PORT/demo/index 155 | 156 | http://DOCKER_HOST:DOCKER_PORT/demo/show 157 | 158 | http://DOCKER_HOST:DOCKER_PORT/user/online 159 | 160 | http://DOCKER_HOST:DOCKER_PORT/user/offline 161 | 162 | * Access registry, and we can see eureka-service had been registered 163 | 164 | http://EUREKA_HOST:EUREKA_PORT/eureka/ 165 | 166 | 167 | -------------------------------------------------------------------------------- /READMORE/netflix-feign.md: -------------------------------------------------------------------------------- 1 | ## Read more about Netflix Feign 2 | 3 | Feign is a declarative, templated HTTP client that makes building web service easier. We can use Feign by create a interface and annotate it. Feign supports pluggable annotation, includes Feign annotate and JAX-RS annotate. Feign support pluggable encoders and decoders. When we use Feign, Spring Cloud integrated Ribbon and Eureka to provide load balancing. 4 | 5 | In short: 1) Feign is using interface annotation; 2) Feign is integrated with Ribbon 6 | 7 | #### Build Feign service 8 | 9 | * Build mvn and name it feign (check pom.xml in example code), core dependencies: 10 | 11 | ``` 12 | 13 | org.springframework.cloud 14 | spring-cloud-starter-ribbon 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-feign 19 | 20 | ``` 21 | 22 | * Start the configuration server by adding @EnableFeignServer in the Application class of application entry 23 | 24 | ``` 25 | @SpringBootApplication 26 | @EnableDiscoveryClient 27 | @EnableFeignClients 28 | public class FeignDemoApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(FeignDemoApplication.class, args); 32 | } 33 | } 34 | ``` 35 | 36 | * Create a remote call service 37 | 38 | ``` 39 | @FeignClient("eureka-service") 40 | public interface RemoteInvokerService { 41 | 42 | @RequestMapping(value = "/demo/show", method = RequestMethod.GET) 43 | public String remoteInvoker(); 44 | } 45 | ``` 46 | 47 | eureka-service is the service that we register in the eureka module 48 | 49 | remote call /demo/show REST interface, can replace it with /demo/index etc. 50 | 51 | * Configuration 52 | 53 | ``` 54 | spring.application.name=feign 55 | server.port=5000 56 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 57 | eureka.instance.preferIpAddress=true 58 | ``` 59 | 60 | EUREKA_HOST:registry ip 61 | 62 | EUREKA_PORT:registry interface 63 | 64 | * Address 65 | 66 | http://DOCKER_HOST:DOCKER_PORT/feign 67 | 68 | 69 | -------------------------------------------------------------------------------- /READMORE/netflix-hystrix.md: -------------------------------------------------------------------------------- 1 | ## Read more about Netflix Hystrix 2 | 3 | Netflix Hystrix is a latency and fault-tolerant library designed to isolate access points from remote systems, services and third-party libraries, stop cascading failures, and enable resiliency in complex, distributed systems with unavoidable failures. 4 | 5 | #### Build Hystrix service 6 | 7 | * Build mvn and name it hystrix-service, core dependencies: 8 | 9 | ``` 10 | 11 | org.springframework.cloud 12 | spring-cloud-netflix-hystrix-stream 13 | 14 | ``` 15 | 16 | * In the application class of application entry 17 | 18 | ``` 19 | @SpringBootApplication 20 | @EnableDiscoveryClient 21 | @EnableFeignClients 22 | public class HystrixDemoApplication { 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(HystrixDemoApplication.class, args); 26 | } 27 | } 28 | ``` 29 | 30 | * Create a remote call service 31 | 32 | ``` 33 | @Service 34 | public class RemoteShowService { 35 | 36 | @Autowired 37 | private RestTemplate restTemplate; 38 | 39 | @HystrixCommand(fallbackMethod = "reliable", groupKey = "Demo", commandKey = "Show", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) 40 | public String remoteShow() { 41 | return restTemplate.getForObject("http://EUREKA-SERVICE/demo/show", String.class); 42 | } 43 | 44 | public String reliable() { 45 | return "fallback Method"; 46 | } 47 | } 48 | ``` 49 | 50 | @HystrixCommand: custom blocking mechanism 51 | 52 | EUREKA-SERVICE is the service registered in the eureka module 53 | 54 | * Create another remote call service 55 | 56 | ``` 57 | @RestController 58 | @RequestMapping("/first") 59 | public class HystrixHelloController { 60 | 61 | @Autowired 62 | private RemoteInvokerService remoteInvokerService; 63 | 64 | @RequestMapping("hystrix") 65 | @HystrixCommand(fallbackMethod = "failme", groupKey = "Demo", commandKey = "first", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000") }) 66 | public String remoteHello() { 67 | return remoteInvokerService.remoteInvoker(); 68 | } 69 | 70 | protected String failme() { 71 | return "failed invoked Method"; 72 | } 73 | } 74 | ``` 75 | 76 | @HystrixCommand: custom blocking mechanism 77 | 78 | EUREKA-SERVICEis the service registered in the eureka module 79 | 80 | * Configuration 81 | 82 | ``` 83 | spring.application.name=hystrix-service 84 | server.port=5000 85 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 86 | eureka.instance.preferIpAddress=true 87 | ``` 88 | 89 | EUREKA_HOST:registry ip 90 | 91 | EUREKA_PORT:registry interface 92 | 93 | * Address 94 | 95 | http://DOCKER_HOST:DOCKER_PORT/first 96 | 97 | http://DOCKER_HOST:DOCKER_PORT/second 98 | 99 | #### Build Hystrix monitoring 100 | 101 | * Build mvn and name it hystrix-monitoring, core denpendencies: 102 | 103 | ``` 104 | 105 | org.springframework.cloud 106 | spring-cloud-starter-turbine-stream 107 | 108 | 109 | org.springframework.cloud 110 | spring-cloud-starter-stream-rabbit 111 | 112 | 113 | org.springframework.cloud 114 | spring-cloud-starter-hystrix-dashboard 115 | 116 | ``` 117 | 118 | Collection based on Rabbitmq 119 | 120 | * Add @EnableTurbineStream and @EnableHystrixDashboard in the Application class of application entry 121 | 122 | ``` 123 | @@SpringBootApplication 124 | @EnableTurbineStream 125 | @EnableHystrixDashboard 126 | public class MonitoringApplication { 127 | 128 | public static void main(String[] args) { 129 | SpringApplication.run(MonitoringApplication.class, args); 130 | } 131 | } 132 | ``` 133 | 134 | * Configuration 135 | 136 | ``` 137 | eureka: 138 | instance: 139 | prefer-ip-address: true 140 | client: 141 | serviceUrl: 142 | defaultZone: http://EUREKA_HOST:EUREKA_PORT/eureka/ 143 | 144 | spring: 145 | application: 146 | name: hystrix-monitor 147 | rabbitmq: 148 | host: rabbitmq 149 | ``` 150 | 151 | EUREKA_HOST: registry ip 152 | 153 | EUREKA_PORT: registry interface 154 | 155 | * Address 156 | 157 | add http://DOCKER_HOST:8989/ in http://DOCKER_HOST:8080/hystrix 158 | 159 | 160 | -------------------------------------------------------------------------------- /READMORE/netflix-ribbon.md: -------------------------------------------------------------------------------- 1 | ## Read more about Netflix Ribbon 2 | 3 | Ribbon is a client load balancing with variety of load balancing strategies, can be used with service discovery and circuit breakers. Lists all the machines in the configuration, and Ribbon will automatically connect those machines based on certain rules (simple polling, random connections, etc.) 4 | 5 | Ribbon can do 1) load balancing; 2) fault-tolerance; 3) support multi-protocol (HTT, TCP, UDP) in asynchronous and reactive models; 4) cache and batch 6 | 7 | #### Build Ribbon service 8 | 9 | * Build mvn and name it ribbon, core dependencies: 10 | 11 | ``` 12 | 13 | org.springframework.cloud 14 | spring-cloud-starter-ribbon 15 | 16 | ``` 17 | 18 | * In the application class of application entry 19 | 20 | ``` 21 | @SpringBootApplication 22 | @EnableDiscoveryClient 23 | public class RibbonApplication { 24 | 25 | @Bean 26 | @LoadBalanced 27 | RestTemplate restTemplate() { 28 | return new RestTemplate(); 29 | } 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(RibbonApplication.class, args); 33 | } 34 | } 35 | ``` 36 | 37 | @LoadBalanced: declare a loadBalanced template 38 | 39 | * Create a remote call service 40 | 41 | ``` 42 | @RestController 43 | public class DemoController { 44 | 45 | @Autowired 46 | RestTemplate restTemplate; 47 | 48 | @RequestMapping(value = "/ribbon", method = RequestMethod.GET) 49 | public String add() { 50 | return restTemplate.getForEntity("http://EUREKA-SERVICE/demo/show", String.class).getBody(); 51 | } 52 | } 53 | ``` 54 | 55 | EUREKA-SERVICE is the service registered in eureka module 56 | 57 | remote call /demo/show REST interface, can be replace with /demo/index etc. 58 | 59 | * Configuration 60 | 61 | ``` 62 | spring.application.name=ribbon 63 | server.port=5000 64 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 65 | eureka.instance.preferIpAddress=true 66 | ``` 67 | 68 | EUREKA_HOST: registry ip 69 | 70 | EUREKA_PORT: registry interface 71 | 72 | * Address 73 | 74 | http://DOCKER_HOST:DOCKER_PORT/ribbon 75 | 76 | 77 | -------------------------------------------------------------------------------- /READMORE/netflix-zuul.md: -------------------------------------------------------------------------------- 1 | ## Read more about Netflix Zuul 2 | 3 | Netflix Zuul provides edge services for dynamic routing, monitoring, resiliency, and security. 4 | 5 | Netflix Zuul provides front-door protection for microservice providers in a microservice architecture that provides REST APIs through a unified service gateway, while migrating rights to these heavier non-business logic content to the service routing layer Clusters can have higher reusability and testability. 6 | 7 | #### Build Zuul service 8 | 9 | * Build mvn and name it zuul, core dependencies: 10 | 11 | ``` 12 | 13 | org.springframework.cloud 14 | spring-cloud-starter-zuul 15 | 16 | ``` 17 | 18 | * Start the configuration server by adding @EnableZuulProxy in the Application class of application entry 19 | 20 | ``` 21 | @SpringBootApplication 22 | @EnableDiscoveryClient 23 | @EnableZuulProxy 24 | public class GatewayApplication { 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(GatewayApplication.class, args); 28 | } 29 | } 30 | ``` 31 | 32 | * Configuration 33 | 34 | ``` 35 | server: 36 | port: 5000 37 | 38 | spring: 39 | application: 40 | name: zuul 41 | 42 | eureka: 43 | client: 44 | service-url: 45 | defaultZone: http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 46 | 47 | hystrix: 48 | command: 49 | default: 50 | execution: 51 | isolation: 52 | thread: 53 | timeoutInMilliseconds: 20000 54 | 55 | ribbon: 56 | ReadTimeout: 20000 57 | ConnectTimeout: 20000 58 | 59 | zuul: 60 | ignoredServices: '*' 61 | host: 62 | connect-timeout-millis: 20000 63 | socket-timeout-millis: 20000 64 | 65 | routes: 66 | routes: 67 | demo: 68 | path: /demo/** 69 | serviceId: eureka-service 70 | stripPrefix: false 71 | sensitiveHeaders: Cookie,Set-Cookie,Authorization 72 | user: 73 | path: /user/** 74 | serviceId: eureka-service 75 | stripPrefix: false 76 | sensitiveHeaders: Cookie,Set-Cookie,Authorization 77 | outer: 78 | path: /baidu/** 79 | url: http://www.baidu.com 80 | ``` 81 | 82 | EUREKA_HOST: registry ip 83 | 84 | EUREKA_PORT: registry interface 85 | 86 | * Address 87 | 88 | http://DOCKER_HOST:DOCKER_PORT/feign 89 | 90 | 91 | -------------------------------------------------------------------------------- /READMORE/spring-cloud-config.md: -------------------------------------------------------------------------------- 1 | ## Read more about Spring Cloud Config 2 | 3 | In a distributed system, Spring Cloud Config provides extensible configuration services through config-server and config-client, and uses the configuration service center to centrally manage the various environment profiles for all services. Spring Cloud Config supports three ways of storing Git (default), SVN, and File, based on the idea of using the central repository (version control). 4 | 5 | #### Build Config Server 6 | 7 | * Build mvn and name it config-server, core dependencies: 8 | 9 | ``` 10 | 11 | org.springframework.boot 12 | spring-boot-starter-test 13 | test 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-config-server 18 | 19 | ``` 20 | 21 | * Start the configuration server by adding @EnableConfigServer in the Application class of application entry 22 | 23 | ``` 24 | @EnableConfigServer 25 | @SpringBootApplication 26 | public class Application { 27 | 28 | public static void main(String[] args) { 29 | new SpringApplicationBuilder(Application.class).web(true).run(args); 30 | } 31 | } 32 | ``` 33 | 34 | * Configuration 35 | 36 | ``` 37 | server: 38 | port: 8888 39 | 40 | spring: 41 | application: 42 | name: config-server 43 | cloud: 44 | config: 45 | server: 46 | git: 47 | uri: ## The git address stored in the configuration file 48 | searchPaths: config 49 | 50 | ``` 51 | 52 | Obtaining resource information on git follows the following rules: 53 | 54 | ``` 55 | /{application}/{profile}[/{label}] 56 | /{application}-{profile}.yml 57 | /{label}/{application}-{profile}.yml 58 | /{application}-{profile}.properties 59 | /{label}/{application}-{profile}.properties 60 | ``` 61 | 62 | #### Build Config client 63 | 64 | * Build mvn and name it config-client, core dependencies: 65 | 66 | ``` 67 | 68 | org.springframework.cloud 69 | spring-cloud-config-client 70 | 71 | ``` 72 | 73 | * Start the configuration server by adding @EnableConfigServer in the Application class of application entry 74 | 75 | ``` 76 | @SpringBootApplication 77 | public class Application { 78 | 79 | public static void main(String[] args) { 80 | new SpringApplicationBuilder(Application.class).web(true).run(args); 81 | } 82 | } 83 | ``` 84 | 85 | * Create a restful interface to access configuration 86 | 87 | ``` 88 | @EnableAutoConfiguration 89 | @RefreshScope 90 | @RestController 91 | public class DemoController { 92 | 93 | @Value("${from}") 94 | String from; 95 | 96 | 97 | @RequestMapping("/from") 98 | public String from() { 99 | return this.from; 100 | } 101 | } 102 | ``` 103 | 104 | * Configuration 105 | 106 | ``` 107 | server: 108 | port: 9000 109 | 110 | spring: 111 | application: 112 | name: config-client 113 | profiles: 114 | active: dev 115 | cloud: 116 | config: 117 | uri: http://${CONFIG_HOST}:${CONFIG_PORT} 118 | label: master 119 | ``` 120 | 121 | * Address 122 | 123 | http://DOCKER_HOST:DOCKER_PORT/from 124 | 125 | 126 | -------------------------------------------------------------------------------- /READMORE_CN/deploy-via-script.md: -------------------------------------------------------------------------------- 1 | ## 通过脚本分别部署每个组件,命令如下 2 | 3 | ``` 4 | docker run -d -p15672:15672 --name=rabbitmq rabbitmq:3-management 5 | 6 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -p 8888:8888 --name=config goodraincloudframeworks/piggymetrics-config 7 | 8 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} --link config:config --name=registry -p 8761:8761 goodraincloudframeworks/piggymetrics-registry 9 | 10 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name auth-mongodb goodraincloudframeworks/piggymetrics-mongodb 11 | 12 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e NOTIFICATION_SERVICE_PASSWORD=${NOTIFICATION_SERVICE_PASSWORD} -e STATISTICS_SERVICE_PASSWORD=${STATISTICS_SERVICE_PASSWORD} -e ACCOUNT_SERVICE_PASSWORD=${ACCOUNT_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link auth-mongodb:auth-mongodb --link registry:registry --name=auth-service goodraincloudframeworks/piggymetrics-auth-service 13 | 14 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name account-mongodb goodraincloudframeworks/piggymetrics-mongodb 15 | 16 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e ACCOUNT_SERVICE_PASSWORD=${ACCOUNT_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link account-mongodb:account-mongodb --link registry:registry --link auth-service:auth-service --link rabbitmq:rabbitmq --name=account-service goodraincloudframeworks/piggymetrics-account-service 17 | 18 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name statistics-mongodb goodraincloudframeworks/piggymetrics-mongodb 19 | 20 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e STATISTICS_SERVICE_PASSWORD=${STATISTICS_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link statistics-mongodb:statistics-mongodb --link registry:registry --link auth-service:auth-service --link rabbitmq:rabbitmq --name=statistics-service goodraincloudframeworks/piggymetrics-statistics-service 21 | 22 | docker run -d -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --name notification-mongodb goodraincloudframeworks/piggymetrics-mongodb 23 | 24 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} -e NOTIFICATION_SERVICE_PASSWORD=${NOTIFICATION_SERVICE_PASSWORD} -e MONGODB_PASSWORD=${MONGODB_PASSWORD} --link config:config --link statistics-mongodb:statistics-mongodb --link registry:registry --link auth-service:auth-service --link rabbitmq:rabbitmq --name=notification-service goodraincloudframeworks/piggymetrics-notification-service 25 | 26 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} --link config:config --link registry:registry --link rabbitmq:rabbitmq --name=monitoring -p 9000:8080 -p 8989:8989 goodraincloudframeworks/piggymetrics-monitoring 27 | 28 | docker run -d -e CONFIG_SERVICE_PASSWORD=${CONFIG_SERVICE_PASSWORD} --link config:config --link registry:registry --link auth-service:auth-service --name=gateway -p 80:4000 goodraincloudframeworks/piggymetrics-gateway 29 | ``` 30 | -------------------------------------------------------------------------------- /READMORE_CN/install-docker.md: -------------------------------------------------------------------------------- 1 | ## install docker 2 | 3 | #### centos 4 | 5 | ``` 6 | 1.清除docker 旧版本 7 | 8 | rpm -qa |grep docker 9 | yum -y remove docker* 10 | 11 | 2.安装新的docker 12 | 13 | yum install -y docker-engine 14 | 15 | 3.systemctl start docker 16 | 17 | 4.docker info 查看docker状态 18 | ``` 19 | 20 | #### ubuntu 21 | 22 | ``` 23 | 1.更新apt包 24 | 25 | sudo apt-get update 26 | 27 | 2.安装 Docker 28 | 29 | sudo apt-get install docker-engine 30 | 31 | 3.sudo service docker start 32 | 33 | 4.docker info 查看docker状态 34 | ``` 35 | 36 | #### mac 37 | 38 | 请参考[https://docs.docker.com/docker-for-mac/](https://docs.docker.com/docker-for-mac/) 39 | -------------------------------------------------------------------------------- /READMORE_CN/netflix-eureka.md: -------------------------------------------------------------------------------- 1 | ## 进一步了解Netflix Eureka 2 | 3 | 微服务架构比传统SOA架构中的服务粒度更小、服务数量更多,为了有效管理各个服务,服务注册的概念应运而生。它的特点是1)简单易用,对用户透明;2)高可用,满足CAP理论;3)多语言支持。 4 | 5 | 在基于Spring Cloud的微服务架构中,通常采用Netflix Eureka作为注册中心,它的易用性体现在: 6 | 7 | * 通过与Spring Boot(Cloud)结合达到只用注解和Maven依赖即可部署和启动服务的效果 8 | * Netflix Eureka自带Client包,使得使用Eureka作为注册中心的客户端(即服务)不需要关心自己与Eureka的通讯机制,只需要引入Client依赖即可,当然前提是使用Java 9 | 10 | Netflix Eureka通过“伙伴”机制实现高可用,每一台Eureka都需要在配置中指定另一个Eureka的地址作为伙伴,Eureka启动时会向自己的伙伴节点获取当前已经存在的注册列表,这样在向Eureka集群中增加新机器时就不需要担心注册列表不完整的问题,在CAP理论中满足AP原则。 11 | 12 | 除此之外,Netflix Eureka支持Region和Zone的概念,其中一个Region可以包含多个Zone。Eureka在启动时需要指定一个Zone名,即指定当前Eureka属于哪个Zone, 如果不指定则属于defaultZone。值得注意的是,Eureka Client也需要指定Zone。 13 | 14 | Netflix Eureka使用Java编写,但它会将所有注册信息和心跳连接地址都暴露为HTTP REST接口,客户端实际是通过HTTP请求与Server进行通讯的,因此Client完全可以使用其它语言进行编写,只需要即时调用注册服务、注销服务、获取服务列表和心跳请求的HTTP REST接口即可。 15 | 16 | #### 创建Eureka Server 17 | 18 | * 创建一个mvn工程,起名为eureka-server,核心依赖: 19 | 20 | ``` 21 | 22 | org.springframework.cloud 23 | spring-cloud-starter-eureka-server 24 | 25 | 26 | ``` 27 | 28 | * 在程序的入口Application类加上@EnableEurekaServer注解开启配置服务器 29 | 30 | ``` 31 | @SpringBootApplication 32 | @EnableEurekaServer 33 | public class EurekaApplication { 34 | 35 | public static void main(String[] args) { 36 | SpringApplication.run(EurekaApplication.class, args); 37 | } 38 | } 39 | 40 | ``` 41 | 42 | * 配置文件 43 | 44 | ``` 45 | server: 46 | port: 8761 47 | spring: 48 | application: 49 | name: eureka-server 50 | eureka: 51 | instance: 52 | prefer-ip-address: true 53 | client: 54 | registerWithEureka: false 55 | fetchRegistry: false 56 | serviceUrl: 57 | defaultZone: http://127.0.0.1:8761/eureka/ 58 | server: 59 | waitTimeInMsWhenSyncEmpty: 0 60 | server 61 | eviction-interval-timer-in-ms: 4000 62 | enableSelfPreservation: false 63 | renewalPercentThreshold: 0.9 64 | ``` 65 | 66 | #### 创建Eureka service 67 | 68 | * 创建一个mvn工程,起名为eureka-service,核心依赖: 69 | 70 | ``` 71 | 72 | org.springframework.cloud 73 | spring-cloud-starter-eureka 74 | 75 | ``` 76 | 77 | * 在程序的入口Application类加上@EnableDiscoveryClient注解开启配置服务器 78 | 79 | ``` 80 | @SpringBootApplication 81 | @EnableDiscoveryClient 82 | public class DemoServiceApplication { 83 | 84 | public static void main(String[] args) { 85 | SpringApplication.run(DemoServiceApplication.class, args); 86 | } 87 | } 88 | ``` 89 | 90 | * 创建2个restful接口 91 | 92 | 普通的demo程序,提供/demo/show 和 /demo/index 接口 93 | 94 | ``` 95 | @RequestMapping("/demo") 96 | @RestController 97 | public class DemoController { 98 | 99 | @RequestMapping("/show") 100 | public String show() { 101 | return "demo show"; 102 | } 103 | 104 | @RequestMapping("/index") 105 | public String index() { 106 | return "demo index"; 107 | } 108 | } 109 | ``` 110 | 111 | user程序,提供/user/online和/user/offline 接口, 其中EurekaDiscoveryClientConfiguration管理改服务在注册中心的声明周期(下线和上线) 112 | 113 | ``` 114 | @RequestMapping("/user") 115 | @RestController 116 | public class UserController { 117 | @Autowired 118 | private EurekaDiscoveryClientConfiguration lifecycle; 119 | 120 | @RequestMapping("/online") 121 | public String online() { 122 | this.lifecycle.start(); 123 | return "user online method"; 124 | } 125 | 126 | @RequestMapping("/offline") 127 | public String offline() { 128 | this.lifecycle.stop(); 129 | return "user offline method"; 130 | } 131 | } 132 | ``` 133 | 134 | * 配置文件 135 | 136 | ``` 137 | spring.application.name=eureka-service 138 | server.port=5000 139 | eureka.region=default 140 | eureka.preferSameZone=false 141 | eureka.shouldUseDns=false 142 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 143 | eureka.instance.preferIpAddress=true 144 | eureka.instance.leaseRenewalIntervalInSeconds=10 145 | eureka.instance.leaseExpirationDurationInSeconds=20 146 | ``` 147 | 148 | EUREKA_HOST:注册中心ip 149 | 150 | EUREKA_PORT:注册中心端口 151 | 152 | * 访问地址 153 | 154 | http://DOCKER_HOST:DOCKER_PORT/demo/index 155 | 156 | http://DOCKER_HOST:DOCKER_PORT/demo/show 157 | 158 | http://DOCKER_HOST:DOCKER_PORT/user/online 159 | 160 | http://DOCKER_HOST:DOCKER_PORT/user/offline 161 | 162 | * 访问注册中心可以看到eureka-service已注册 163 | 164 | http://EUREKA_HOST:EUREKA_PORT/eureka/ 165 | -------------------------------------------------------------------------------- /READMORE_CN/netflix-feign.md: -------------------------------------------------------------------------------- 1 | ## 进一步了解Netflix Feign 2 | 3 | Feign是一个声明式、模板化的HTTP客户端,它使得写web服务变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,包括Feign注解和JAX-RS注解。Feign同时支持可插拔的编码器和解码器。当我们使用feign的时候,spring cloud 整和了Ribbon和Eureka去提供负载均衡。 4 | 5 | 简而言之:1)Feign采用的是接口加注解;2)Feign 整合了Ribbon。 6 | 7 | #### 创建feign service 8 | 9 | * 创建一个mvn工程,起名为feign,其pom.xml见实例代码,核心依赖如下: 10 | 11 | ``` 12 | 13 | org.springframework.cloud 14 | spring-cloud-starter-ribbon 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-feign 19 | 20 | ``` 21 | 22 | * 在程序的入口Application类加上@EnableFeignClients注解开启配置服务器 23 | 24 | ``` 25 | @SpringBootApplication 26 | @EnableDiscoveryClient 27 | @EnableFeignClients 28 | public class FeignDemoApplication { 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(FeignDemoApplication.class, args); 32 | } 33 | } 34 | ``` 35 | 36 | * 创建一个远程调用服务 37 | 38 | ``` 39 | @FeignClient("eureka-service") 40 | public interface RemoteInvokerService { 41 | 42 | @RequestMapping(value = "/demo/show", method = RequestMethod.GET) 43 | public String remoteInvoker(); 44 | } 45 | ``` 46 | 47 | eureka-service: 是我们在eureka模块中注册的服务 48 | 49 | 远程调用/demo/show这个rest接口,也可以改成/demo/index 等 50 | 51 | * 配置文件 52 | 53 | ``` 54 | spring.application.name=feign 55 | server.port=5000 56 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 57 | eureka.instance.preferIpAddress=true 58 | ``` 59 | 60 | EUREKA_HOST:注册中心ip 61 | 62 | EUREKA_PORT:注册中心端口 63 | 64 | * 访问地址 65 | 66 | http://DOCKER_HOST:DOCKER_PORT/feign 67 | 68 | 69 | -------------------------------------------------------------------------------- /READMORE_CN/netflix-hystrix.md: -------------------------------------------------------------------------------- 1 | ## 进一步了解Netflix Hystrix 2 | 3 | Netflix Hystrix是一个延迟和容错库,旨在隔离远程系统,服务和第三方库的访问点,停止级联故障,并在不可避免的故障的复杂分布式系统中启用弹性。 4 | 5 | #### 创建Hystrix service 6 | 7 | * 创建一个mvn工程,起名为hystrix-service,核心依赖如下: 8 | 9 | ``` 10 | 11 | org.springframework.cloud 12 | spring-cloud-netflix-hystrix-stream 13 | 14 | ``` 15 | 16 | * 在程序的入口Application 17 | 18 | ``` 19 | @SpringBootApplication 20 | @EnableDiscoveryClient 21 | @EnableFeignClients 22 | public class HystrixDemoApplication { 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(HystrixDemoApplication.class, args); 26 | } 27 | } 28 | ``` 29 | 30 | * 创建一个远程调用服务 31 | 32 | ``` 33 | @Service 34 | public class RemoteShowService { 35 | 36 | @Autowired 37 | private RestTemplate restTemplate; 38 | 39 | @HystrixCommand(fallbackMethod = "reliable", groupKey = "Demo", commandKey = "Show", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) 40 | public String remoteShow() { 41 | return restTemplate.getForObject("http://EUREKA-SERVICE/demo/show", String.class); 42 | } 43 | 44 | public String reliable() { 45 | return "fallback Method"; 46 | } 47 | } 48 | ``` 49 | 50 | @HystrixCommand: 自定义拦截机制 51 | 52 | EUREKA-SERVICE:在eureka模块中注册的服务 53 | 54 | * 创建另一个远程调用服务 55 | 56 | ``` 57 | @RestController 58 | @RequestMapping("/first") 59 | public class HystrixHelloController { 60 | 61 | @Autowired 62 | private RemoteInvokerService remoteInvokerService; 63 | 64 | @RequestMapping("hystrix") 65 | @HystrixCommand(fallbackMethod = "failme", groupKey = "Demo", commandKey = "first", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000") }) 66 | public String remoteHello() { 67 | return remoteInvokerService.remoteInvoker(); 68 | } 69 | 70 | protected String failme() { 71 | return "failed invoked Method"; 72 | } 73 | } 74 | ``` 75 | 76 | @HystrixCommand: 自定义拦截机制 77 | 78 | EUREKA-SERVICE:在eureka模块中注册的服务 79 | 80 | * 配置文件 81 | 82 | ``` 83 | spring.application.name=hystrix-service 84 | server.port=5000 85 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 86 | eureka.instance.preferIpAddress=true 87 | ``` 88 | 89 | EUREKA_HOST:注册中心ip 90 | 91 | EUREKA_PORT:注册中心端口 92 | 93 | * 访问地址 94 | 95 | http://DOCKER_HOST:DOCKER_PORT/first 96 | 97 | http://DOCKER_HOST:DOCKER_PORT/second 98 | 99 | #### 创建Hystrix monitoring 100 | 101 | * 创建一个mvn工程,起名为hystrix-monitoring,核心依赖: 102 | 103 | ``` 104 | 105 | org.springframework.cloud 106 | spring-cloud-starter-turbine-stream 107 | 108 | 109 | org.springframework.cloud 110 | spring-cloud-starter-stream-rabbit 111 | 112 | 113 | org.springframework.cloud 114 | spring-cloud-starter-hystrix-dashboard 115 | 116 | ``` 117 | 118 | 基于rabbitmq去收集聚合 119 | 120 | * 在程序的入口Application,加入@EnableTurbineStream 和 @EnableHystrixDashboard 121 | 122 | ``` 123 | @@SpringBootApplication 124 | @EnableTurbineStream 125 | @EnableHystrixDashboard 126 | public class MonitoringApplication { 127 | 128 | public static void main(String[] args) { 129 | SpringApplication.run(MonitoringApplication.class, args); 130 | } 131 | } 132 | ``` 133 | 134 | * 配置文件 135 | 136 | ``` 137 | eureka: 138 | instance: 139 | prefer-ip-address: true 140 | client: 141 | serviceUrl: 142 | defaultZone: http://EUREKA_HOST:EUREKA_PORT/eureka/ 143 | 144 | spring: 145 | application: 146 | name: hystrix-monitor 147 | rabbitmq: 148 | host: rabbitmq 149 | ``` 150 | 151 | EUREKA_HOST:注册中心ip 152 | 153 | EUREKA_PORT:注册中心端口 154 | 155 | * 访问地址 156 | 157 | http://DOCKER_HOST:8080/hystrix 页面中加入 http://DOCKER_HOST:8989/ 158 | -------------------------------------------------------------------------------- /READMORE_CN/netflix-ribbon.md: -------------------------------------------------------------------------------- 1 | ## 进一步了解Netflix Ribbon 2 | 3 | Ribbon是一个客户端负载均衡器,有多种负载均衡策略可选(包括自定义的负载均衡算法),并可配合服务发现及断路器使用。在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询、随机连接等)去连接这些机器。 4 | 5 | Ribbon的主要特点包括:1)负载均衡,2)容错,3)在异步和反应模型中支持多协议(HTT、TCP、UDP),4)缓存和批处理 6 | 7 | #### 创建Ribbon service 8 | 9 | * 创建一个mvn工程,起名为ribbon,核心依赖如下: 10 | 11 | ``` 12 | 13 | org.springframework.cloud 14 | spring-cloud-starter-ribbon 15 | 16 | ``` 17 | 18 | * 程序的入口Application类 19 | 20 | ``` 21 | @SpringBootApplication 22 | @EnableDiscoveryClient 23 | public class RibbonApplication { 24 | 25 | @Bean 26 | @LoadBalanced 27 | RestTemplate restTemplate() { 28 | return new RestTemplate(); 29 | } 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(RibbonApplication.class, args); 33 | } 34 | } 35 | ``` 36 | 37 | @LoadBalanced:声明一个loadBalanced模版 38 | 39 | * 创建一个远程调用服务 40 | 41 | ``` 42 | @RestController 43 | public class DemoController { 44 | 45 | @Autowired 46 | RestTemplate restTemplate; 47 | 48 | @RequestMapping(value = "/ribbon", method = RequestMethod.GET) 49 | public String add() { 50 | return restTemplate.getForEntity("http://EUREKA-SERVICE/demo/show", String.class).getBody(); 51 | } 52 | } 53 | ``` 54 | 55 | EUREKA-SERVICE: 在eureka模块中注册的服务 56 | 57 | 远程调用/demo/show这个rest接口,也可以改成/demo/index 等 58 | 59 | * 配置文件 60 | 61 | ``` 62 | spring.application.name=ribbon 63 | server.port=5000 64 | eureka.client.serviceUrl.defaultZone=http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 65 | eureka.instance.preferIpAddress=true 66 | ``` 67 | 68 | EUREKA_HOST:注册中心ip 69 | 70 | EUREKA_PORT:注册中心端口 71 | 72 | * 访问地址 73 | 74 | http://DOCKER_HOST:DOCKER_PORT/ribbon 75 | -------------------------------------------------------------------------------- /READMORE_CN/netflix-zuul.md: -------------------------------------------------------------------------------- 1 | ## 进一步了解Netflix Zuul 2 | 3 | Netflix Zuul提供动态路由、监控、弹性、安全等的边缘服务。 4 | 5 | 在通过服务网关统一向外的提供REST API的微服务架构中,Netflix Zuul为微服务机构提供了前门保护的作用,同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面,使得服务集群主体能够具备更高的可复用性和可测试性。 6 | 7 | #### 创建zuul service 8 | 9 | * 创建一个mvn工程,起名为zuul,核心依赖: 10 | 11 | ``` 12 | 13 | org.springframework.cloud 14 | spring-cloud-starter-zuul 15 | 16 | ``` 17 | 18 | * 在程序的入口Application类加上@EnableZuulProxy注解开启配置服务器 19 | 20 | ``` 21 | @SpringBootApplication 22 | @EnableDiscoveryClient 23 | @EnableZuulProxy 24 | public class GatewayApplication { 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(GatewayApplication.class, args); 28 | } 29 | } 30 | ``` 31 | 32 | * 配置文件 33 | 34 | ``` 35 | server: 36 | port: 5000 37 | 38 | spring: 39 | application: 40 | name: zuul 41 | 42 | eureka: 43 | client: 44 | service-url: 45 | defaultZone: http://${EUREKA_HOST}:${EUREKA_PORT}/eureka/ 46 | 47 | hystrix: 48 | command: 49 | default: 50 | execution: 51 | isolation: 52 | thread: 53 | timeoutInMilliseconds: 20000 54 | 55 | ribbon: 56 | ReadTimeout: 20000 57 | ConnectTimeout: 20000 58 | 59 | zuul: 60 | ignoredServices: '*' 61 | host: 62 | connect-timeout-millis: 20000 63 | socket-timeout-millis: 20000 64 | 65 | routes: 66 | routes: 67 | demo: 68 | path: /demo/** 69 | serviceId: eureka-service 70 | stripPrefix: false 71 | sensitiveHeaders: Cookie,Set-Cookie,Authorization 72 | user: 73 | path: /user/** 74 | serviceId: eureka-service 75 | stripPrefix: false 76 | sensitiveHeaders: Cookie,Set-Cookie,Authorization 77 | outer: 78 | path: /baidu/** 79 | url: http://www.baidu.com 80 | ``` 81 | 82 | EUREKA_HOST:注册中心ip 83 | 84 | EUREKA_PORT:注册中心端口 85 | 86 | * 访问地址 87 | 88 | http://DOCKER_HOST:DOCKER_PORT/feign 89 | -------------------------------------------------------------------------------- /READMORE_CN/spring-cloud-config.md: -------------------------------------------------------------------------------- 1 | ## 进一步了解Spring Cloud Config 2 | 3 | 在分布式系统中,Spring Cloud Config通过config-server(服务端)和config-client(客户端)提供可扩展的配置服务,并利用配置服务中心集中管理所有服务的各种环境配置文件。Spring Cloud Config基于使用中心配置仓库的思想(版本控制),支持Git(默认)、SVN、File等三种储存方式。 4 | 5 | #### 创建Config Server 6 | 7 | * 创建一个mvn工程,起名为config-server,核心依赖: 8 | 9 | ``` 10 | 11 | org.springframework.boot 12 | spring-boot-starter-test 13 | test 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-config-server 18 | 19 | ``` 20 | 21 | * 在程序的入口Application类加上@EnableConfigServer注解开启配置服务器。 22 | 23 | ``` 24 | @EnableConfigServer 25 | @SpringBootApplication 26 | public class Application { 27 | 28 | public static void main(String[] args) { 29 | new SpringApplicationBuilder(Application.class).web(true).run(args); 30 | } 31 | } 32 | ``` 33 | 34 | * 配置文件 35 | 36 | ``` 37 | server: 38 | port: 8888 39 | 40 | spring: 41 | application: 42 | name: config-server 43 | cloud: 44 | config: 45 | server: 46 | git: 47 | uri: ## 配置文件所存放的git地址 48 | searchPaths: config ## 寻找路径 49 | 50 | ``` 51 | 52 | 获取git上的资源信息遵循如下规则: 53 | 54 | ``` 55 | /{application}/{profile}[/{label}] 56 | /{application}-{profile}.yml 57 | /{label}/{application}-{profile}.yml 58 | /{application}-{profile}.properties 59 | /{label}/{application}-{profile}.properties 60 | ``` 61 | 62 | #### 创建Config client 63 | 64 | * 创建一个mvn工程,起名为config-client,核心依赖: 65 | 66 | ``` 67 | 68 | org.springframework.cloud 69 | spring-cloud-config-client 70 | 71 | ``` 72 | 73 | * 在程序的入口Application类加上@EnableConfigServer注解开启配置服务器 74 | 75 | ``` 76 | @SpringBootApplication 77 | public class Application { 78 | 79 | public static void main(String[] args) { 80 | new SpringApplicationBuilder(Application.class).web(true).run(args); 81 | } 82 | } 83 | ``` 84 | 85 | * 创建一个restful接口访问配置文件中属性 86 | 87 | ``` 88 | @EnableAutoConfiguration 89 | @RefreshScope 90 | @RestController 91 | public class DemoController { 92 | 93 | @Value("${from}") 94 | String from; 95 | 96 | 97 | @RequestMapping("/from") 98 | public String from() { 99 | return this.from; 100 | } 101 | } 102 | ``` 103 | 104 | * 配置文件 105 | 106 | ``` 107 | server: 108 | port: 9000 109 | 110 | spring: 111 | application: 112 | name: config-client 113 | profiles: 114 | active: dev 115 | cloud: 116 | config: 117 | uri: http://${CONFIG_HOST}:${CONFIG_PORT} 118 | label: master 119 | ``` 120 | 121 | * 访问地址 122 | 123 | http://DOCKER_HOST:DOCKER_PORT/from 124 | -------------------------------------------------------------------------------- /image/app-lifecycle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/app-lifecycle.png -------------------------------------------------------------------------------- /image/business-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/business-architecture.png -------------------------------------------------------------------------------- /image/components-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/components-architecture.png -------------------------------------------------------------------------------- /image/config-ha.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/config-ha.png -------------------------------------------------------------------------------- /image/fault-tolerance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/fault-tolerance.png -------------------------------------------------------------------------------- /image/logs-collection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/logs-collection.png -------------------------------------------------------------------------------- /image/monitoring-system.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/monitoring-system.png -------------------------------------------------------------------------------- /image/piggymetrics-k8s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/piggymetrics-k8s.png -------------------------------------------------------------------------------- /image/pm-spring-cloud-config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/pm-spring-cloud-config.png -------------------------------------------------------------------------------- /image/production-environment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/production-environment.png -------------------------------------------------------------------------------- /image/register-and-discovery-mechanism.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudframeworks-springcloud/user-guide-springcloud/f9eddfcb55594ef0b58a14e12066428c1d87abbe/image/register-and-discovery-mechanism.png -------------------------------------------------------------------------------- /yaml/deployment/account-mongodb-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: account-mongodb 5 | namespace: springcloud 6 | labels: 7 | name: account-mongodb 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: account-mongodb 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: account-mongodb 23 | image: goodraincloudframeworks/piggymetrics-mongodb 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 27017 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 500m 36 | memory: 500Mi 37 | limits: 38 | cpu: 2000m 39 | memory: 2000Mi 40 | -------------------------------------------------------------------------------- /yaml/deployment/account-service-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: account-service 5 | namespace: springcloud 6 | labels: 7 | name: account-service 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: account-service 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: account-service 23 | image: goodraincloudframeworks/piggymetrics-account-service 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 6000 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/auth-mongodb-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: auth-mongodb 5 | namespace: springcloud 6 | labels: 7 | name: auth-mongodb 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: auth-mongodb 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: auth-mongodb 23 | image: goodraincloudframeworks/piggymetrics-mongodb 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 27017 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 500m 36 | memory: 500Mi 37 | limits: 38 | cpu: 2000m 39 | memory: 2000Mi -------------------------------------------------------------------------------- /yaml/deployment/auth-service-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: auth-service 5 | namespace: springcloud 6 | labels: 7 | name: auth-service 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: auth-service 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: auth-service 23 | image: goodraincloudframeworks/piggymetrics-auth-service 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 5000 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/config-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: config 5 | namespace: springcloud 6 | labels: 7 | name: config 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: config 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: config 23 | image: goodraincloudframeworks/piggymetrics-config 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 8888 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/gateway-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: gateway 5 | namespace: springcloud 6 | labels: 7 | name: gateway 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: gateway 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: gateway 23 | image: goodraincloudframeworks/piggymetrics-gateway 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 4000 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/monitoring-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: monitoring 5 | namespace: springcloud 6 | labels: 7 | name: monitoring 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: monitoring 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: monitoring 23 | image: goodraincloudframeworks/piggymetrics-monitoring 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 8080 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/notification-mongodb-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: notification-mongodb 5 | namespace: springcloud 6 | labels: 7 | name: notification-mongodb 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: notification-mongodb 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: notification-mongodb 23 | image: goodraincloudframeworks/piggymetrics-mongodb 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 27017 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 500m 36 | memory: 500Mi 37 | limits: 38 | cpu: 2000m 39 | memory: 2000Mi -------------------------------------------------------------------------------- /yaml/deployment/notification-service-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: notification-service 5 | namespace: springcloud 6 | labels: 7 | name: notification-service 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: notification-service 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: notification-service 23 | image: goodraincloudframeworks/piggymetrics-notification-service 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 8000 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/rabbitmq-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: rabbitmq 5 | namespace: springcloud 6 | labels: 7 | name: rabbitmq 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: rabbitmq 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: rabbitmq 23 | image: rabbitmq:3-management 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 15672 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/registry-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: registry 5 | namespace: springcloud 6 | labels: 7 | name: registry 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: registry 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: registry 23 | image: goodraincloudframeworks/piggymetrics-registry 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 8761 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/deployment/statistics-mongodb-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: statistics-mongodb 5 | namespace: springcloud 6 | labels: 7 | name: statistics-mongodb 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: statistics-mongodb 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: statistics-mongodb 23 | image: goodraincloudframeworks/piggymetrics-mongodb 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 27017 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 500m 36 | memory: 500Mi 37 | limits: 38 | cpu: 2000m 39 | memory: 2000Mi -------------------------------------------------------------------------------- /yaml/deployment/statistics-service-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: statistics-service 5 | namespace: springcloud 6 | labels: 7 | name: statistics-service 8 | spec: 9 | replicas: 1 10 | revisionHistoryLimit: 1 11 | strategy: 12 | rollingUpdate: 13 | maxSurge: 1 14 | maxUnavailable: 1 15 | template: 16 | metadata: 17 | labels: 18 | name: statistics-service 19 | spec: 20 | terminationGracePeriodSeconds: 120 21 | containers: 22 | - name: statistics-service 23 | image: goodraincloudframeworks/piggymetrics-statistics-service 24 | imagePullPolicy: IfNotPresent 25 | readinessProbe: 26 | failureThreshold: 3 27 | initialDelaySeconds: 30 28 | periodSeconds: 10 29 | successThreshold: 1 30 | tcpSocket: 31 | port: 15672 32 | timeoutSeconds: 5 33 | resources: 34 | requests: 35 | cpu: 100m 36 | memory: 300Mi 37 | limits: 38 | cpu: 1000m 39 | memory: 1000Mi -------------------------------------------------------------------------------- /yaml/dns/dns-addon.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: kube-dns 5 | namespace: kube-system 6 | labels: 7 | k8s-app: kube-dns 8 | kubernetes.io/cluster-service: "true" 9 | kubernetes.io/name: "KubeDNS" 10 | spec: 11 | selector: 12 | k8s-app: kube-dns 13 | clusterIP: ${DNS_SERVICE_IP} 14 | ports: 15 | - name: dns 16 | port: 53 17 | protocol: UDP 18 | - name: dns-tcp 19 | port: 53 20 | protocol: TCP 21 | 22 | 23 | --- 24 | 25 | 26 | apiVersion: v1 27 | kind: ReplicationController 28 | metadata: 29 | name: kube-dns-v20 30 | namespace: kube-system 31 | labels: 32 | k8s-app: kube-dns 33 | version: v20 34 | kubernetes.io/cluster-service: "true" 35 | spec: 36 | replicas: 1 37 | selector: 38 | k8s-app: kube-dns 39 | version: v20 40 | template: 41 | metadata: 42 | labels: 43 | k8s-app: kube-dns 44 | version: v20 45 | annotations: 46 | scheduler.alpha.kubernetes.io/critical-pod: '' 47 | scheduler.alpha.kubernetes.io/tolerations: '[{"key":"CriticalAddonsOnly", "operator":"Exists"}]' 48 | spec: 49 | containers: 50 | - name: kubedns 51 | image: gcr.io/google_containers/kubedns-amd64:1.8 52 | resources: 53 | limits: 54 | memory: 170Mi 55 | requests: 56 | cpu: 100m 57 | memory: 70Mi 58 | livenessProbe: 59 | httpGet: 60 | path: /healthz-kubedns 61 | port: 8080 62 | scheme: HTTP 63 | initialDelaySeconds: 60 64 | timeoutSeconds: 5 65 | successThreshold: 1 66 | failureThreshold: 5 67 | readinessProbe: 68 | httpGet: 69 | path: /readiness 70 | port: 8081 71 | scheme: HTTP 72 | initialDelaySeconds: 3 73 | timeoutSeconds: 5 74 | args: 75 | - --domain=cluster.local. 76 | - --dns-port=10053 77 | ports: 78 | - containerPort: 10053 79 | name: dns-local 80 | protocol: UDP 81 | - containerPort: 10053 82 | name: dns-tcp-local 83 | protocol: TCP 84 | - name: dnsmasq 85 | image: gcr.io/google_containers/kube-dnsmasq-amd64:1.4 86 | livenessProbe: 87 | httpGet: 88 | path: /healthz-dnsmasq 89 | port: 8080 90 | scheme: HTTP 91 | initialDelaySeconds: 60 92 | timeoutSeconds: 5 93 | successThreshold: 1 94 | failureThreshold: 5 95 | args: 96 | - --cache-size=1000 97 | - --no-resolv 98 | - --server=127.0.0.1#10053 99 | - --log-facility=- 100 | ports: 101 | - containerPort: 53 102 | name: dns 103 | protocol: UDP 104 | - containerPort: 53 105 | name: dns-tcp 106 | protocol: TCP 107 | - name: healthz 108 | image: gcr.io/google_containers/exechealthz-amd64:1.2 109 | resources: 110 | limits: 111 | memory: 50Mi 112 | requests: 113 | cpu: 10m 114 | memory: 50Mi 115 | args: 116 | - --cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1 >/dev/null 117 | - --url=/healthz-dnsmasq 118 | - --cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1:10053 >/dev/null 119 | - --url=/healthz-kubedns 120 | - --port=8080 121 | - --quiet 122 | ports: 123 | - containerPort: 8080 124 | protocol: TCP 125 | dnsPolicy: Default -------------------------------------------------------------------------------- /yaml/svc/account-mongodb-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: account-mongodb 6 | name: account-mongodb 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 27017 12 | protocol: TCP 13 | targetPort: 27017 14 | selector: 15 | name: account-mongodb -------------------------------------------------------------------------------- /yaml/svc/account-service-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: account-service 6 | name: account-service 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 6000 12 | protocol: TCP 13 | targetPort: 6000 14 | selector: 15 | name: account-service -------------------------------------------------------------------------------- /yaml/svc/auth-mongodb-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: auth-mongodb 6 | name: auth-mongodb 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 27017 12 | protocol: TCP 13 | targetPort: 27017 14 | selector: 15 | name: auth-mongodb -------------------------------------------------------------------------------- /yaml/svc/auth-service-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: auth-service 6 | name: auth-service 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 5000 12 | protocol: TCP 13 | targetPort: 5000 14 | selector: 15 | name: auth-service -------------------------------------------------------------------------------- /yaml/svc/config-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: config 6 | name: config 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 8888 12 | protocol: TCP 13 | targetPort: 8888 14 | selector: 15 | name: config -------------------------------------------------------------------------------- /yaml/svc/gateway-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: gateway 6 | name: gateway 7 | namespace: springcloud 8 | spec: 9 | type: NodePort 10 | ports: 11 | - port: 4000 12 | protocol: TCP 13 | targetPort: 4000 14 | nodePort: 80 15 | selector: 16 | name: gateway -------------------------------------------------------------------------------- /yaml/svc/monitoring-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: monitoring 6 | name: monitoring 7 | namespace: springcloud 8 | spec: 9 | type: NodePort 10 | ports: 11 | - port: 8080 12 | protocol: TCP 13 | targetPort: 8080 14 | nodePort: 8080 15 | - port: 8089 16 | protocol: TCP 17 | targetPort: 8089 18 | nodePort: 8080 19 | selector: 20 | name: monitoring -------------------------------------------------------------------------------- /yaml/svc/notification-mongodb-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: notification-mongodb 6 | name: notification-mongodb 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 27017 12 | protocol: TCP 13 | targetPort: 27017 14 | selector: 15 | name: notification-mongodb -------------------------------------------------------------------------------- /yaml/svc/notification-service-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: notification-service 6 | name: notification-service 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 8000 12 | protocol: TCP 13 | targetPort: 8000 14 | selector: 15 | name: notification-service -------------------------------------------------------------------------------- /yaml/svc/rabbitmq-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: rabbitmq 6 | name: rabbitmq 7 | namespace: springcloud 8 | spec: 9 | type: NodePort 10 | ports: 11 | - port: 5671 12 | protocol: TCP 13 | targetPort: 5671 14 | - port: 15672 15 | protocol: TCP 16 | targetPort: 15672 17 | nodePort: 15672 18 | selector: 19 | name: rabbitmq -------------------------------------------------------------------------------- /yaml/svc/registry-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: registry 6 | name: registry 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 8761 12 | protocol: TCP 13 | targetPort: 8761 14 | selector: 15 | name: registry -------------------------------------------------------------------------------- /yaml/svc/statistics-mongodb-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: statistics-mongodb 6 | name: statistics-mongodb 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 27017 12 | protocol: TCP 13 | targetPort: 27017 14 | selector: 15 | name: statistics-mongodb -------------------------------------------------------------------------------- /yaml/svc/statistics-service-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | name: statistics-service 6 | name: statistics-service 7 | namespace: springcloud 8 | spec: 9 | type: ClusterIP 10 | ports: 11 | - port: 8888 12 | protocol: TCP 13 | targetPort: 8888 14 | selector: 15 | name: statistics-service --------------------------------------------------------------------------------