├── .gitignore ├── README.md ├── boot-admin-server ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── boot │ │ └── admin │ │ └── server │ │ └── BootAdminServerApplication.java │ └── resources │ └── application.properties ├── cloud-config-server ├── README.md ├── configserver.yml ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── boot │ │ └── config │ │ └── server │ │ └── CloudConfigServerApplication.java │ └── resources │ └── application.properties ├── cloud-data-flow-server ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── boot │ │ └── data │ │ └── flow │ │ └── CloudDataFlowServerApplication.java │ └── resources │ └── application.yml ├── cloud-gateway ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── gateway │ │ └── CloudGatewayApplication.java │ └── resources │ └── application.yml ├── cloud-registry-server ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── boot │ │ └── registry │ │ └── server │ │ └── CloudRegistryServerApplication.java │ └── resources │ └── application.yml ├── cloud-skipper-server ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── boot │ │ └── skipper │ │ └── server │ │ └── CloudSkipperServerApplication.java │ └── resources │ └── application.properties ├── docker-compose.yml ├── microapp-1 ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── mvnsearch │ │ │ └── app1 │ │ │ ├── Microapp1Application.java │ │ │ └── PortalController.java │ └── resources │ │ ├── application.properties │ │ ├── bootstrap.properties │ │ └── logback-spring.xml │ └── test │ ├── java │ └── org │ │ └── mvnsearch │ │ └── app1 │ │ └── Microapp1ApplicationTests.java │ └── resources │ └── logback-test.xml ├── microapp-2 ├── README.md ├── import_data.sh ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── mvnsearch │ │ │ └── app2 │ │ │ ├── Microapp2Application.java │ │ │ └── PortalController.java │ └── resources │ │ ├── application.properties │ │ ├── bootstrap.properties │ │ └── logback-spring.xml │ └── test │ ├── java │ └── org │ │ └── mvnsearch │ │ └── app2 │ │ └── Microapp2ApplicationTests.java │ └── resources │ └── logback-test.xml ├── microapp-3 ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── app1 │ │ ├── Microapp3Application.java │ │ └── PortalController.java │ └── resources │ ├── application.properties │ ├── bootstrap.properties │ └── logback-spring.xml ├── microapp-hystrix ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── mvnsearch │ │ │ └── app4 │ │ │ ├── MicroappHystrixApplication.java │ │ │ ├── UserController.java │ │ │ └── domain │ │ │ ├── infra │ │ │ ├── HttpBinAutoConfiguration.java │ │ │ ├── HttpBinResponse.java │ │ │ ├── HttpBinServiceFeignAPI.java │ │ │ ├── HttpBinServiceRetrofitAPI.java │ │ │ └── retrofit │ │ │ │ ├── HystrixCallAdapter.java │ │ │ │ └── HystrixCallAdapterFactory.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ └── resources │ │ ├── application.properties │ │ ├── bootstrap.properties │ │ └── logback-spring.xml │ └── test │ └── java │ └── org │ └── mvnsearch │ └── app4 │ ├── HttpRequestTest.java │ ├── HystrixApplicationBaseTest.java │ └── domain │ └── infra │ ├── HttpBinServiceFeignAPITest.java │ └── HttpBinServiceRetrofitAPITest.java ├── microapp-proxy ├── README.md ├── pom.xml ├── proxy.http └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ └── proxy │ │ ├── MicroProxyApplication.java │ │ └── PortalController.java │ └── resources │ └── application.properties ├── pom.xml ├── sidecar-1 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── mvnsearch │ │ │ └── Sidecar1Application.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── org │ └── mvnsearch │ └── Sidecar1ApplicationTests.java ├── task-demo ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── mvnsearch │ │ ├── DemoTaskApplication.java │ │ └── TaskListener.java │ └── resources │ └── application.properties └── zuul-proxy ├── pom.xml └── src └── main ├── java └── org │ └── mvnsearch │ ├── ZuulProxyDemoApplication.java │ ├── controller │ ├── PortalController.java │ └── SpikeController.java │ └── filter │ └── AuthFilter.java └── resources └── application.yml /.gitignore: -------------------------------------------------------------------------------- 1 | ### application tempates 2 | *.log 3 | 4 | # Created by .ignore support plugin (hsz.mobi) 5 | ### Java template 6 | *.class 7 | 8 | # Mobile Tools for Java (J2ME) 9 | .mtj.tmp/ 10 | 11 | # Package Files # 12 | *.jar 13 | *.war 14 | *.ear 15 | 16 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 17 | hs_err_pid* 18 | ### JetBrains template 19 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 20 | 21 | *.iml 22 | 23 | ## Directory-based project format: 24 | .idea/ 25 | # if you remove the above rule, at least ignore the following: 26 | 27 | # User-specific stuff: 28 | # .idea/workspace.xml 29 | # .idea/tasks.xml 30 | # .idea/dictionaries 31 | 32 | # Sensitive or high-churn files: 33 | # .idea/dataSources.ids 34 | # .idea/dataSources.xml 35 | # .idea/sqlDataSources.xml 36 | # .idea/dynamic.xml 37 | # .idea/uiDesigner.xml 38 | 39 | # Gradle: 40 | # .idea/gradle.xml 41 | # .idea/libraries 42 | 43 | # Mongo Explorer plugin: 44 | # .idea/mongoSettings.xml 45 | 46 | ## File-based project format: 47 | *.ipr 48 | *.iws 49 | 50 | ## Plugin-specific files: 51 | 52 | # IntelliJ 53 | /out/ 54 | 55 | # mpeltonen/sbt-idea plugin 56 | .idea_modules/ 57 | 58 | # JIRA plugin 59 | atlassian-ide-plugin.xml 60 | 61 | # Crashlytics plugin (for Android Studio and IntelliJ) 62 | com_crashlytics_export_strings.xml 63 | crashlytics.properties 64 | crashlytics-build.properties 65 | 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring Boot MicroServices 2 | ================================== 3 | 4 | Spring Boot 1.5.x MicroServices 5 | 6 | ### environment 7 | 8 | please install Java 8 JCE: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html 9 | 10 | ### Services 11 | 12 | * Config Server 13 | * Eureka Registry Server 14 | * Spring Boot Admin Server 15 | * Spring Cloud DataFlow Server 16 | * Spring Cloud Skipper Ser 17 | * Spring Cloud Gateway 18 | * Zull Reverse Proxy 19 | * Charon Reverse Proxy 20 | * Spring Cloud Hystrix 21 | 22 | ### How to start services 23 | 24 | * Start Eureka 25 | * Start Config Server(integrated with eureka) 26 | * Start Spring Boot Admin (integrated with eureka) 27 | 28 | ### Maven plugins 29 | 30 | * maven-compiler-plugin: 1.8 compile 31 | * spring-boot-maven-plugin: build-info 32 | * git-commit-id-plugin: git full mode 33 | 34 | ###Spring Cloud Consul 35 | 36 | #### Startup 37 | 38 | * docker-compose up -d 39 | * visit http://localhost:8500/ui/ for Consul 40 | * visit http://localhost:9411/zipkin/ for Zipkin 41 | 42 | #### Consul Configuration 43 | 44 | Consul Key/Value支持的Spring config Server目录结构如下: 45 | 46 | config/testApp,dev/ 47 | config/testApp/ 48 | config/application,dev/ 49 | config/application/ 50 | 51 | 接下来就是在这些目录下创建对应的Key/Value。 52 | 53 | * MicroServices with Consul: https://www.infoq.com/articles/Microservices-SpringBoot 54 | 55 | ### Tips 56 | 57 | * 不同Spring Boot加载的配置问题,可以通过 --spring.profiles.active=15X 来激活全局的 application-15X.properties这样 58 | 59 | ### Reference 60 | 61 | * Spring Cloud: http://cloud.spring.io/spring-cloud-static/Camden.SR3/ 62 | * Spring Cloud Netflix: http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.2.3.RELEASE/ 63 | * Spring Cloud Consul: http://cloud.spring.io/spring-cloud-consul/ 64 | * Spring Cloud ZooKeeper: http://cloud.spring.io/spring-cloud-zookeeper/spring-cloud-zookeeper.html 65 | * Spring Cloud CLI Launcher: https://spring.io/blog/2016/11/02/introducing-the-spring-cloud-cli-launcher 66 | -------------------------------------------------------------------------------- /boot-admin-server/README.md: -------------------------------------------------------------------------------- 1 | Spring Boot Admin Server 2 | ================================== 3 | Admin UI for administration of spring boot applications. 4 | 5 | ### how to start server 6 | Spring Boot Admin Server integrates with Eureka to fetch applications. 7 | 8 | * Start Eureka Server first 9 | * Start Spring Boot Admin Server. 10 | 11 | ### How to use in applications 12 | Spring Boot Admin has been integrated with Eureka Registry Server, and Boot Admin will fetch all applications from registry server. 13 | 14 | * If you want to enable JMX-HTTP support, such log view, please add Jolokia dependency: 15 | ```xml 16 | 17 | org.jolokia 18 | jolokia-core 19 | 20 | ``` 21 | * Add JMX support in your logback-spring.xml: 22 | ```xml 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | ``` 40 | * Open browser to visit boot admin server and check: http://localhost:9761 41 | 42 | ### references 43 | 44 | * Spring Boot Admin Security: https://dzone.com/articles/a-look-at-spring-boot-admin 45 | * Spring Boot Admin Docs: http://codecentric.github.io/spring-boot-admin/1.4.4/ 46 | * Spring Boot Admin Git: https://github.com/codecentric/spring-boot-admin 47 | -------------------------------------------------------------------------------- /boot-admin-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | boot-admin-server 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | boot-admin-server 17 | Spring Boot admin Server 18 | 19 | 1.5.7 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-actuator 26 | 27 | 28 | de.codecentric 29 | spring-boot-admin-server 30 | ${spring-boot-admin.version} 31 | 32 | 33 | de.codecentric 34 | spring-boot-admin-server-ui 35 | ${spring-boot-admin.version} 36 | 37 | 38 | de.codecentric 39 | spring-boot-admin-server-ui-hystrix 40 | ${spring-boot-admin.version} 41 | 42 | 43 | de.codecentric 44 | spring-boot-admin-server-ui-turbine 45 | ${spring-boot-admin.version} 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-starter-eureka 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-starter-test 54 | test 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /boot-admin-server/src/main/java/org/mvnsearch/boot/admin/server/BootAdminServerApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.boot.admin.server; 2 | 3 | import de.codecentric.boot.admin.config.EnableAdminServer; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | 8 | @SpringBootApplication 9 | @EnableAdminServer 10 | @EnableDiscoveryClient 11 | public class BootAdminServerApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(BootAdminServerApplication.class, args); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /boot-admin-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=9761 2 | spring.application.name=SpringBootAdminServer 3 | management.security.enabled=false 4 | zuul.forceOriginalQueryStringEncoding=true 5 | spring.boot.admin.routes.endpoints= env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream 6 | 7 | spring.boot.admin.turbine.location=turbine 8 | spring.boot.admin.turbine.clusters=default 9 | 10 | ### eureka 11 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 12 | eureka.client.fetch-registry=true 13 | eureka.instance.lease-renewal-interval-in-seconds=10 14 | eureka.client.registry-fetch-interval-seconds=5 -------------------------------------------------------------------------------- /cloud-config-server/README.md: -------------------------------------------------------------------------------- 1 | Cloud Config Server 2 | ===================================== 3 | Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. 4 | 5 | ### Settings 6 | 7 | Please change git url in application.properties, and you can check by http://localhost:8888/configserver/default 8 | 9 | ### How to use 10 | 11 | * please add dependency in your pom.xml. 12 | ```xml 13 | 14 | 15 | org.springframework.cloud 16 | spring-cloud-starter-config 17 | 18 | ``` 19 | * Bootstrap Environment from server. In your bootstrap.properties add following code: 20 | ``` 21 | spring.cloud.config.uri: http://localhost:8888/ 22 | ``` 23 | * Verify your properties: 24 | ``` 25 | $ curl http://localhost:8888/demo/master 26 | $ curl http://localhost:8888/demo/prod/master 27 | ``` 28 | * add @RefreshScope for your bean 29 | * Change properties and push them to git repository 30 | * Invoke 'curl -X POST http://localhost:8080/refresh' to refresh config 31 | 32 | ### Testing 33 | Config Server with Git adopts following rules to get configuration: 34 | 35 | * properties or yml: "/{name}/{profile}/{label}": name is application name, profile is Spring profile name, and label is git branch or tag name. 36 | 37 | 38 | $ http http://localhost:8888/demo/default/master 39 | 40 | 41 | * plain text: "/{name}/{profile}/{label}/{path}" 42 | 43 | $ curl http://localhost:8888/demo/default/master/demo.conf 44 | $ curl http://localhost:8888/demo/default/master/hibernate/demo.xml 45 | 46 | ### how to write properties file 47 | 48 | 1. application.properties: global properties for all apps 49 | 2. appname.properties: app properties 50 | 3. appname-profile.properties: app profile properties 51 | 52 | ### How to integrate with Eureka 53 | 54 | * Add spring-cloud-starter-eureka dependency in your pom.xml: 55 | ```xml 56 | 57 | org.springframework.cloud 58 | spring-cloud-starter-eureka 59 | 60 | ``` 61 | * Add @EnableDiscoveryClient in your applictaion 62 | * add property in the applicaton.properties 63 | ```properties 64 | eureka.instance.prefer-ip-address=true 65 | eureka.client.fetch-registry=false 66 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 67 | ``` 68 | 69 | ### Spring Cloud CLI Launcher 70 | 71 | please copy configserver.yml to $HOME/.spring-cloud/configserver.yml and execute: 72 | 73 | $ spring cloud configserver 74 | 75 | ### 支持的git url 76 | 77 | * file: file:///${user.home}/config-repo 78 | * git https url: https://github.com/myorg/{application} 79 | * git url: git@gitlab.yourdomain.com:myorg/config-repo 80 | 81 | ### References 82 | 83 | * Spring Config Server: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html 84 | 85 | -------------------------------------------------------------------------------- /cloud-config-server/configserver.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: git 4 | cloud: 5 | config: 6 | server: 7 | git: 8 | uri: https://github.com/spring-cloud-samples/config-repo 9 | encrypt: 10 | key: 123456 -------------------------------------------------------------------------------- /cloud-config-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | cloud-config-server 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | cloud-config-server 17 | Spring Boot Cloud Config Server 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-actuator 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-config-server 31 | 32 | 33 | org.jolokia 34 | jolokia-core 35 | 36 | 37 | org.springframework.cloud 38 | spring-cloud-starter-eureka 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /cloud-config-server/src/main/java/org/mvnsearch/boot/config/server/CloudConfigServerApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.boot.config.server; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.config.server.EnableConfigServer; 7 | 8 | @SpringBootApplication 9 | @EnableConfigServer 10 | @EnableDiscoveryClient 11 | public class CloudConfigServerApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(CloudConfigServerApplication.class, args); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /cloud-config-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=configserver 2 | server.port=8888 3 | management.port=8889 4 | encrypt.key=abc123 5 | 6 | ### git config 7 | spring.cloud.config.server.git.clone-on-start=true 8 | spring.cloud.config.server.git.uri=https://github.com/linux-china/config-repo 9 | spring.cloud.config.server.health.enabled=false 10 | 11 | ### ops 12 | eureka.instance.prefer-ip-address=true 13 | eureka.client.fetch-registry=false 14 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 15 | eureka.instance.metadata-map.management.port=${management.port:-${server.port}} -------------------------------------------------------------------------------- /cloud-data-flow-server/README.md: -------------------------------------------------------------------------------- 1 | Spring Cloud Data Flow Server 2 | ============================= 3 | Spring Cloud DataFlow Server for Stream and Task/Batch Applications. 4 | 5 | ### Development 6 | Please visit http://localhost:9393/dashboard/index.html after start. 7 | 8 | ### Reference 9 | 10 | * DataFlow Server: http://docs.spring.io/spring-cloud-dataflow/docs/1.0.1.RELEASE/reference/html/ 11 | * Spring Cloud Stream App Starters: http://cloud.spring.io/spring-cloud-stream-app-starters/ -------------------------------------------------------------------------------- /cloud-data-flow-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | cloud-data-flow-server 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | cloud-data-flow-server 17 | Spring Boot Cloud Data flow Server 18 | 19 | 20 | 1.7.4.RELEASE 21 | 1.2.4.RELEASE 22 | 5.2.17.Final 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-dataflow-server-local 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-task-core 41 | ${cloud-task.version} 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-deployer-resource-support 46 | 1.3.4.RELEASE 47 | 48 | 49 | mysql 50 | mysql-connector-java 51 | 52 | 53 | org.hibernate 54 | hibernate-core 55 | ${hibernate.version} 56 | 57 | 58 | org.hibernate 59 | hibernate-entitymanager 60 | ${hibernate.version} 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | test 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.springframework.cloud 73 | spring-cloud-dataflow-dependencies 74 | ${cloud-dataflow.version} 75 | pom 76 | import 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /cloud-data-flow-server/src/main/java/org/mvnsearch/boot/data/flow/CloudDataFlowServerApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.boot.data.flow; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.dataflow.server.EnableDataFlowServer; 6 | 7 | @SpringBootApplication 8 | @EnableDataFlowServer 9 | public class CloudDataFlowServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(CloudDataFlowServerApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /cloud-data-flow-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: dataflow 4 | datasource: 5 | driver-class-name: com.mysql.jdbc.Driver 6 | username: root 7 | password: 123456 8 | url: jdbc:mysql://localhost:3306/spring_cloud?useSSL=false&useUnicode=true&characterEncoding=UTF-8 9 | redis: 10 | host: 127.0.0.1 11 | cloud: 12 | dataflow: 13 | features: 14 | skipper-enabled: false 15 | server: 16 | port: 9393 17 | -------------------------------------------------------------------------------- /cloud-gateway/README.md: -------------------------------------------------------------------------------- 1 | Spring Cloud Gateway 2 | ==================== 3 | 4 | GETTING STARTED: https://spring.io/guides/gs/gateway/ 5 | 6 | # References 7 | 8 | * Spring Cloud Gateway: https://cloud.spring.io/spring-cloud-gateway/ 9 | 10 | -------------------------------------------------------------------------------- /cloud-gateway/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.mvnsearch 7 | spring-boot-microservices 8 | 1.0.0-SNAPSHOT 9 | 10 | cloud-gateway 11 | 1.0.0-SNAPSHOT 12 | jar 13 | 14 | cloud gateway 15 | Spring Cloud Gateway 16 | 17 | 18 | 19 | org.springframework.cloud 20 | spring-cloud-gateway-mvc 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-actuator 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-gateway 43 | 1.0.3.RELEASE 44 | pom 45 | import 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /cloud-gateway/src/main/java/org/mvnsearch/gateway/CloudGatewayApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.gateway; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.gateway.mvc.ProxyExchange; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @SpringBootApplication 11 | @RestController 12 | public class CloudGatewayApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(CloudGatewayApplication.class, args); 16 | } 17 | 18 | @GetMapping("/test") 19 | public ResponseEntity proxy(ProxyExchange proxy) throws Exception { 20 | return proxy.uri("http://remoteserver/image/png").get(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /cloud-gateway/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7171 3 | 4 | spring: 5 | application: 6 | name: cloud-gateway 7 | -------------------------------------------------------------------------------- /cloud-registry-server/README.md: -------------------------------------------------------------------------------- 1 | Cloud Registry Server 2 | ================================== 3 | Eureka Registry Server, the default port is 8761, please visit http://localhost:8761 4 | 5 | ### How to use 6 | 7 | * Add spring-cloud-starter-eureka dependency in your pom.xml 8 | ```xml 9 | 10 | org.springframework.cloud 11 | spring-cloud-starter-eureka 12 | 13 | ``` 14 | * Add Eureka configuration in your application.yml 15 | ```yaml 16 | eureka: 17 | instance: 18 | prefer-ip-address: true 19 | client: 20 | fetchRegistry: false 21 | serviceUrl: 22 | defaultZone: http://localhost:8761/eureka/ 23 | ``` 24 | or application.properties 25 | ```properties 26 | eureka.instance.prefer-ip-address=true 27 | eureka.client.fetch-registry=false 28 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 29 | ``` 30 | Please notice: you can add multi url in defaultZone to setup cluster, such as "http://dsc01:8761/eureka/,http://dsc02:8762/eureka/" for both client and server. 31 | 32 | * Add @EnableDiscoveryClient for your Spring Boot Application 33 | 34 | ### metadata meta 35 | You can add metadata for Eureka instance, such as following: 36 | ```properties 37 | eureka.instance.metadata-map.management.port=${management.port:-${server.port}} 38 | ``` -------------------------------------------------------------------------------- /cloud-registry-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | cloud-registry-server 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | cloud-registry-server 17 | Spring Boot Cloud Registry Server 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-actuator 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-starter-eureka-server 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /cloud-registry-server/src/main/java/org/mvnsearch/boot/registry/server/CloudRegistryServerApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.boot.registry.server; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class CloudRegistryServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(CloudRegistryServerApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /cloud-registry-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: eureka 4 | 5 | server: 6 | port: 8761 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | registerWithEureka: false 13 | fetchRegistry: false 14 | server: 15 | waitTimeInMsWhenSyncEmpty: 0 -------------------------------------------------------------------------------- /cloud-skipper-server/README.md: -------------------------------------------------------------------------------- 1 | Cloud Skipper Server 2 | ==================== 3 | Spring Cloud Skipper Server to manage spring boot applications on different platforms. 4 | 5 | ### References 6 | 7 | * Spring Skipper Server: https://cloud.spring.io/spring-cloud-skipper/ 8 | 9 | -------------------------------------------------------------------------------- /cloud-skipper-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | cloud-skipper-server 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | cloud-skipper-server 17 | Spring Boot Cloud Skipper Server 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-actuator 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-skipper-server-core 31 | 1.0.10.RELEASE 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-config 36 | 37 | 38 | 39 | 40 | org.jolokia 41 | jolokia-core 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-test 46 | test 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /cloud-skipper-server/src/main/java/org/mvnsearch/boot/skipper/server/CloudSkipperServerApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.boot.skipper.server; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; 7 | import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration; 8 | import org.springframework.cloud.deployer.spi.local.LocalDeployerAutoConfiguration; 9 | import org.springframework.cloud.skipper.server.EnableSkipperServer; 10 | 11 | // LocalDeployerAutoConfiguration.class, 12 | @SpringBootApplication(exclude = { 13 | ManagementWebSecurityAutoConfiguration.class, 14 | SecurityAutoConfiguration.class, 15 | SessionAutoConfiguration.class 16 | }) 17 | @EnableSkipperServer 18 | public class CloudSkipperServerApplication { 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(CloudSkipperServerApplication.class, args); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cloud-skipper-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=skipperserver 2 | server.port=7577 -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | consul: 5 | image: consul:1.0.3 6 | ports: 7 | - "8300:8300" 8 | - "8301:8301" 9 | - "8302:8302" 10 | - "8400:8400" 11 | - "8500:8500" 12 | - "8600:53/udp" 13 | redis: 14 | image: redis:4.0.7-alpine 15 | ports: 16 | - "6379:6379" 17 | # zookeeper: 18 | # image: jplock/zookeeper 19 | # ports: 20 | # - "2181:2181" 21 | # - "2888:2888" 22 | # - "3888:3888" 23 | mysql: 24 | image: mysql:5.7.21 25 | ports: 26 | - "3306:3306" 27 | environment: 28 | MYSQL_ROOT_PASSWORD: 123456 29 | MYSQL_DATABASE: spring_cloud 30 | zipkin: 31 | image: openzipkin/zipkin:1.31.0 32 | ports: 33 | - "9411:9411" -------------------------------------------------------------------------------- /microapp-1/README.md: -------------------------------------------------------------------------------- 1 | MicroApp-1 2 | ========== 3 | MicroApp-1 to test config and eureka integration 4 | -------------------------------------------------------------------------------- /microapp-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | microapp-1 13 | 0.0.1-SNAPSHOT 14 | jar 15 | 16 | microapp-1 17 | Demo project for Spring Boot 18 | 19 | 20 | UTF-8 21 | UTF-8 22 | 1.8 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-devtools 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-starter-config 41 | 42 | 43 | org.jolokia 44 | jolokia-core 45 | 46 | 47 | org.springframework.cloud 48 | spring-cloud-starter-eureka 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-test 53 | test 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /microapp-1/src/main/java/org/mvnsearch/app1/Microapp1Application.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app1; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class Microapp1Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Microapp1Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /microapp-1/src/main/java/org/mvnsearch/app1/PortalController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app1; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.cloud.context.config.annotation.RefreshScope; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * portal controller 10 | * 11 | * @author linux_china 12 | */ 13 | @RestController 14 | @RefreshScope 15 | public class PortalController { 16 | @Value("${developer}") 17 | private String nick; 18 | 19 | @GetMapping("/") 20 | public String index() { 21 | return "hello" + nick; 22 | } 23 | 24 | @GetMapping("/welcome") 25 | public String welcome() { 26 | return "welcome"; 27 | } 28 | 29 | @GetMapping("/demo/welcome") 30 | public String welcome2() { 31 | return "welcome2"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /microapp-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=demo 2 | server.port=8080 3 | management.port=8081 4 | management.info.git.mode=full 5 | 6 | ### application variables 7 | developer=xxx 8 | 9 | #INFO 10 | info.service.version=1.0.0-SNAPSHOT 11 | info.service.description=demo1 product 12 | info.service.tags=demo1 13 | info.team.developer=linux_china 14 | info.team.tester=linux_china 15 | info.team.pe=linux_china 16 | info.scm.url=https://github.com/linux-china/spring-boot-microservices 17 | info.docs.url=https://github.com/linux-china/spring-boot-microservices 18 | 19 | #logging 20 | logging.level.root=error 21 | logging.level.org.springframework.boot=info 22 | logging.level.org.mvnsearch=info 23 | logging.path=${user.home}/logs 24 | logging.file=${spring.application.name}.log 25 | 26 | ### ops 27 | eureka.instance.prefer-ip-address=true 28 | eureka.client.fetch-registry=false 29 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 30 | eureka.instance.metadata-map.management.port=${management.port:-${server.port}} -------------------------------------------------------------------------------- /microapp-1/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=demo 2 | spring.cloud.config.uri=http://localhost:8888 3 | spring.cloud.config.fail-fast=true -------------------------------------------------------------------------------- /microapp-1/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /microapp-1/src/test/java/org/mvnsearch/app1/Microapp1ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app1; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Microapp1ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /microapp-1/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /microapp-2/README.md: -------------------------------------------------------------------------------- 1 | MicroApp-2 2 | ========== 3 | MicroApp-2 to test Consul integration with config and registry. 4 | -------------------------------------------------------------------------------- /microapp-2/import_data.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | consul kv put config/testApp/developer Lucas -------------------------------------------------------------------------------- /microapp-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | microapp-2 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | microapp-2 17 | Demo project for Spring Boot 18 | 19 | 20 | UTF-8 21 | UTF-8 22 | 1.8 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-devtools 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | com.ecwid.consul 40 | consul-api 41 | 1.4.2 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-starter-consul-discovery 46 | 47 | 48 | org.springframework.cloud 49 | spring-cloud-starter-consul-config 50 | 51 | 52 | io.dropwizard.metrics 53 | metrics-core 54 | 55 | 56 | org.jolokia 57 | jolokia-core 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-starter-test 62 | test 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /microapp-2/src/main/java/org/mvnsearch/app2/Microapp2Application.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app2; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class Microapp2Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Microapp2Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /microapp-2/src/main/java/org/mvnsearch/app2/PortalController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app2; 2 | 3 | import com.codahale.metrics.Counter; 4 | import com.codahale.metrics.MetricRegistry; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.boot.actuate.metrics.CounterService; 8 | import org.springframework.cloud.context.config.annotation.RefreshScope; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | * portal controller 14 | * 15 | * @author linux_china 16 | */ 17 | @RestController 18 | @RefreshScope 19 | public class PortalController { 20 | @Value("${developer}") 21 | private String nick; 22 | @Autowired 23 | private CounterService counterService; 24 | @Autowired 25 | private MetricRegistry registry; 26 | 27 | @GetMapping("/") 28 | public String welcome() { 29 | counterService.increment("counter.page.welcome"); 30 | Counter evictions = registry.counter("dropwizard.page.welcome"); 31 | evictions.inc(2); 32 | return "hello" + nick; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /microapp-2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=9080 2 | management.port=9081 3 | spring.application.name=demo2 4 | 5 | ## application variables 6 | developer=xxx 7 | 8 | #INFO 9 | info.service.version=1.0.0-SNAPSHOT 10 | info.service.description=demo product 11 | info.service.tags=demo 12 | info.team.developer=linux_china 13 | info.team.tester=linux_china 14 | info.team.pe=linux_china 15 | info.scm.url=https://github.com/linux-china/spring-boot-microservices 16 | info.docs.url=https://github.com/linux-china/spring-boot-microservices 17 | 18 | #logging 19 | logging.level.root=error 20 | logging.level.org.springframework.boot=info 21 | logging.level.org.mvnsearch=info 22 | logging.path=${user.home}/logs 23 | logging.file=${spring.application.name}.log -------------------------------------------------------------------------------- /microapp-2/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=demo2 2 | spring.cloud.consul.host=localhost -------------------------------------------------------------------------------- /microapp-2/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /microapp-2/src/test/java/org/mvnsearch/app2/Microapp2ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app2; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Microapp2ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /microapp-2/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d %-5level %logger{36} - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /microapp-3/README.md: -------------------------------------------------------------------------------- 1 | MicroApp-3 2 | ========== 3 | MicroApp-3 to test zipkin integration 4 | 5 | 6 | ### Usage 7 | 8 | Please visit http://localhost:9411/zipkin/ for detail -------------------------------------------------------------------------------- /microapp-3/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | microapp-3 13 | 0.0.1-SNAPSHOT 14 | jar 15 | 16 | microapp-1 17 | Demo project for Spring Boot 18 | 19 | 20 | UTF-8 21 | UTF-8 22 | 1.8 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-devtools 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-starter-config 41 | 42 | 43 | org.jolokia 44 | jolokia-core 45 | 46 | 47 | org.springframework.cloud 48 | spring-cloud-starter-eureka 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-aop 53 | 54 | 55 | org.springframework.cloud 56 | spring-cloud-sleuth-core 57 | 58 | 59 | org.springframework.cloud 60 | spring-cloud-sleuth-zipkin 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | test 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /microapp-3/src/main/java/org/mvnsearch/app1/Microapp3Application.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app1; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 8 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 9 | import org.springframework.cloud.sleuth.SpanReporter; 10 | import org.springframework.context.annotation.Bean; 11 | 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | public class Microapp3Application { 15 | private static final Logger log = LoggerFactory.getLogger(Microapp3Application.class); 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(Microapp3Application.class, args); 19 | } 20 | 21 | /** 22 | * Use this for debugging (or if there is no Zipkin server running on port 9411) 23 | * 24 | * @return zipkin span reporter 25 | */ 26 | @Bean 27 | @ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false") 28 | public SpanReporter spanCollector() { 29 | return span -> log.info(String.format("Reporting span [%s]", span)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /microapp-3/src/main/java/org/mvnsearch/app1/PortalController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app1; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * portal controller 8 | * 9 | * @author linux_china 10 | */ 11 | @RestController 12 | public class PortalController { 13 | 14 | @GetMapping("/welcome") 15 | public String welcome() { 16 | return "welcome"; 17 | } 18 | 19 | @GetMapping("/demo/welcome") 20 | public String welcome2() { 21 | return "welcome2"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /microapp-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=microapp-3 2 | server.port=8080 3 | management.port=8081 4 | management.info.git.mode=full 5 | 6 | ### application variables 7 | developer=xxx 8 | 9 | #INFO 10 | info.service.version=1.0.0-SNAPSHOT 11 | info.service.description=microapp-3 product 12 | info.service.tags=microapp-3 13 | info.team.developer=linux_china 14 | info.team.tester=linux_china 15 | info.team.pe=linux_china 16 | info.scm.url=https://github.com/linux-china/spring-boot-microservices 17 | info.docs.url=https://github.com/linux-china/spring-boot-microservices 18 | 19 | #logging 20 | logging.level.root=error 21 | logging.level.org.springframework.boot=info 22 | logging.level.org.mvnsearch=info 23 | logging.path=${user.home}/logs 24 | logging.file=${spring.application.name}.log 25 | 26 | #tracing 27 | spring.zipkin.baseUrl=http://localhost:9411 28 | spring.zipkin.enabled=true 29 | spring.zipkin.service.name=microapp-3 30 | spring.sleuth.sampler.percentage=0.1 31 | sample.zipkin.enabled=true 32 | 33 | ### ops 34 | eureka.instance.prefer-ip-address=true 35 | eureka.client.fetch-registry=false 36 | eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 37 | eureka.instance.metadata-map.management.port=${management.port:-${server.port}} -------------------------------------------------------------------------------- /microapp-3/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=demo 2 | spring.cloud.config.uri=http://localhost:8888 3 | spring.cloud.config.fail-fast=true -------------------------------------------------------------------------------- /microapp-3/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /microapp-hystrix/README.md: -------------------------------------------------------------------------------- 1 | MicroApp-hystrix 2 | ========== 3 | MicroApp to test Hiystrix integration 4 | 5 | 6 | ### Retrofit 7 | 8 | * Change destination dynamically(load balance): https://gist.github.com/swankjesse/8571a8207a5815cca1fb 9 | 10 | ### UI 11 | please visit http://localhost:8080/hystrix 12 | 13 | http://localhost:8080/hystrix/monitor?stream=http%3A%2F%2Flocalhost%3A8081%2Fhystrix.stream 14 | 15 | ### References 16 | 17 | * http://cloud.spring.io/spring-cloud-netflix/ 18 | * https://github.com/Netflix/Hystrix -------------------------------------------------------------------------------- /microapp-hystrix/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | microapp-hystrix 13 | 0.0.1-SNAPSHOT 14 | jar 15 | 16 | microapp hystrix 17 | Demo hystrix project for Spring Boot 18 | 19 | 20 | UTF-8 21 | UTF-8 22 | 1.8 23 | 2.5.0 24 | 3.14.0 25 | 10.2.3 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-devtools 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-actuator 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | org.jolokia 43 | jolokia-core 44 | 45 | 46 | org.springframework.cloud 47 | spring-cloud-starter-netflix-hystrix 48 | 49 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-starter-netflix-hystrix-dashboard 56 | 57 | 58 | com.squareup.okhttp3 59 | okhttp 60 | ${okhttp3.version} 61 | 62 | 63 | com.squareup.okhttp3 64 | logging-interceptor 65 | ${okhttp3.version} 66 | 67 | 68 | com.squareup.retrofit2 69 | retrofit 70 | ${retrofit.version} 71 | 72 | 73 | com.squareup.retrofit2 74 | converter-jackson 75 | ${retrofit.version} 76 | 77 | 78 | 79 | io.github.openfeign 80 | feign-core 81 | 82 | 83 | io.github.openfeign 84 | feign-hystrix 85 | 86 | 87 | io.github.openfeign 88 | feign-okhttp 89 | 90 | 91 | io.github.openfeign 92 | feign-slf4j 93 | 94 | 95 | io.github.openfeign 96 | feign-java8 97 | 98 | 99 | io.github.openfeign 100 | feign-jackson 101 | 102 | 103 | 104 | com.fasterxml.jackson.core 105 | jackson-databind 106 | 107 | 108 | org.springframework.boot 109 | spring-boot-starter-test 110 | test 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | io.github.openfeign 119 | feign-bom 120 | ${openfeign.version} 121 | pom 122 | import 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/MicroappHystrixApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 6 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 7 | 8 | @SpringBootApplication 9 | @EnableCircuitBreaker() 10 | @EnableHystrixDashboard 11 | public class MicroappHystrixApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(MicroappHystrixApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/UserController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4; 2 | 3 | import org.mvnsearch.app4.domain.infra.HttpBinResponse; 4 | import org.mvnsearch.app4.domain.infra.HttpBinServiceFeignAPI; 5 | import org.mvnsearch.app4.domain.infra.HttpBinServiceRetrofitAPI; 6 | import org.mvnsearch.app4.domain.service.UserService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RestController; 11 | import retrofit2.Response; 12 | 13 | import java.io.IOException; 14 | 15 | /** 16 | * user controller 17 | * 18 | * @author linux_china 19 | */ 20 | @RestController 21 | public class UserController { 22 | @Autowired 23 | private UserService userService; 24 | @Autowired 25 | private HttpBinServiceRetrofitAPI retrofitAPI; 26 | @Autowired 27 | private HttpBinServiceFeignAPI feignAPI; 28 | 29 | @GetMapping("/user/{id}") 30 | public String welcome(@PathVariable Integer id) { 31 | return userService.findNick(id); 32 | } 33 | 34 | @GetMapping("/ip") 35 | public String ip() throws IOException { 36 | Response response = retrofitAPI.ip().execute(); 37 | return response.body().getIp(); 38 | } 39 | 40 | @GetMapping("/ip2") 41 | public String ip2() throws IOException { 42 | return feignAPI.ip().getIp(); 43 | } 44 | 45 | @GetMapping("/uuid") 46 | public String uuid() throws IOException { 47 | Response response = retrofitAPI.uuid(); 48 | return response.body().getUuid(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/infra/HttpBinAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.netflix.hystrix.HystrixCommand; 5 | import feign.RequestInterceptor; 6 | import feign.RequestTemplate; 7 | import feign.Target; 8 | import feign.hystrix.HystrixFeign; 9 | import feign.hystrix.SetterFactory; 10 | import feign.jackson.JacksonDecoder; 11 | import feign.jackson.JacksonEncoder; 12 | import feign.slf4j.Slf4jLogger; 13 | import okhttp3.OkHttpClient; 14 | import org.mvnsearch.app4.domain.infra.retrofit.HystrixCallAdapterFactory; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | import retrofit2.Retrofit; 18 | import retrofit2.converter.jackson.JacksonConverterFactory; 19 | 20 | import java.lang.reflect.Method; 21 | 22 | /** 23 | * httpbin auto configuration 24 | * 25 | * @author linux_china 26 | */ 27 | @Configuration 28 | public class HttpBinAutoConfiguration { 29 | 30 | @Bean 31 | public HttpBinServiceRetrofitAPI httpBinServiceRetrofitAPI(ObjectMapper objectMapper) { 32 | return new Retrofit.Builder(). 33 | baseUrl("http://httpbin.org"). 34 | client(okHttpClient()). 35 | addCallAdapterFactory(new HystrixCallAdapterFactory("HttpBinServiceRetrofitAPI")). 36 | addConverterFactory(JacksonConverterFactory.create(objectMapper)). 37 | build(). 38 | create(HttpBinServiceRetrofitAPI.class); 39 | } 40 | 41 | @Bean 42 | public HttpBinServiceFeignAPI httpBinServiceFeignAPI(ObjectMapper objectMapper, OkHttpClient okHttpClient) { 43 | return HystrixFeign.builder() 44 | .client(new feign.okhttp.OkHttpClient(okHttpClient)) 45 | .logger(new Slf4jLogger()) 46 | .encoder(new JacksonEncoder(objectMapper)) 47 | .decoder(new JacksonDecoder(objectMapper)) 48 | .target(HttpBinServiceFeignAPI.class, "http://httpbin.org"); 49 | } 50 | 51 | @Bean 52 | public OkHttpClient okHttpClient() { 53 | return new OkHttpClient.Builder().build(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/infra/HttpBinResponse.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import lombok.Data; 5 | 6 | import java.util.Map; 7 | 8 | /** 9 | * http bin response 10 | * 11 | * @author linux_china 12 | */ 13 | @Data 14 | public class HttpBinResponse { 15 | private String uuid; 16 | @JsonProperty(value = "origin") 17 | private String ip; 18 | private Map headers; 19 | } 20 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/infra/HttpBinServiceFeignAPI.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra; 2 | 3 | import feign.RequestLine; 4 | 5 | /** 6 | * httpbin feign api 7 | * 8 | * @author linux_china 9 | */ 10 | public interface HttpBinServiceFeignAPI { 11 | 12 | @RequestLine("GET /ip") 13 | HttpBinResponse ip(); 14 | 15 | @RequestLine("GET /uuid") 16 | HttpBinResponse uuid(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/infra/HttpBinServiceRetrofitAPI.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra; 2 | 3 | import com.netflix.hystrix.HystrixCommand; 4 | import retrofit2.Call; 5 | import retrofit2.Response; 6 | import retrofit2.http.GET; 7 | 8 | /** 9 | * httpbin service retrofit API 10 | * 11 | * @author linux_china 12 | */ 13 | public interface HttpBinServiceRetrofitAPI { 14 | 15 | @GET("/ip") 16 | HystrixCommand> ip(); 17 | 18 | @GET("/uuid") 19 | Response uuid(); 20 | 21 | @GET("/headers") 22 | Call headers(); 23 | } 24 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/infra/retrofit/HystrixCallAdapter.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra.retrofit; 2 | 3 | 4 | import com.netflix.hystrix.HystrixCommand; 5 | import com.netflix.hystrix.HystrixCommandGroupKey; 6 | import com.netflix.hystrix.HystrixCommandKey; 7 | import com.netflix.hystrix.exception.HystrixBadRequestException; 8 | import org.jetbrains.annotations.NotNull; 9 | import org.springframework.util.ReflectionUtils; 10 | import retrofit2.Call; 11 | import retrofit2.CallAdapter; 12 | import retrofit2.Response; 13 | 14 | import java.lang.reflect.Field; 15 | import java.lang.reflect.Type; 16 | 17 | /** 18 | * hystrix call adapter 19 | * 20 | * @author baitouweng 21 | */ 22 | public class HystrixCallAdapter implements CallAdapter { 23 | 24 | private final boolean isHystrixCommand; 25 | 26 | private final boolean isObservable; 27 | 28 | private final boolean isSingle; 29 | 30 | private final boolean isCompletable; 31 | 32 | private final boolean isBody; 33 | 34 | private final boolean isResponse; 35 | 36 | private final Type responseType; 37 | 38 | private final String commandGroup; 39 | 40 | private static Field serviceMethodField; 41 | private static Field relativeUrlField; 42 | 43 | public HystrixCallAdapter(Type responseType, boolean isResponse, boolean isBody, 44 | boolean isHystrixCommand, boolean isObservable, boolean isSingle, 45 | boolean isCompletable, String commandGroup) { 46 | this.responseType = responseType; 47 | this.isResponse = isResponse; 48 | this.isBody = isBody; 49 | this.isHystrixCommand = isHystrixCommand; 50 | this.isObservable = isObservable; 51 | this.isSingle = isSingle; 52 | this.isCompletable = isCompletable; 53 | this.commandGroup = commandGroup; 54 | } 55 | 56 | static { 57 | try { 58 | Class okHttpCallClass = Class.forName("retrofit2.OkHttpCall"); 59 | Class serviceMethodClass = Class.forName("retrofit2.ServiceMethod"); 60 | serviceMethodField = ReflectionUtils.findField(okHttpCallClass, "serviceMethod"); 61 | serviceMethodField.setAccessible(true); 62 | relativeUrlField = ReflectionUtils.findField(serviceMethodClass, "relativeUrl"); 63 | relativeUrlField.setAccessible(true); 64 | } catch (Exception e) { 65 | throw new RuntimeException(e); 66 | } 67 | } 68 | 69 | @Override 70 | public Type responseType() { 71 | return responseType; 72 | } 73 | 74 | @Override 75 | public Object adapt(@NotNull Call call) { 76 | HystrixCommand.Setter setter = obtainHystrixCommandSetter(call); 77 | 78 | HystrixCommand hystrixCommand = new HystrixCommand(setter) { 79 | @Override 80 | protected Object run() throws Exception { 81 | try { 82 | Response response = call.execute(); 83 | if (HystrixCallAdapter.this.isBody) { 84 | return response.body(); 85 | } 86 | return response; 87 | } catch (Exception e) { 88 | //Do not trigger circuitBreaker 89 | throw new HystrixBadRequestException(e.getMessage(), e.getCause()); 90 | } 91 | } 92 | }; 93 | 94 | if (isHystrixCommand) { 95 | return hystrixCommand; 96 | } 97 | 98 | if (isObservable) { 99 | // cold Observable 100 | return hystrixCommand.toObservable(); 101 | } 102 | 103 | if (isSingle) { 104 | return hystrixCommand.toObservable().toSingle(); 105 | } 106 | 107 | if (isCompletable) { 108 | return hystrixCommand.toObservable().toCompletable(); 109 | } 110 | 111 | return hystrixCommand.execute(); 112 | } 113 | 114 | private HystrixCommand.Setter obtainHystrixCommandSetter(Call call) { 115 | String method = call.request().method(); 116 | Object serviceMethod = ReflectionUtils.getField(serviceMethodField, call); 117 | String relativeUrl = (String) ReflectionUtils.getField(relativeUrlField, serviceMethod); 118 | String commandKey = method + "#" + relativeUrl; 119 | return HystrixCommand.Setter 120 | .withGroupKey(HystrixCommandGroupKey.Factory.asKey(this.commandGroup)) 121 | .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); 122 | } 123 | } -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/infra/retrofit/HystrixCallAdapterFactory.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra.retrofit; 2 | 3 | 4 | import com.netflix.hystrix.HystrixCommand; 5 | import org.jetbrains.annotations.NotNull; 6 | import retrofit2.Call; 7 | import retrofit2.CallAdapter; 8 | import retrofit2.Response; 9 | import retrofit2.Retrofit; 10 | import rx.Completable; 11 | import rx.Observable; 12 | import rx.Single; 13 | 14 | import java.lang.annotation.Annotation; 15 | import java.lang.reflect.ParameterizedType; 16 | import java.lang.reflect.Type; 17 | 18 | /** 19 | * hystrix adapter factory 20 | * 21 | * @author baitouweng 22 | * @author linux_china 23 | */ 24 | 25 | public class HystrixCallAdapterFactory extends CallAdapter.Factory { 26 | /** 27 | * command group 28 | */ 29 | private String commandGroup; 30 | 31 | public HystrixCallAdapterFactory(String commandGroup) { 32 | this.commandGroup = commandGroup; 33 | } 34 | 35 | @Override 36 | public CallAdapter get(@NotNull Type returnType, @NotNull Annotation[] annotations, @NotNull Retrofit retrofit) { 37 | Class rawType = getRawType(returnType); 38 | if (rawType == Call.class) { 39 | return null; 40 | } 41 | boolean isHystrixCommand = rawType == HystrixCommand.class; 42 | boolean isObservable = rawType == Observable.class; 43 | boolean isSingle = rawType == Single.class; 44 | boolean isCompletable = rawType == Completable.class; 45 | boolean isResponse = rawType == Response.class; 46 | 47 | Type responseType; 48 | boolean isBody = false; 49 | 50 | if (!isHystrixCommand && !isObservable && !isSingle && !isCompletable && !isResponse) { 51 | return new HystrixCallAdapter<>(returnType, false, true, false, 52 | false, false, false, commandGroup); 53 | } 54 | 55 | if (isCompletable) { 56 | return new HystrixCallAdapter<>(Void.class, false, false, false, 57 | false, false, true, commandGroup); 58 | } 59 | 60 | if (!(returnType instanceof ParameterizedType)) { 61 | String name = isHystrixCommand ? "HystrixCommand" : isObservable ? "Observable" 62 | : isSingle ? "Single" : "Response"; 63 | throw new IllegalStateException(name + " return type must be parameterized" 64 | + " as " + name + " or " + name + ""); 65 | } 66 | 67 | if (isResponse) { 68 | responseType = getParameterUpperBound(0, (ParameterizedType) returnType); 69 | return new HystrixCallAdapter<>(responseType, true, false, isHystrixCommand, 70 | isObservable, isSingle, false, commandGroup); 71 | } 72 | 73 | Type commandType = getParameterUpperBound(0, (ParameterizedType) returnType); 74 | Class rawCommandType = getRawType(commandType); 75 | if (rawCommandType == Response.class) { 76 | if (!(commandType instanceof ParameterizedType)) { 77 | throw new IllegalStateException("Response must be parameterized" 78 | + " as Response or Response"); 79 | } 80 | responseType = getParameterUpperBound(0, (ParameterizedType) commandType); 81 | } else { 82 | responseType = commandType; 83 | isBody = true; 84 | } 85 | return new HystrixCallAdapter(responseType, isResponse, isBody, isHystrixCommand, isObservable, isSingle, isCompletable, commandGroup); 86 | } 87 | } -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/service/UserService.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.service; 2 | 3 | /** 4 | * user service 5 | * 6 | * @author linux_china 7 | */ 8 | public interface UserService { 9 | 10 | String findNick(Integer id); 11 | } 12 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/java/org/mvnsearch/app4/domain/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.service; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.actuate.metrics.CounterService; 7 | import org.springframework.stereotype.Service; 8 | 9 | /** 10 | * user service implementation 11 | * 12 | * @author linux_china 13 | */ 14 | @Service 15 | public class UserServiceImpl implements UserService { 16 | @Autowired 17 | private CounterService counterService; 18 | 19 | @Override 20 | @HystrixCommand(fallbackMethod = "nullNick", commandProperties = {@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "20")}) 21 | public String findNick(Integer id) { 22 | counterService.increment("invoke"); 23 | if (id % 3 == 1) { 24 | counterService.increment("invoke.error"); 25 | throw new RuntimeException(); 26 | } 27 | return "nick:" + id; 28 | } 29 | 30 | public String nullNick(Integer id) { 31 | counterService.increment("fallback"); 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=microapp-hystrix 2 | server.port=8080 3 | management.port=8081 4 | management.security.enabled=false 5 | 6 | management.info.git.mode=full 7 | 8 | hystrix.metrics.enabled=false 9 | 10 | #logging 11 | logging.level.root=error 12 | logging.level.org.springframework.boot=info 13 | logging.level.org.mvnsearch=info 14 | logging.path=${user.home}/logs 15 | logging.file=${spring.application.name}.log 16 | 17 | 18 | ### jackson 19 | spring.jackson.deserialization.fail-on-unknown-properties = false 20 | 21 | -------------------------------------------------------------------------------- /microapp-hystrix/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=microapp-hystrix 2 | spring.cloud.config.uri=http://localhost:8888 3 | spring.cloud.config.fail-fast=true -------------------------------------------------------------------------------- /microapp-hystrix/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /microapp-hystrix/src/test/java/org/mvnsearch/app4/HttpRequestTest.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4; 2 | 3 | import okhttp3.OkHttpClient; 4 | import okhttp3.Request; 5 | import okhttp3.Response; 6 | import org.junit.Test; 7 | 8 | /** 9 | * http request test 10 | * 11 | * @author linux_china 12 | */ 13 | public class HttpRequestTest { 14 | 15 | @Test 16 | public void testGet() throws Exception { 17 | OkHttpClient okHttpClient = new OkHttpClient(); 18 | for (int i = 0; i < 1000; i++) { 19 | Request request = new Request.Builder() 20 | .url("http://localhost:8080/user/" + i % 2) 21 | .build(); 22 | Response response = okHttpClient.newCall(request).execute(); 23 | Thread.sleep(50); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /microapp-hystrix/src/test/java/org/mvnsearch/app4/HystrixApplicationBaseTest.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4; 2 | 3 | import org.junit.runner.RunWith; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | import org.springframework.test.context.junit4.SpringRunner; 6 | 7 | /** 8 | * hystrix application base test 9 | * 10 | * @author linux_china 11 | */ 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class HystrixApplicationBaseTest { 15 | } 16 | -------------------------------------------------------------------------------- /microapp-hystrix/src/test/java/org/mvnsearch/app4/domain/infra/HttpBinServiceFeignAPITest.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra; 2 | 3 | import org.junit.Test; 4 | import org.mvnsearch.app4.HystrixApplicationBaseTest; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | 7 | /** 8 | * httpbin service feign api test 9 | * 10 | * @author linux_china 11 | */ 12 | public class HttpBinServiceFeignAPITest extends HystrixApplicationBaseTest { 13 | @Autowired 14 | private HttpBinServiceFeignAPI feignAPI; 15 | 16 | @Test 17 | public void testIp() { 18 | System.out.println(feignAPI.ip().getIp()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /microapp-hystrix/src/test/java/org/mvnsearch/app4/domain/infra/HttpBinServiceRetrofitAPITest.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.app4.domain.infra; 2 | 3 | import org.junit.Test; 4 | import org.mvnsearch.app4.HystrixApplicationBaseTest; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import retrofit2.Response; 7 | 8 | /** 9 | * http bin retrofit api test 10 | * 11 | * @author linux_china 12 | */ 13 | public class HttpBinServiceRetrofitAPITest extends HystrixApplicationBaseTest { 14 | 15 | @Autowired 16 | private HttpBinServiceRetrofitAPI httpBinServiceRetrofitAPI; 17 | 18 | @Test 19 | public void testIp() throws Exception { 20 | Response response = httpBinServiceRetrofitAPI.ip().execute(); 21 | if (response.isSuccessful()) { 22 | System.out.println(response.body().getIp()); 23 | } 24 | } 25 | 26 | @Test 27 | public void testHeaders() throws Exception { 28 | Response response = httpBinServiceRetrofitAPI.headers().execute(); 29 | if (response.isSuccessful()) { 30 | System.out.println(response.body().getHeaders()); 31 | } 32 | } 33 | 34 | @Test 35 | public void testUuid() throws Exception { 36 | Response response = httpBinServiceRetrofitAPI.uuid(); 37 | if (response.isSuccessful()) { 38 | System.out.println(response.body().getUuid()); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /microapp-proxy/README.md: -------------------------------------------------------------------------------- 1 | MicroApp-Proxy 2 | ============== 3 | Demo for reverse proxy 4 | -------------------------------------------------------------------------------- /microapp-proxy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | microapp-proxy 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | microapp-proxy 17 | Proxy project for Spring Boot 18 | 19 | 20 | UTF-8 21 | UTF-8 22 | 1.8 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-devtools 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-actuator 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-web 37 | 38 | 39 | com.github.mkopylec 40 | charon-spring-boot-starter 41 | 2.6.1 42 | 43 | 44 | org.jolokia 45 | jolokia-core 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /microapp-proxy/proxy.http: -------------------------------------------------------------------------------- 1 | GET http://localhost:3126/httpbin/ip 2 | Host: httpbin.org 3 | 4 | ### 5 | GET http://httpbin.org/ip 6 | Host: httpbin.org 7 | 8 | ### 9 | GET http://localhost:3126/welcome 10 | 11 | ### 12 | -------------------------------------------------------------------------------- /microapp-proxy/src/main/java/org/mvnsearch/proxy/MicroProxyApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.proxy; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.core.task.TaskExecutor; 7 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 8 | 9 | @SpringBootApplication 10 | public class MicroProxyApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(MicroProxyApplication.class, args); 14 | } 15 | 16 | @Bean("charonTaskExecutor") 17 | public TaskExecutor taskExecutor() { 18 | ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 19 | executor.setCorePoolSize(5); 20 | executor.setMaxPoolSize(10); 21 | executor.setQueueCapacity(25); 22 | return executor; 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /microapp-proxy/src/main/java/org/mvnsearch/proxy/PortalController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.proxy; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * portal controller 8 | * 9 | * @author linux_china 10 | */ 11 | @RestController 12 | public class PortalController { 13 | 14 | 15 | @GetMapping("/") 16 | public String index() { 17 | return "hello jackie"; 18 | } 19 | 20 | @GetMapping("/welcome") 21 | public String welcome() { 22 | return "welcome"; 23 | } 24 | 25 | @GetMapping("/demo/welcome") 26 | public String welcome2() { 27 | return "welcome2"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /microapp-proxy/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=micro-proxy 2 | server.port=3126 3 | management.port=3127 4 | management.info.git.mode=full 5 | 6 | #INFO 7 | info.service.version=1.0.0-SNAPSHOT 8 | info.service.description=demo1 proxy 9 | info.service.tags=demo1,proxy 10 | info.team.developer=linux_china 11 | info.team.tester=linux_china 12 | info.team.pe=linux_china 13 | info.scm.url=https://github.com/linux-china/spring-boot-microservices 14 | info.docs.url=https://github.com/linux-china/spring-boot-microservices 15 | 16 | #logging 17 | logging.level.root=error 18 | logging.level.org.springframework.boot=info 19 | logging.level.org.mvnsearch=info 20 | logging.path=${user.home}/logs 21 | logging.file=${spring.application.name}.log 22 | 23 | ##proxy 24 | charon.mappings[0].name=httpbin 25 | charon.mappings[0].path=/httpbin 26 | charon.mappings[0].stripPath=true 27 | charon.mappings[0].destinations=http://54.225.152.117 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | org.mvnsearch 6 | spring-boot-microservices 7 | pom 8 | 1.0.0-SNAPSHOT 9 | 10 | boot-admin-server 11 | cloud-config-server 12 | cloud-registry-server 13 | cloud-data-flow-server 14 | cloud-skipper-server 15 | microapp-1 16 | microapp-2 17 | microapp-3 18 | microapp-proxy 19 | microapp-hystrix 20 | zuul-proxy 21 | cloud-gateway 22 | sidecar-1 23 | task-demo 24 | 25 | 26 | 27 | UTF-8 28 | 1.8 29 | 5.0.14.RELEASE 30 | 1.5.21.RELEASE 31 | Edgware.SR6 32 | Brussels-SR17 33 | 2.9.9 34 | 35 | 36 | 37 | 38 | 39 | org.jetbrains 40 | annotations 41 | 17.0.0 42 | 43 | 44 | com.fasterxml.jackson.core 45 | jackson-annotations 46 | 47 | 48 | io.swagger 49 | swagger-annotations 50 | 1.5.22 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 56 | 57 | 58 | 59 | 60 | 61 | maven-compiler-plugin 62 | 3.8.1 63 | 64 | 1.8 65 | 1.8 66 | true 67 | 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-maven-plugin 72 | ${spring-boot.version} 73 | 74 | false 75 | 76 | 77 | 78 | 79 | repackage 80 | build-info 81 | 82 | 83 | 84 | 85 | 86 | pl.project13.maven 87 | git-commit-id-plugin 88 | 2.2.3 89 | 90 | 91 | 92 | revision 93 | 94 | 95 | 96 | 97 | true 98 | yyyy-MM-dd'T'HH:mm:ssZ 99 | true 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | com.fasterxml.jackson 108 | jackson-bom 109 | ${jackson.version} 110 | import 111 | pom 112 | 113 | 114 | org.springframework 115 | spring-framework-bom 116 | ${spring.version} 117 | pom 118 | import 119 | 120 | 121 | org.springframework.boot 122 | spring-boot-dependencies 123 | ${spring-boot.version} 124 | pom 125 | import 126 | 127 | 128 | org.springframework.cloud 129 | spring-cloud-dependencies 130 | ${spring-cloud.version} 131 | pom 132 | import 133 | 134 | 135 | io.spring.platform 136 | platform-bom 137 | ${platform-bom.version} 138 | pom 139 | import 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /sidecar-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | sidecar-1 13 | 1.0.0-SNAPSHOT 14 | jar 15 | 16 | sidecar-1 17 | Spring Boot SideCar Demo 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-actuator 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-netflix-sidecar 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /sidecar-1/src/main/java/org/mvnsearch/Sidecar1Application.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.sidecar.EnableSidecar; 6 | 7 | @SpringBootApplication 8 | @EnableSidecar 9 | public class Sidecar1Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Sidecar1Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /sidecar-1/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 5678 3 | management: 4 | port: 5679 5 | spring: 6 | application: 7 | name: mvnsearch 8 | 9 | sidecar: 10 | port: 80 11 | health-uri: http://www.mvnsearch.org/health.json 12 | 13 | eureka: 14 | client: 15 | service-url: 16 | defaultZone: http://localhost:8761/eureka/ 17 | 18 | zuul: 19 | routes: 20 | mvnsearch: 21 | path: /mvnsearch/** 22 | url: http://www.mvnsearch.org 23 | stripPrefix: true 24 | 25 | -------------------------------------------------------------------------------- /sidecar-1/src/test/java/org/mvnsearch/Sidecar1ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) 10 | public class Sidecar1ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /task-demo/README.md: -------------------------------------------------------------------------------- 1 | Spring Cloud Task 2 | ================= 3 | Spring Cloud Task就是执行一个短的任务,也就是启动JVM进程后执行逻辑,最后JVM退出,将执行任务的结果记录下来。 4 | 5 | ### 为何要Cloud Task 6 | 7 | * 和Schedule和Cron不一样: 短时间的定是运行可以理解为stream,poll模型 8 | * 节约成本: 每半小时运行一次,直接开vm然后销毁 9 | 10 | 11 | ### 使用 12 | 13 | * 首先你要创建一个数据库来记录task执行情况 14 | * 创建一个Spring Boot应用,然后创建一个CommandLineRunner的Bean 15 | * 执行 mvn -DskipTests clean package会调用Spring Boot Maven plugin,生成一个fat jar 16 | * 提交fat jar到执行平台执行即可 17 | 18 | ### 注意 19 | 20 | * 和Cron不一样,那个驻留定时执行 21 | 22 | ### References 23 | 24 | * 常见Task: https://github.com/spring-cloud-task-app-starters 25 | -------------------------------------------------------------------------------- /task-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | org.mvnsearch 8 | spring-boot-microservices 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | org.mvnsearch.cloud 13 | task-demo 14 | 1.0.0-SNAPSHOT 15 | jar 16 | 17 | Spring Cloud Task Demo 18 | 19 | 20 | 21 | UTF-8 22 | 1.8 23 | 1.2.4.RELEASE 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter 30 | 31 | 32 | org.springframework.cloud 33 | spring-cloud-task-core 34 | ${spring-cloud-task.version} 35 | 36 | 37 | org.springframework.cloud 38 | spring-cloud-deployer-resource-support 39 | 1.3.4.RELEASE 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-jdbc 44 | 45 | 46 | commons-dbcp 47 | commons-dbcp 48 | 49 | 50 | mysql 51 | mysql-connector-java 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-starter-test 56 | test 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /task-demo/src/main/java/org/mvnsearch/DemoTaskApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.springframework.boot.CommandLineRunner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.task.configuration.EnableTask; 7 | import org.springframework.context.annotation.Bean; 8 | 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | 12 | @SpringBootApplication 13 | @EnableTask 14 | public class DemoTaskApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(DemoTaskApplication.class, args); 18 | } 19 | 20 | @Bean 21 | public CommandLineRunner commandLineRunner() { 22 | return args -> 23 | System.out.println("Executed at :" + new SimpleDateFormat().format(new Date())); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /task-demo/src/main/java/org/mvnsearch/TaskListener.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.springframework.cloud.task.listener.annotation.AfterTask; 4 | import org.springframework.cloud.task.listener.annotation.BeforeTask; 5 | import org.springframework.cloud.task.listener.annotation.FailedTask; 6 | import org.springframework.cloud.task.repository.TaskExecution; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | * task listener 11 | * 12 | * @author linux_china 13 | */ 14 | @Component 15 | public class TaskListener { 16 | @BeforeTask 17 | public void methodA(TaskExecution taskExecution) { 18 | 19 | } 20 | 21 | @AfterTask 22 | public void methodB(TaskExecution taskExecution) { 23 | } 24 | 25 | @FailedTask 26 | public void methodC(TaskExecution taskExecution, Throwable throwable) { 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /task-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=HelloWorldTask 2 | ### task datasource 3 | spring.datasource.url=jdbc:mysql://localhost:3306/spring_cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false 4 | spring.datasource.username=root 5 | spring.datasource.password=123456 6 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /zuul-proxy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.mvnsearch 7 | spring-boot-microservices 8 | 1.0.0-SNAPSHOT 9 | 10 | zuul-proxy 11 | 1.0.0-SNAPSHOT 12 | jar 13 | 14 | zuul-proxy 15 | Zuul proxy server 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-actuator 21 | 22 | 23 | org.springframework.cloud 24 | spring-cloud-starter-zuul 25 | 26 | 27 | org.springframework.cloud 28 | spring-cloud-starter-ribbon 29 | 30 | 31 | org.springframework.cloud 32 | spring-cloud-starter-feign 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-eureka 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-web 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-security 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /zuul-proxy/src/main/java/org/mvnsearch/ZuulProxyDemoApplication.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 | import org.springframework.cloud.netflix.feign.EnableFeignClients; 8 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | @SpringBootApplication 13 | @EnableZuulProxy 14 | @EnableDiscoveryClient 15 | @EnableFeignClients 16 | public class ZuulProxyDemoApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(ZuulProxyDemoApplication.class, args); 20 | } 21 | 22 | @LoadBalanced 23 | @Bean 24 | RestTemplate restTemplate() { 25 | return new RestTemplate(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /zuul-proxy/src/main/java/org/mvnsearch/controller/PortalController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import javax.annotation.PostConstruct; 11 | import java.util.Arrays; 12 | 13 | /** 14 | * portal controller 15 | * 16 | * @author linux_china 17 | */ 18 | @RestController 19 | public class PortalController { 20 | @Autowired 21 | private RestTemplate restTemplate; 22 | 23 | @RequestMapping("/") 24 | public String index() { 25 | ResponseEntity entity = restTemplate.getForEntity("http://demo/welcome", String.class); 26 | return entity.getBody() + " Jacky"; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /zuul-proxy/src/main/java/org/mvnsearch/controller/SpikeController.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.controller; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * spike controller 8 | * 9 | * @author linux_china 10 | */ 11 | @RestController 12 | @RequestMapping("/spike") 13 | public class SpikeController { 14 | 15 | @RequestMapping("") 16 | public String index() { 17 | return "index"; 18 | } 19 | 20 | @RequestMapping("/hello") 21 | public String hello() { 22 | return "hello"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /zuul-proxy/src/main/java/org/mvnsearch/filter/AuthFilter.java: -------------------------------------------------------------------------------- 1 | package org.mvnsearch.filter; 2 | 3 | import com.netflix.zuul.ZuulFilter; 4 | import com.netflix.zuul.context.RequestContext; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.util.UrlPathHelper; 7 | 8 | /** 9 | * auth zuul filter 10 | * 11 | * @author linux_china 12 | */ 13 | @Component 14 | public class AuthFilter extends ZuulFilter { 15 | private UrlPathHelper urlPathHelper = new UrlPathHelper(); 16 | 17 | public String filterType() { 18 | return "pre"; 19 | } 20 | 21 | public int filterOrder() { 22 | return 1; 23 | } 24 | 25 | public boolean shouldFilter() { 26 | RequestContext ctx = RequestContext.getCurrentContext(); 27 | final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest()); 28 | return true; 29 | } 30 | 31 | public Object run() { 32 | RequestContext ctx = RequestContext.getCurrentContext(); 33 | final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest()); 34 | String account = ctx.getRequest().getHeader("Token"); 35 | ctx.addZuulRequestHeader("AccountId", "xxx"); 36 | return null; 37 | } 38 | 39 | /** 40 | * Reports an error message given a response body and code. 41 | * 42 | * @param body http bodybody 43 | * @param code http status code 44 | */ 45 | private void setFailedRequest(String body, int code) { 46 | RequestContext ctx = RequestContext.getCurrentContext(); 47 | ctx.setResponseStatusCode(code); 48 | if (ctx.getResponseBody() == null) { 49 | ctx.setResponseBody(body); 50 | } 51 | ctx.setSendZuulResponse(false); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /zuul-proxy/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7070 3 | 4 | spring: 5 | application: 6 | name: zuul-proxy 7 | 8 | eureka: 9 | client: 10 | service-url: 11 | defaultZone: http://localhost:8761/eureka/ 12 | 13 | 14 | zuul: 15 | routes: 16 | demo: 17 | path: /demo/** 18 | serviceId: demo 19 | stripPrefix: true 20 | --------------------------------------------------------------------------------