├── .idea ├── .gitignore ├── vcs.xml ├── misc.xml ├── jarRepositories.xml └── compiler.xml ├── study-micro-services-zuul └── src │ └── main │ ├── resources │ ├── static │ │ └── index.js │ └── application.yml │ └── java │ └── com │ └── bage │ └── study │ └── micro │ └── services │ └── zuul │ ├── filters │ ├── AddResponseHeaderFilter.java │ ├── QueryParamServiceIdPreFilter.java │ ├── PreRequestLogFilter.java │ ├── QueryParamPortPreFilter.java │ ├── ModifyResponseDataStreamFilter.java │ ├── ModifyResponseBodyFilter.java │ ├── UppercaseRequestEntityFilter.java │ └── PrefixRequestEntityFilter.java │ ├── ZuulApplication.java │ └── provider │ └── MyFallbackProvider.java ├── study-micro-services-eureka-client5 ├── src │ ├── main │ │ ├── resources │ │ │ ├── static │ │ │ │ └── index.js │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── eureka │ │ │ └── client5 │ │ │ ├── EurekaClientController.java │ │ │ └── Client5Application.java │ └── test │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── client1 │ │ └── AppTest.java └── study-micro-services-eureka-client5.iml ├── study-micro-services-eureka-client6 ├── src │ ├── main │ │ ├── resources │ │ │ ├── static │ │ │ │ └── index.js │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── eureka │ │ │ └── client6 │ │ │ ├── EurekaClientController.java │ │ │ └── Client6Application.java │ └── test │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── client1 │ │ └── AppTest.java └── study-micro-services-eureka-client6.iml ├── study-micro-services-sleuth ├── src │ └── main │ │ ├── resources │ │ ├── bootstrap.properties │ │ ├── application.yml │ │ └── logback-spring.xml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── sleuth │ │ ├── FeignServiceClient.java │ │ ├── FeignClientController.java │ │ ├── LogController.java │ │ └── SleuthApplication.java └── study-micro-services-sleuth.iml ├── study-micro-services-h2 ├── src │ ├── main │ │ ├── resources │ │ │ ├── data.sql │ │ │ └── schema.sql │ │ └── java │ │ │ └── com │ │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── h2 │ │ │ ├── demo │ │ │ ├── mybatis │ │ │ │ ├── CustomerMapper.java │ │ │ │ └── SampleMybatisApplication.java │ │ │ └── official │ │ │ │ └── OfficialApplication.java │ │ │ └── domain │ │ │ └── Customer.java │ └── test │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── h2 │ │ └── AppTest.java └── study-micro-services-h2.iml ├── study-micro-services-eureka-client1 ├── src │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── eureka │ │ │ └── client1 │ │ │ └── AppTest.java │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── client1 │ │ ├── Client1Application.java │ │ └── EurekaClientController.java └── study-micro-services-eureka-client1.iml ├── study-micro-services-hystrix └── src │ └── main │ ├── java │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── hystrix │ │ ├── HystrixClientFallback.java │ │ ├── FeignServiceClient.java │ │ ├── FeignClientController.java │ │ ├── HystrixClientController.java │ │ ├── StoreIntegration.java │ │ └── HystrixApplication.java │ └── resources │ └── application.yml ├── study-micro-services-config-client └── src │ └── main │ ├── resources │ ├── bootstrap.properties │ └── application.yml │ └── java │ └── com │ └── bage │ └── study │ └── micro │ └── services │ └── eureka │ └── register │ ├── ConfigClientApplication.java │ └── MessageRestController.java ├── study-micro-services-spring-boot ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── bage │ │ │ │ └── study │ │ │ │ └── micro │ │ │ │ └── services │ │ │ │ └── spring │ │ │ │ └── boot │ │ │ │ ├── cache │ │ │ │ ├── BookRepository.java │ │ │ │ ├── SimpleBookRepository.java │ │ │ │ ├── Book.java │ │ │ │ └── AppRunner.java │ │ │ │ ├── aop │ │ │ │ └── controller │ │ │ │ │ └── UserController.java │ │ │ │ ├── Application.java │ │ │ │ ├── GreetingController.java │ │ │ │ └── CounterController.java │ │ └── resources │ │ │ ├── static │ │ │ └── index.html │ │ │ └── templates │ │ │ └── index.html │ └── test │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── spring │ │ └── boot │ │ └── AppTest.java └── study-micro-services-spring-boot.iml ├── study-micro-services-feign ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── bage │ │ │ ├── App.java │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── feign │ │ │ ├── FeignServiceClient.java │ │ │ ├── FeignClientController.java │ │ │ └── FeignApplication.java │ │ └── resources │ │ └── application.yml ├── README.md └── study-micro-services-feign.iml ├── study-micro-services-ribbon └── src │ ├── main │ ├── resources │ │ └── application.yml │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── ribbon │ │ ├── RibbonApplication.java │ │ └── EurekaClientController.java │ └── test │ └── java │ └── com │ └── bage │ └── study │ └── micro │ └── services │ └── ribbon │ └── AppTest.java ├── study-micro-services-organization └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── organization │ │ │ ├── user │ │ │ ├── dao │ │ │ │ ├── UserDao.java │ │ │ │ ├── UserMapper.java │ │ │ │ └── UserDaoImp.java │ │ │ ├── service │ │ │ │ ├── UserService.java │ │ │ │ └── UserServiceImp.java │ │ │ ├── api │ │ │ │ ├── FeignServiceClient.java │ │ │ │ └── FeignClientController.java │ │ │ ├── domain │ │ │ │ └── User.java │ │ │ ├── controller │ │ │ │ └── UserController.java │ │ │ └── aop │ │ │ │ └── UserAspect.java │ │ │ └── Application.java │ └── resources │ │ ├── application.yml │ │ ├── com │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── organization │ │ │ └── user │ │ │ └── dao │ │ │ └── userMapper.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── bage │ └── study │ └── micro │ └── services │ └── organization │ └── AppTest.java ├── study-micro-services-eureka-client3 ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── client3 │ │ ├── Client3Application.java │ │ └── EurekaClientController.java └── study-micro-services-eureka-client3.iml ├── study-micro-services-eureka-client4 ├── src │ └── main │ │ ├── resources │ │ ├── eureka-config.properties │ │ └── ribbon-config-default.properties │ │ ├── webapp │ │ ├── index.jsp │ │ └── WEB-INF │ │ │ ├── applicationContext.xml │ │ │ ├── dispatcher-servlet.xml │ │ │ └── web.xml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── client4 │ │ ├── HelloController.java │ │ └── MyInstanceConfig.java └── study-micro-services-eureka-client4.iml ├── study-micro-services-eureka-client2 ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── client2 │ │ ├── Client2Application.java │ │ └── EurekaClientController.java └── study-micro-services-eureka-client2.iml ├── study-micro-services-zuul-dynamic-routes ├── src │ └── main │ │ ├── resources │ │ ├── data.sql │ │ ├── schema.sql │ │ ├── mybatis-config.xml │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── zuul │ │ └── dynamix │ │ └── routes │ │ ├── dao │ │ └── RouteMapper.java │ │ ├── service │ │ ├── RouteService.java │ │ └── RouteServiceImpl.java │ │ ├── RouteApplication.java │ │ ├── aop │ │ └── UserAspect.java │ │ ├── controller │ │ └── RouteController.java │ │ └── CustomRouteLocator.java ├── study-micro-services-zuul-dynamic-routes.iml └── README.md ├── study-micro-services-bus ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── bus │ │ ├── HelloReceiver.java │ │ └── BusApplication.java ├── study-micro-services-bus.iml └── pom.xml ├── study-micro-services-eureka-server2 └── src │ └── main │ ├── resources │ └── application.yml │ └── java │ └── com │ └── bage │ └── study │ └── micro │ └── services │ └── eureka │ ├── EurekaServer2Application.java │ └── EurekaClient2Controller.java ├── study-micro-services-eureka-server3 ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ ├── EurekaServer3Application.java │ │ └── EurekaClient3Controller.java └── study-micro-services-eureka-server3.iml ├── study-micro-services-eureka ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ ├── EurekaApplication.java │ │ └── EurekaClientController.java ├── pom.xml └── README.md ├── study-micro-services-bus-client ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── bus │ │ └── client │ │ ├── HelloController.java │ │ └── BusClientApplication.java ├── study-micro-services-bus-client.iml └── pom.xml ├── study-micro-services-gateway └── src │ ├── main │ ├── resources │ │ └── application.yml │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── gateway │ │ ├── EurekaClientController.java │ │ ├── CustomerGatewayFilter.java │ │ ├── GatewayApplication.java │ │ └── ThrottleGatewayFilter.java │ └── test │ └── java │ └── com │ └── bage │ └── AppTest.java ├── study-micro-services-config ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── eureka │ │ └── register │ │ └── ConfigServerApplication.java └── pom.xml ├── study-micro-services-gateway-dynamic └── src │ └── main │ ├── resources │ └── application.yml │ └── java │ └── com │ └── bage │ └── study │ └── micro │ └── services │ └── gateway │ ├── route │ ├── dao │ │ ├── RouteMapper.java │ │ └── RouteDaoImp.java │ ├── service │ │ ├── RouteService.java │ │ └── RouteServiceImp.java │ ├── domain │ │ ├── Filter.java │ │ └── Predicate.java │ └── controller │ │ └── RouteController.java │ ├── GatewayApplication.java │ └── EurekaClientController.java ├── study-micro-services-stream ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── stream │ │ └── StreamApplication.java └── study-micro-services-stream.iml ├── study-micro-services-multi-datasources ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── bage │ │ │ │ └── study │ │ │ │ └── micro │ │ │ │ └── services │ │ │ │ └── multi │ │ │ │ └── datasources │ │ │ │ ├── CustomerMapper.java │ │ │ │ ├── Customer.java │ │ │ │ ├── HomeController.java │ │ │ │ └── MultiDataSourceConfig.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── customerMapper.xml │ │ │ └── mybatis-config.xml │ └── test │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── multi │ │ └── datasources │ │ └── AppTest.java └── study-micro-services-multi-datasources.iml ├── study-micro-services-turbine ├── src │ └── main │ │ ├── resources │ │ └── application.yml │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── turbine │ │ └── TurbineApplication.java └── study-micro-services-turbine.iml ├── study-micro-services-spring-cloud-netflix ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── bage │ │ │ └── study │ │ │ └── micro │ │ │ └── services │ │ │ └── spring │ │ │ └── cloud │ │ │ └── netflix │ │ │ ├── server │ │ │ └── Application.java │ │ │ └── client │ │ │ └── Application.java │ └── test │ │ └── java │ │ └── com │ │ └── bage │ │ └── study │ │ └── micro │ │ └── services │ │ └── spring │ │ └── cloud │ │ └── netflix │ │ └── AppTest.java └── study-micro-services-spring-cloud-netflix.iml ├── study-micro-services-distribute-transaction ├── study-micro-services-distribute-transaction.iml ├── README - lock.md └── README.md ├── .gitignore.md └── pom.xml /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/resources/static/index.js: -------------------------------------------------------------------------------- 1 | // index from zuul -------------------------------------------------------------------------------- /study-micro-services-eureka-client5/src/main/resources/static/index.js: -------------------------------------------------------------------------------- 1 | // index from client5 -------------------------------------------------------------------------------- /study-micro-services-eureka-client6/src/main/resources/static/index.js: -------------------------------------------------------------------------------- 1 | // index from client6 -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=cloud-sleuth -------------------------------------------------------------------------------- /study-micro-services-h2/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO city (name, state, country) VALUES ('San Francisco', 'CA', 'US'); -------------------------------------------------------------------------------- /study-micro-services-h2/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE city ( 2 | id INT PRIMARY KEY auto_increment, 3 | name VARCHAR, 4 | state VARCHAR, 5 | country VARCHAR 6 | ); -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client5/src/test/java/com/bage/study/micro/services/eureka/client1/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client1; 2 | 3 | public class AppTest { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client6/src/test/java/com/bage/study/micro/services/eureka/client1/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client1; 2 | 3 | public class AppTest { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client1/src/test/java/com/bage/study/micro/services/eureka/client1/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client1; 2 | 3 | public class AppTest { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/java/com/bage/study/micro/services/hystrix/HystrixClientFallback.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.hystrix; 2 | 3 | public class HystrixClientFallback { 4 | } 5 | -------------------------------------------------------------------------------- /study-micro-services-config-client/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=config 2 | spring.cloud.config.profile=dev 3 | 4 | # N.B. this is the default: 5 | spring.cloud.config.uri=http://localhost:8099 -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8909 3 | 4 | eureka: 5 | instance: 6 | hostname: localhost 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-config-client/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8909 3 | 4 | eureka: 5 | instance: 6 | hostname: localhost 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:8761/eureka/ 10 | 11 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/cache/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot.cache; 2 | 3 | public interface BookRepository { 4 | 5 | Book getByIsbn(String isbn); 6 | 7 | } -------------------------------------------------------------------------------- /study-micro-services-feign/src/main/java/com/bage/App.java: -------------------------------------------------------------------------------- 1 | package com.bage; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /study-micro-services-feign/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8808 3 | 4 | Spring: 5 | application: 6 | name: cloud-feign 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-ribbon/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8088 3 | 4 | Spring: 5 | application: 6 | name: cloud-ribbon 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-eureka-client1/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8001 3 | 4 | Spring: 5 | application: 6 | name: cloud-client 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-eureka-client5/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8005 3 | 4 | Spring: 5 | application: 6 | name: cloud-client5 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-eureka-client6/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8006 3 | 4 | Spring: 5 | application: 6 | name: cloud-client6 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.dao; 2 | 3 | import com.bage.study.micro.services.organization.user.domain.User; 4 | 5 | public interface UserDao { 6 | 7 | User getUserById(String id); 8 | 9 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client3/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8003 3 | 4 | Spring: 5 | application: 6 | name: cloud-client 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/resources/eureka-config.properties: -------------------------------------------------------------------------------- 1 | eureka.region=default 2 | eureka.name=cloud-client-not-springboot 3 | eureka.vipAddress=cloud-client-not-springboot 4 | eureka.preferSameZone=true 5 | eureka.shouldUseDns=false 6 | eureka.us-east-1.availabilityZones=default 7 | eureka.serviceUrl.default=http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8098 3 | 4 | Spring: 5 | application: 6 | name: cloud-hystrix 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ 14 | 15 | feign: 16 | hystrix: 17 | enabled: true -------------------------------------------------------------------------------- /study-micro-services-eureka-client2/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8002 3 | 4 | spring: 5 | application: 6 | name: cloud-client 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | serviceUrl: 13 | defaultZone: http://localhost:8761/eureka/ 14 | 15 | logging: 16 | level: 17 | org.springframework: debug # 设置debug -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | 2 | INSERT INTO tb_route ( 3 | id, 4 | path_pattern, 5 | service_id, 6 | url, 7 | strip_prefix, 8 | retryable, 9 | sensitive_headers, 10 | custom_sensitive_headers 11 | ) 12 | VALUES 13 | ( 14 | '0', 15 | '/client6/**', 16 | 'cloud-client6', 17 | '', 18 | 'true', 19 | 'true', 20 | '', 21 | 'false' 22 | ); 23 | -------------------------------------------------------------------------------- /study-micro-services-bus/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8801 3 | 4 | spring: 5 | application: 6 | name: cloud-bus 7 | rabbitmq: 8 | host: localhost 9 | port: 8892 10 | username: admin 11 | password: admin 12 | 13 | 14 | eureka: 15 | instance: 16 | hostname: localhost 17 | client: 18 | serviceUrl: 19 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-eureka-server2/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8762 3 | 4 | Spring: 5 | application: 6 | name: cloud-server2 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | # registerWithEureka: false 13 | # fetchRegistry: false 14 | serviceUrl: 15 | defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ 16 | -------------------------------------------------------------------------------- /study-micro-services-eureka-server3/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8763 3 | 4 | Spring: 5 | application: 6 | name: cloud-server3 7 | 8 | eureka: 9 | instance: 10 | hostname: localhost 11 | client: 12 | # registerWithEureka: false 13 | # fetchRegistry: false 14 | serviceUrl: 15 | defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ 16 | -------------------------------------------------------------------------------- /study-micro-services-eureka/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8761 3 | 4 | eureka: 5 | instance: 6 | hostname: localhost 7 | client: 8 | registerWithEureka: false # 不注册自己 9 | fetchRegistry: false # 不抓取注册列表 10 | serviceUrl: 11 | defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka 12 | 13 | logging: 14 | level: 15 | org.springframework: debug # 设置debug -------------------------------------------------------------------------------- /study-micro-services-bus-client/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8802 3 | 4 | spring: 5 | application: 6 | name: cloud-bus-client 7 | rabbitmq: 8 | host: localhost 9 | port: 8892 10 | username: admin 11 | password: admin 12 | 13 | 14 | eureka: 15 | instance: 16 | hostname: localhost 17 | client: 18 | serviceUrl: 19 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.service; 2 | 3 | import java.util.List; 4 | 5 | import com.bage.study.micro.services.organization.user.domain.User; 6 | 7 | public interface UserService { 8 | 9 | User getUserById(String id); 10 | 11 | List queryAll(); 12 | 13 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: lenovo 4 | Date: 2017/5/18 5 | Time: 10:31 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | $Title$ 12 | 13 | 14 | $END$ 15 | 16 | 17 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | --------------- H2 --------------- 2 | 3 | drop table if exists tb_route; 4 | create table tb_route ( 5 | id VARCHAR(255) PRIMARY KEY, 6 | path_pattern VARCHAR(255), 7 | service_id VARCHAR(255), 8 | url VARCHAR(1024), 9 | strip_prefix VARCHAR(8), 10 | retryable VARCHAR(8), 11 | sensitive_headers VARCHAR(255), 12 | custom_sensitive_headers VARCHAR(8) 13 | ); -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/dao/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.dao; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Mapper; 6 | 7 | import com.bage.study.micro.services.organization.user.domain.User; 8 | 9 | @Mapper 10 | public interface UserMapper { 11 | 12 | List queryAll(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/aop/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot.aop.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | @Controller 7 | @RequestMapping 8 | public class UserController { 9 | 10 | public String get() { 11 | return ""; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /study-micro-services-gateway/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 8888 4 | 5 | Spring: 6 | application: 7 | name: cloud-gateway 8 | cloud: 9 | gateway: 10 | routes: 11 | - id: cloud-client5 12 | uri: lb://cloud-client5 13 | predicates: 14 | - Path=/foo/** 15 | 16 | eureka: 17 | instance: 18 | hostname: localhost 19 | client: 20 | serviceUrl: 21 | defaultZone: http://localhost:8761/eureka/ 22 | 23 | -------------------------------------------------------------------------------- /study-micro-services-config/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8099 3 | 4 | Spring: 5 | application: 6 | name: cloud-config 7 | cloud: 8 | config: 9 | server: 10 | git: 11 | uri: https://github.com/bage2014/config-center 12 | search-paths: /** 13 | 14 | 15 | eureka: 16 | instance: 17 | hostname: localhost 18 | client: 19 | serviceUrl: 20 | defaultZone: http://localhost:8761/eureka/ 21 | 22 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | server: 3 | port: 8888 4 | 5 | Spring: 6 | application: 7 | name: cloud-gateway 8 | cloud: 9 | gateway: 10 | routes: 11 | - id: cloud-client5 12 | uri: lb://cloud-client5 13 | predicates: 14 | - Path=/client5/** 15 | 16 | eureka: 17 | instance: 18 | hostname: localhost 19 | client: 20 | serviceUrl: 21 | defaultZone: http://localhost:8761/eureka/ 22 | 23 | -------------------------------------------------------------------------------- /study-micro-services-feign/README.md: -------------------------------------------------------------------------------- 1 | # study-micro-services-feign # 2 | 3 | ## 参考链接 ## 4 | -Spring Cloud OpenFeign [https://spring.io/projects/spring-cloud-openfeign#overview](https://spring.io/projects/spring-cloud-openfeign#overview "Spring Cloud OpenFeign")、 [https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.1.0.M2/single/spring-cloud-openfeign.html](https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.1.0.M2/single/spring-cloud-openfeign.html "Spring Cloud OpenFeign") 5 | 6 | - -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/dao/RouteMapper.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.dao; 2 | 3 | import com.bage.study.micro.services.gateway.route.domain.Route; 4 | 5 | import java.util.List; 6 | 7 | public interface RouteMapper { 8 | 9 | int insert(Route route); 10 | 11 | int delete(String id); 12 | 13 | int update(Route route); 14 | 15 | int query(String id); 16 | 17 | List queryAll(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/java/com/bage/study/micro/services/sleuth/FeignServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.sleuth; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Service 8 | @FeignClient("CLOUD-CLIENT") 9 | public interface FeignServiceClient { 10 | 11 | @RequestMapping("/eurekaClient/feign") 12 | public String feign() ; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /study-micro-services-stream/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8803 3 | 4 | logging: 5 | level: 6 | root: info 7 | 8 | spring: 9 | application: 10 | name: cloud-stream 11 | rabbitmq: 12 | host: localhost 13 | port: 8892 14 | username: {your username} 15 | password: {your password} 16 | virtual-host: / 17 | 18 | eureka: 19 | instance: 20 | hostname: localhost 21 | client: 22 | serviceUrl: 23 | defaultZone: http://localhost:8761/eureka/ 24 | 25 | -------------------------------------------------------------------------------- /study-micro-services-h2/src/main/java/com/bage/study/micro/services/h2/demo/mybatis/CustomerMapper.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.h2.demo.mybatis; 2 | 3 | import com.bage.study.micro.services.h2.domain.Customer; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Select; 6 | 7 | @Mapper 8 | public interface CustomerMapper { 9 | 10 | @Select("SELECT id, first_name, last_name FROM customers WHERE id = #{id}") 11 | Customer queryById(String id); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/java/com/bage/study/micro/services/multi/datasources/CustomerMapper.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.multi.datasources; 2 | 3 | import org.apache.ibatis.annotations.Mapper; 4 | import org.apache.ibatis.annotations.Select; 5 | 6 | import java.util.List; 7 | 8 | @Mapper 9 | public interface CustomerMapper { 10 | 11 | @Select("SELECT id, first_name, last_name FROM customers WHERE id = #{id}") 12 | Customer queryById(String id); 13 | 14 | List queryAll(); 15 | 16 | } -------------------------------------------------------------------------------- /study-micro-services-bus/src/main/java/com/bage/study/micro/services/bus/HelloReceiver.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.bus; 2 | 3 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 4 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | @RabbitListener(queues = "hello") 9 | public class HelloReceiver { 10 | 11 | @RabbitHandler 12 | public void process(String text) { 13 | System.out.println("hello, " + text); 14 | } 15 | } -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/service/RouteService.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.service; 2 | 3 | import com.bage.study.micro.services.gateway.route.domain.Route; 4 | 5 | import java.util.List; 6 | 7 | public interface RouteService { 8 | 9 | int insert(Route route); 10 | 11 | int delete(String id); 12 | 13 | int update(Route route); 14 | 15 | int query(String id); 16 | 17 | List queryAll(); 18 | 19 | void refresh(); 20 | } 21 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/api/FeignServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.api; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Service 8 | @FeignClient("CLOUD-CLIENT") 9 | public interface FeignServiceClient { 10 | 11 | @RequestMapping("/eurekaClient/feign") 12 | public String feign() ; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /study-micro-services-feign/src/main/java/com/bage/study/micro/services/feign/FeignServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.feign; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.stereotype.Service; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | 8 | @Service 9 | @FeignClient("CLOUD-CLIENT") 10 | public interface FeignServiceClient { 11 | 12 | @RequestMapping("/eurekaClient/feign") 13 | public String feign() ; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/java/com/bage/study/micro/services/hystrix/FeignServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.hystrix; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Service 8 | @FeignClient(value = "CLOUD-CLIENT"/*,fallback = HystrixClientFallback.class*/) 9 | public interface FeignServiceClient { 10 | 11 | @RequestMapping("/eurekaClient/feign") 12 | public String feign() ; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /study-micro-services-turbine/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8989 3 | 4 | logging: 5 | level: 6 | root: info 7 | 8 | Spring: 9 | application: 10 | name: cloud-turbine 11 | 12 | eureka: 13 | instance: 14 | hostname: localhost 15 | client: 16 | serviceUrl: 17 | defaultZone: http://localhost:8761/eureka/ 18 | 19 | feign: 20 | hystrix: 21 | enabled: true 22 | 23 | turbine: 24 | aggregator: 25 | cluster-config: default 26 | app-config: cloud-client,cloud-hystrix 27 | cluster-name-expression: new String("default") 28 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/dao/RouteMapper.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes.dao; 2 | 3 | import com.bage.study.micro.services.zuul.dynamix.routes.domain.Route; 4 | import org.apache.ibatis.annotations.Mapper; 5 | 6 | import java.util.List; 7 | 8 | @Mapper 9 | public interface RouteMapper { 10 | 11 | int insert(Route route); 12 | 13 | int delete(String id); 14 | 15 | int update(Route route); 16 | 17 | Route query(String id); 18 | 19 | List queryAll(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /study-micro-services-spring-cloud-netflix/src/main/java/com/bage/study/micro/services/spring/cloud/netflix/server/Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.cloud.netflix.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 Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Application.class, args); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/service/RouteService.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes.service; 2 | 3 | import com.bage.study.micro.services.zuul.dynamix.routes.domain.Route; 4 | 5 | import java.util.List; 6 | 7 | public interface RouteService { 8 | 9 | int insert(Route route); 10 | 11 | int delete(String id); 12 | 13 | int update(Route route); 14 | 15 | Route query(String id); 16 | 17 | List queryAll(); 18 | 19 | List queryAllFromCache(); 20 | 21 | List refresh(); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /study-micro-services-eureka/src/main/java/com/bage/study/micro/services/eureka/EurekaApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka; 2 | import org.springframework.boot.SpringApplication; 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 5 | 6 | /** 7 | * 8 | * @author Gunnar Hillert 9 | * 10 | */ 11 | @SpringBootApplication 12 | @EnableEurekaServer 13 | public class EurekaApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | SpringApplication.run(EurekaApplication.class, args); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-server2/src/main/java/com/bage/study/micro/services/eureka/EurekaServer2Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka; 2 | import org.springframework.boot.SpringApplication; 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 5 | 6 | /** 7 | * 8 | * @author Gunnar Hillert 9 | * 10 | */ 11 | @SpringBootApplication 12 | @EnableEurekaServer 13 | public class EurekaServer2Application { 14 | 15 | public static void main(String[] args) throws Exception { 16 | SpringApplication.run(EurekaServer2Application.class, args); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-server3/src/main/java/com/bage/study/micro/services/eureka/EurekaServer3Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka; 2 | import org.springframework.boot.SpringApplication; 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 5 | 6 | /** 7 | * 8 | * @author Gunnar Hillert 9 | * 10 | */ 11 | @SpringBootApplication 12 | @EnableEurekaServer 13 | public class EurekaServer3Application { 14 | 15 | public static void main(String[] args) throws Exception { 16 | SpringApplication.run(EurekaServer3Application.class, args); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /study-micro-services-distribute-transaction/study-micro-services-distribute-transaction.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | import org.springframework.context.annotation.ComponentScan; 7 | 8 | @EnableAutoConfiguration 9 | @ComponentScan(basePackageClasses=Application.class) 10 | @EnableCaching 11 | public class Application { 12 | public static void main(String[] args) { 13 | SpringApplication.run(Application.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /study-micro-services-feign/src/main/java/com/bage/study/micro/services/feign/FeignClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.feign; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | @RequestMapping("/eurekaClient") 10 | public class FeignClientController { 11 | 12 | @Autowired 13 | FeignServiceClient feignServiceClient; 14 | 15 | @GetMapping("/feign") 16 | public String feign() { 17 | return feignServiceClient.feign(); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/java/com/bage/study/micro/services/hystrix/FeignClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.hystrix; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | @RequestMapping("/eurekaClient") 10 | public class FeignClientController { 11 | 12 | @Autowired 13 | FeignServiceClient feignServiceClient; 14 | 15 | @GetMapping("/feign") 16 | public String feign() { 17 | return feignServiceClient.feign(); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/GreetingController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.Model; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | 8 | @Controller 9 | public class GreetingController { 10 | 11 | @GetMapping("/greeting") 12 | public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) { 13 | model.addAttribute("name", name); 14 | return "index"; 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | mybatis: 2 | config: 3 | location: mybatis-config.xml 4 | spring: 5 | datasource: 6 | db1: 7 | jdbcUrl: jdbc:h2:~/test1;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE 8 | username: sa 9 | password: 10 | driver-class-name: org.h2.Driver 11 | db2: 12 | jdbcUrl: jdbc:h2:~/test2;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE 13 | username: sa 14 | password: 15 | driver-class-name: org.h2.Driver 16 | h2: 17 | console: 18 | path: /h2-console # h2 web consloe 路径 19 | enabled: true # 开启 h2 web consloe,默认为 false 20 | settings: 21 | web-allow-others: true #允许远程访问 h2 web consloe -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8888 3 | 4 | Spring: 5 | application: 6 | name: cloud-ribbon 7 | 8 | zuul: 9 | ignored-services: ignored-dao # 需要忽视的服务(配置后将不会被路由) 10 | routes: 11 | cloud-ribbon: 12 | path: /cloud-ribbon/** 13 | serviceId: cloud-ribbon 14 | cloud-feign: 15 | path: /cloud-feign/** 16 | serviceId: cloud-feign 17 | 18 | cloud-client5: 19 | path: /cloud-client5/** 20 | serviceId: cloud-client5 21 | cloud-client6: 22 | path: /cloud-client6/** 23 | url: http://localhost:8006/ # 也可以指定为url 24 | 25 | eureka: 26 | instance: 27 | hostname: localhost 28 | client: 29 | serviceUrl: 30 | defaultZone: http://localhost:8761/eureka/ -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Demo 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |

Spring Boot Demo

16 |
17 | 18 | -------------------------------------------------------------------------------- /study-micro-services-spring-cloud-netflix/src/main/java/com/bage/study/micro/services/spring/cloud/netflix/client/Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.cloud.netflix.client; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @SpringBootApplication 9 | @RestController 10 | public class Application { 11 | 12 | @RequestMapping("/") 13 | public String home() { 14 | return "Hello world"; 15 | } 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(Application.class, args); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.domain; 2 | 3 | public class User { 4 | 5 | private String id; 6 | private String name; 7 | 8 | public User() { 9 | } 10 | public User(String id, String name) { 11 | super(); 12 | this.id = id; 13 | this.name = name; 14 | } 15 | public String getId() { 16 | return id; 17 | } 18 | public void setId(String id) { 19 | this.id = id; 20 | } 21 | public String getName() { 22 | return name; 23 | } 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | @Override 28 | public String toString() { 29 | return "User [id=" + id + ", name=" + name + "]"; 30 | } 31 | 32 | 33 | 34 | } -------------------------------------------------------------------------------- /study-micro-services-bus-client/src/main/java/com/bage/study/micro/services/bus/client/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.bus.client; 2 | 3 | import org.springframework.amqp.core.AmqpTemplate; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | @RequestMapping("/hello") 11 | public class HelloController { 12 | 13 | @Autowired 14 | AmqpTemplate amqpTemplate; 15 | 16 | @RequestMapping("/{msg}") 17 | public String sendMsg(@PathVariable(value="msg") String msg) { 18 | amqpTemplate.convertAndSend("hello",msg); 19 | return msg; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /study-micro-services-bus/study-micro-services-bus.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/java/com/bage/study/micro/services/eureka/client4/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client4; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.ResponseBody; 6 | 7 | /** 8 | * 9 | */ 10 | @Controller 11 | public class HelloController { 12 | 13 | @RequestMapping("/hello") 14 | @ResponseBody 15 | public String hello(){ 16 | return "Hello Not Spring Boot Client"; 17 | } 18 | 19 | @RequestMapping("/ribbon") 20 | @ResponseBody 21 | public String ribbon(){ 22 | System.out.println("hello ribbon"); 23 | return RibbonRestClient.executeWithLoadBalancer("cloud-ribbon" , ""); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /study-micro-services-feign/study-micro-services-feign.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-sleuth/study-micro-services-sleuth.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-stream/study-micro-services-stream.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-turbine/study-micro-services-turbine.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-bus-client/study-micro-services-bus-client.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Demo 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |

Spring Boot Demo

16 |
17 |

18 |

19 | 20 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client2/study-micro-services-eureka-client2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client3/study-micro-services-eureka-client3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/study-micro-services-eureka-client4.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-eureka-server3/study-micro-services-eureka-server3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-spring-cloud-netflix/study-micro-services-spring-cloud-netflix.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/study-micro-services-zuul-dynamic-routes.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/cache/SimpleBookRepository.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot.cache; 2 | 3 | import org.springframework.cache.annotation.Cacheable; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class SimpleBookRepository implements BookRepository { 8 | 9 | @Cacheable("books") 10 | public Book getByIsbn(String isbn) { 11 | simulateSlowService(); 12 | return new Book(isbn, "Some book"); 13 | } 14 | 15 | // Don't do this at home 16 | private void simulateSlowService() { 17 | try { 18 | long time = 3000L; 19 | Thread.sleep(time); 20 | } catch (InterruptedException e) { 21 | throw new IllegalStateException(e); 22 | } 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway; 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.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @EnableDiscoveryClient 10 | @SpringBootApplication 11 | @RestController 12 | public class GatewayApplication { 13 | 14 | @RequestMapping("/") 15 | public String home() { 16 | return "Hello Spring Gateway"; 17 | } 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(GatewayApplication.class, args); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/cache/Book.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot.cache; 2 | 3 | public class Book { 4 | 5 | private String isbn; 6 | private String title; 7 | 8 | public Book(String isbn, String title) { 9 | this.isbn = isbn; 10 | this.title = title; 11 | } 12 | 13 | public String getIsbn() { 14 | return isbn; 15 | } 16 | 17 | public void setIsbn(String isbn) { 18 | this.isbn = isbn; 19 | } 20 | 21 | public String getTitle() { 22 | return title; 23 | } 24 | 25 | public void setTitle(String title) { 26 | this.title = title; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "Book{" + "isbn='" + isbn + '\'' + ", title='" + title + '\'' + '}'; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/java/com/bage/study/micro/services/sleuth/FeignClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.sleuth; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @RequestMapping("/eurekaClient") 12 | public class FeignClientController { 13 | 14 | private static Logger log = LoggerFactory.getLogger(FeignClientController.class); 15 | 16 | 17 | @Autowired 18 | FeignServiceClient feignServiceClient; 19 | 20 | @GetMapping("/feign") 21 | public String feign() { 22 | log.info("----------feign----------"); 23 | return feignServiceClient.feign(); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /study-micro-services-h2/study-micro-services-h2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /study-micro-services-h2/src/main/java/com/bage/study/micro/services/h2/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.h2.domain; 2 | 3 | 4 | public class Customer { 5 | 6 | private long id; 7 | private String firstName, lastName; 8 | 9 | public Customer(int id, String firstName, String lastName) { 10 | this.id = id; 11 | this.firstName = firstName; 12 | this.lastName = lastName; 13 | } 14 | 15 | public Customer(long id, String firstName, String lastName) { 16 | this.id = id; 17 | this.firstName = firstName; 18 | this.lastName = lastName; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return String.format( 24 | "Customer[id=%d, firstName='%s', lastName='%s']", 25 | id, firstName, lastName); 26 | } 27 | 28 | // getters & setters omitted for brevity 29 | } 30 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/java/com/bage/study/micro/services/multi/datasources/Customer.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.multi.datasources; 2 | 3 | 4 | public class Customer { 5 | 6 | private long id; 7 | private String firstName, lastName; 8 | 9 | public Customer(int id, String firstName, String lastName) { 10 | this.id = id; 11 | this.firstName = firstName; 12 | this.lastName = lastName; 13 | } 14 | 15 | public Customer(long id, String firstName, String lastName) { 16 | this.id = id; 17 | this.firstName = firstName; 18 | this.lastName = lastName; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return String.format( 24 | "Customer[id=%d, firstName='%s', lastName='%s']", 25 | id, firstName, lastName); 26 | } 27 | 28 | // getters & setters omitted for brevity 29 | } 30 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8084 3 | 4 | mybatis: 5 | # Eclipse 配置 6 | # config: 7 | # location: mybatis-config.xml 8 | 9 | # Idea 配置 10 | config-location: mybatis-config.xml 11 | 12 | Spring: 13 | application: 14 | name: cloud-organization 15 | h2: 16 | console: 17 | path: /h2-console # h2 web consloe 路径 18 | enabled: true # 开启 h2 web consloe,默认为 false 19 | settings: 20 | web-allow-others: true #允许远程访问 h2 web consloe 21 | datasource: 22 | username: sa 23 | url: jdbc:h2:~/test;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE 24 | driver-class-name: org.h2.Driver 25 | cloud: 26 | refresh: 27 | refreshable: none 28 | 29 | eureka: 30 | instance: 31 | hostname: localhost 32 | client: 33 | serviceUrl: 34 | defaultZone: http://localhost:8761/eureka/ 35 | 36 | -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/java/com/bage/study/micro/services/sleuth/LogController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.sleuth; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestParam; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @RequestMapping("/log") 12 | public class LogController { 13 | 14 | Logger logger = LoggerFactory.getLogger(LogController.class); 15 | 16 | @RequestMapping("/param") 17 | public String run(@RequestParam(value = "param", defaultValue = "bage") String param) throws Exception { 18 | logger.info("param = {}", param); 19 | logger.error("something run. param = {}", param); 20 | 21 | 22 | return ""; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /study-micro-services-h2/src/test/java/com/bage/study/micro/services/h2/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.h2; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/service/UserServiceImp.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.bage.study.micro.services.organization.user.dao.UserDao; 9 | import com.bage.study.micro.services.organization.user.dao.UserMapper; 10 | import com.bage.study.micro.services.organization.user.domain.User; 11 | 12 | @Service 13 | public class UserServiceImp implements UserService { 14 | 15 | @Autowired 16 | UserDao userDao; 17 | @Autowired 18 | UserMapper userMapper; 19 | 20 | public User getUserById(String id){ 21 | return userDao.getUserById(id); 22 | } 23 | 24 | public List queryAll() { 25 | return userMapper.queryAll(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/study-micro-services-spring-boot.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client1/study-micro-services-eureka-client1.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client5/study-micro-services-eureka-client5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client6/study-micro-services-eureka-client6.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/study-micro-services-multi-datasources.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /study-micro-services-ribbon/src/test/java/com/bage/study/micro/services/ribbon/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.ribbon; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/resources/ribbon-config-default.properties: -------------------------------------------------------------------------------- 1 | # Max number of retries on the same server (excluding the first try) 2 | liems-gateway.ribbon.MaxAutoRetries=1 3 | 4 | # Max number of next servers to retry (excluding the first server) 5 | liems-gateway.ribbon.MaxAutoRetriesNextServer=1 6 | 7 | # Whether all operations can be retried for this client 8 | liems-gateway.ribbon.OkToRetryOnAllOperations=true 9 | 10 | # Interval to refresh the server list from the source 11 | liems-gateway.ribbon.ServerListRefreshInterval=2000 12 | 13 | # Connect timeout used by Apache HttpClient 14 | liems-gateway.ribbon.ConnectTimeout=3000 15 | 16 | # Read timeout used by Apache HttpClient 17 | liems-gateway.ribbon.ReadTimeout=3000 18 | 19 | # Initial list of servers, can be changed via Archaius dynamic property at runtime 20 | liems-gateway.ribbon.listOfServers=localhost:8888 21 | 22 | liems-gateway.ribbon.EnablePrimeConnections=true 23 | 24 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/test/java/com/bage/study/micro/services/organization/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/test/java/com/bage/study/micro/services/spring/boot/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/webapp/WEB-INF/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/java/com/bage/study/micro/services/hystrix/HystrixClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.hystrix; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.HashMap; 10 | 11 | @RestController 12 | @RequestMapping("/hystrixClient") 13 | public class HystrixClientController { 14 | 15 | @Autowired 16 | StoreIntegration storeIntegration; 17 | 18 | @GetMapping("/rest/success") 19 | public String succes() { 20 | return String.valueOf(storeIntegration.getStores(null)); 21 | } 22 | @GetMapping("/rest/error") 23 | public String error() { 24 | return String.valueOf(storeIntegration.getStores(new HashMap<>())); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/test/java/com/bage/study/micro/services/multi/datasources/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.multi.datasources; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/dao/UserDaoImp.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.dao; 2 | 3 | import org.springframework.cache.annotation.Cacheable; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.bage.study.micro.services.organization.user.domain.User; 7 | 8 | @Repository 9 | public class UserDaoImp implements UserDao { 10 | 11 | @Cacheable("books") 12 | public User getUserById(String id) { 13 | simulateSlowService(); 14 | return new User(id, "Some book"); 15 | } 16 | 17 | // Don't do this at home 18 | private void simulateSlowService() { 19 | try { 20 | System.out.println("正在查询数据库 。。。 "); 21 | long time = 300L; 22 | Thread.sleep(time); 23 | } catch (InterruptedException e) { 24 | throw new IllegalStateException(e); 25 | } 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /study-micro-services-spring-cloud-netflix/src/test/java/com/bage/study/micro/services/spring/cloud/netflix/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.cloud.netflix; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/AddResponseHeaderFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import com.netflix.zuul.ZuulFilter; 4 | import com.netflix.zuul.context.RequestContext; 5 | import org.springframework.stereotype.Component; 6 | 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.util.UUID; 9 | 10 | /** 11 | * @author Spencer Gibb 12 | */ 13 | @Component 14 | public class AddResponseHeaderFilter extends ZuulFilter { 15 | public String filterType() { 16 | return "post"; 17 | } 18 | 19 | public int filterOrder() { 20 | return 999; 21 | } 22 | 23 | public boolean shouldFilter() { 24 | return true; 25 | } 26 | 27 | public Object run() { 28 | RequestContext context = RequestContext.getCurrentContext(); 29 | HttpServletResponse servletResponse = context.getResponse(); 30 | servletResponse.addHeader("X-Foo", 31 | UUID.randomUUID().toString()); 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client3/src/main/java/com/bage/study/micro/services/eureka/client3/Client3Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client3; 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.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | 11 | @SpringBootApplication 12 | @EnableDiscoveryClient 13 | @RestController 14 | public class Client3Application { 15 | 16 | @RequestMapping("/") 17 | public String home(HttpServletRequest request) { 18 | System.out.println("这是client 3 "); 19 | return "Hello 3 " + request.getContextPath(); 20 | } 21 | 22 | public static void main(String[] args) { 23 | SpringApplication.run(Client3Application.class, args); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client1/src/main/java/com/bage/study/micro/services/eureka/client1/Client1Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client1; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @SpringBootApplication 12 | @EnableDiscoveryClient 13 | @RestController 14 | public class Client1Application { 15 | 16 | @RequestMapping("/") 17 | public String home(HttpServletRequest request) { 18 | System.out.println("这是client 1 "); 19 | return "Hello 1 " + request.getContextPath(); 20 | } 21 | 22 | public static void main(String[] args) { 23 | SpringApplication.run(Client1Application.class, args); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/api/FeignClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.api; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.Map; 10 | 11 | @RestController 12 | @RequestMapping("/eurekaClient") 13 | public class FeignClientController { 14 | 15 | @Autowired 16 | FeignServiceClient feignServiceClient; 17 | 18 | @GetMapping("/feign") 19 | @HystrixCommand(fallbackMethod = "defaultFallbackMethod") 20 | public String feign() { 21 | return feignServiceClient.feign(); 22 | } 23 | 24 | public String defaultFallbackMethod() { 25 | return "defaultStores"; /* something useful */ 26 | } 27 | 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/java/com/bage/study/micro/services/multi/datasources/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.multi.datasources; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.jdbc.core.JdbcTemplate; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @RequestMapping("/home") 12 | public class HomeController { 13 | 14 | @Autowired 15 | @Qualifier("jdbcTemplate1") 16 | JdbcTemplate jdbcTemplate1; 17 | 18 | @Autowired 19 | @Qualifier("jdbcTemplate2") 20 | JdbcTemplate jdbcTemplate2; 21 | 22 | @GetMapping("/index") 23 | public String client() { 24 | StringBuilder stringBuilder = new StringBuilder(); 25 | stringBuilder.append("home"); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /study-micro-services-config-client/src/main/java/com/bage/study/micro/services/eureka/register/ConfigClientApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.register; 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.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | 11 | @SpringBootApplication 12 | @EnableDiscoveryClient 13 | @RestController 14 | public class ConfigClientApplication { 15 | 16 | @RequestMapping("/home") 17 | public String home(HttpServletRequest request) { 18 | System.out.println("这是cloud config client"); 19 | return "Hello config " + request.getContextPath(); 20 | } 21 | 22 | public static void main(String[] args) { 23 | SpringApplication.run(ConfigClientApplication.class, args); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client2/src/main/java/com/bage/study/micro/services/eureka/client2/Client2Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client2; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @SpringBootApplication 12 | //@EnableDiscoveryClient // 在 spring-cloud-dependencies的Finchley.SR2 版本中可以不用添加此注解 13 | @RestController 14 | public class Client2Application { 15 | 16 | @RequestMapping("/home") 17 | public String home(HttpServletRequest request) { 18 | System.out.println("这是client 2 "); 19 | return "Hello 2 " + request.getContextPath(); 20 | } 21 | 22 | public static void main(String[] args) { 23 | SpringApplication.run(Client2Application.class, args); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-server2/src/main/java/com/bage/study/micro/services/eureka/EurekaClient2Controller.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("eurekaClient") 13 | public class EurekaClient2Controller { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /study-micro-services-gateway/src/main/java/com/bage/study/micro/services/gateway/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.client.discovery.DiscoveryClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/CounterController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import java.util.concurrent.atomic.AtomicInteger; 8 | 9 | @RestController 10 | @RequestMapping("/count") 11 | public class CounterController { 12 | 13 | private int counter1 = 0; 14 | private AtomicInteger counter2 = new AtomicInteger(); 15 | 16 | @RequestMapping("/") 17 | String counter1(HttpServletRequest request) { 18 | System.out.println("home当前次数:" + (counter1 ++ ) + ":" + request.getRemoteHost()); 19 | counter2(request); 20 | return "Hello World!"; 21 | } 22 | 23 | 24 | String counter2(HttpServletRequest request) { 25 | System.out.println("counter当前次数:" + counter2.incrementAndGet() + ":" + request.getRemoteHost()); 26 | return "Hello World!"; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /study-micro-services-eureka-server3/src/main/java/com/bage/study/micro/services/eureka/EurekaClient3Controller.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("server/eurekaClient") 13 | public class EurekaClient3Controller { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.client.discovery.DiscoveryClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /study-micro-services-eureka/src/main/java/com/bage/study/micro/services/eureka/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.client.discovery.DiscoveryClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /study-micro-services-config/src/main/java/com/bage/study/micro/services/eureka/register/ConfigServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.register; 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 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | @RestController 15 | @EnableConfigServer 16 | public class ConfigServerApplication { 17 | 18 | @RequestMapping("/home") 19 | public String home(HttpServletRequest request) { 20 | System.out.println("这是cloud config "); 21 | return "Hello config " + request.getContextPath(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(ConfigServerApplication.class, args); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/webapp/WEB-INF/dispatcher-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/resources/customerMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 17 | 18 | 21 | -------------------------------------------------------------------------------- /study-micro-services-ribbon/src/main/java/com/bage/study/micro/services/ribbon/RibbonApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.ribbon; 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.context.annotation.Bean; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | @RestController 15 | public class RibbonApplication { 16 | 17 | @Bean 18 | @LoadBalanced 19 | RestTemplate restTemplate(){ 20 | return new RestTemplate(); 21 | } 22 | 23 | @RequestMapping("/") 24 | public String home() { 25 | return "Hello world"; 26 | } 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(RibbonApplication.class, args); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import com.bage.study.micro.services.organization.user.domain.User; 12 | import com.bage.study.micro.services.organization.user.service.UserService; 13 | 14 | @Controller 15 | @RequestMapping("/user") 16 | public class UserController { 17 | 18 | @Autowired 19 | UserService userService; 20 | 21 | @RequestMapping("/{id}") 22 | @ResponseBody 23 | public User getUser(@PathVariable String id) { 24 | return userService.getUserById(id); 25 | } 26 | 27 | @RequestMapping("/all") 28 | @ResponseBody 29 | public List getUser() { 30 | return userService.queryAll(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/ZuulApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul; 2 | 3 | import com.bage.study.micro.services.zuul.filters.PreRequestLogFilter; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @SpringBootApplication 13 | @EnableDiscoveryClient 14 | @RestController 15 | @EnableZuulProxy 16 | public class ZuulApplication { 17 | 18 | @Bean 19 | public PreRequestLogFilter preRequestLogFilter() { 20 | return new PreRequestLogFilter(); 21 | } 22 | 23 | @RequestMapping("/") 24 | public String home() { 25 | return "Hello world"; 26 | } 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(ZuulApplication.class, args); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/RouteApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.web.ServerProperties; 7 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 8 | import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | @SpringBootApplication 12 | @EnableZuulProxy 13 | public class RouteApplication { 14 | 15 | @Autowired 16 | ServerProperties serverProperties; 17 | 18 | @Autowired 19 | ZuulProperties zuulProperties; 20 | 21 | @Bean 22 | public CustomRouteLocator customRouteLocator(){ 23 | return new CustomRouteLocator(serverProperties.getServlet().getPath(),zuulProperties); 24 | } 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(RouteApplication.class, args); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /study-micro-services-bus/src/main/java/com/bage/study/micro/services/bus/BusApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.bus; 2 | 3 | import com.rabbitmq.client.AMQP; 4 | import org.springframework.amqp.core.Queue; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | @SpringBootApplication 15 | @RestController 16 | @EnableDiscoveryClient 17 | public class BusApplication { 18 | 19 | @RequestMapping("/") 20 | public String home() { 21 | return "Bus,Hello World"; 22 | } 23 | 24 | @Bean 25 | public Queue helloQueue() { 26 | return new Queue("hello"); 27 | } 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(BusApplication.class, args); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/QueryParamServiceIdPreFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import com.netflix.zuul.ZuulFilter; 6 | import com.netflix.zuul.context.RequestContext; 7 | import org.springframework.stereotype.Component; 8 | 9 | import static com.netflix.zuul.context.RequestContext.getCurrentContext; 10 | 11 | /** 12 | * @author Spencer Gibb 13 | */ 14 | @Component 15 | public class QueryParamServiceIdPreFilter extends ZuulFilter { 16 | 17 | public int filterOrder() { 18 | // run before PreDecorationFilter 19 | return 5 - 1; 20 | } 21 | 22 | public String filterType() { 23 | return "pre"; 24 | } 25 | 26 | @Override 27 | public boolean shouldFilter() { 28 | RequestContext ctx = getCurrentContext(); 29 | return ctx.getRequest().getParameter("dao") != null; 30 | } 31 | 32 | public Object run() { 33 | RequestContext ctx = getCurrentContext(); 34 | HttpServletRequest request = ctx.getRequest(); 35 | // put the serviceId in `RequestContext` 36 | ctx.put("serviceId", request.getParameter("dao")); 37 | return null; 38 | } 39 | } -------------------------------------------------------------------------------- /study-micro-services-bus-client/src/main/java/com/bage/study/micro/services/bus/client/BusClientApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.bus.client; 2 | 3 | import org.springframework.amqp.core.Queue; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | @SpringBootApplication 14 | @RestController 15 | @EnableDiscoveryClient 16 | public class BusClientApplication { 17 | 18 | @RequestMapping("/") 19 | public String home() { 20 | return "Bus Client,Hello World"; 21 | } 22 | 23 | 24 | @Bean 25 | public Queue helloQueue() { 26 | return new Queue("hello"); 27 | } 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(BusClientApplication.class, args); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/dao/RouteDaoImp.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.dao; 2 | 3 | import com.bage.study.micro.services.gateway.route.domain.Route; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | @Service 10 | public class RouteDaoImp implements RouteMapper { 11 | 12 | List list = new ArrayList<>(); 13 | 14 | @Override 15 | public int insert(Route route){ 16 | return list.add(route) ? 1 : 0; 17 | } 18 | @Override 19 | public int delete(String id){ 20 | Route route = new Route(); 21 | route.setId(id); 22 | return list.remove(route) ? 1 : 0; 23 | } 24 | 25 | @Override 26 | public int update(Route route) { 27 | list.set(list.indexOf(route),route); 28 | return 1; 29 | } 30 | 31 | @Override 32 | public int query(String id) { 33 | Route route = new Route(); 34 | route.setId(id); 35 | return list.indexOf(id); 36 | } 37 | 38 | @Override 39 | public List queryAll(){ 40 | return list; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /study-micro-services-feign/src/main/java/com/bage/study/micro/services/feign/FeignApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.feign; 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.openfeign.EnableFeignClients; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | import org.springframework.web.client.RestTemplate; 12 | 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | @RestController 16 | @EnableFeignClients 17 | public class FeignApplication { 18 | 19 | @Bean 20 | @LoadBalanced 21 | RestTemplate restTemplate(){ 22 | return new RestTemplate(); 23 | } 24 | 25 | @RequestMapping("/") 26 | public String home() { 27 | return "Hello world"; 28 | } 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(FeignApplication.class, args); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client3/src/main/java/com/bage/study/micro/services/eureka/client3/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client3; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.client.discovery.DiscoveryClient; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | @GetMapping("/feign") 30 | public String feign() { 31 | System.out.println("22222"); 32 | return "2"; 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/resources/com/bage/study/micro/services/organization/user/dao/userMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 17 | 18 | 21 | -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/java/com/bage/study/micro/services/sleuth/SleuthApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.sleuth; 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.openfeign.EnableFeignClients; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | import org.springframework.web.client.RestTemplate; 12 | 13 | @SpringBootApplication 14 | @EnableDiscoveryClient 15 | @RestController 16 | @EnableFeignClients 17 | public class SleuthApplication { 18 | 19 | @Bean 20 | @LoadBalanced 21 | RestTemplate restTemplate(){ 22 | return new RestTemplate(); 23 | } 24 | 25 | @RequestMapping("/") 26 | public String home() { 27 | return "Hello world"; 28 | } 29 | 30 | public static void main(String[] args) { 31 | SpringApplication.run(SleuthApplication.class, args); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | contextConfigLocation 8 | WEB-INF/applicationContext.xml 9 | 10 | 11 | org.springframework.web.context.ContextLoaderListener 12 | 13 | 14 | 15 | com.bage.study.micro.services.eureka.client4.EurekaInitAndRegisterListener 16 | 17 | 18 | dispatcher 19 | org.springframework.web.servlet.DispatcherServlet 20 | 1 21 | 22 | 23 | dispatcher 24 | / 25 | 26 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client5/src/main/java/com/bage/study/micro/services/eureka/client5/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client5; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | @GetMapping("/feign") 30 | public String feign() { 31 | System.out.println("111111"); 32 | return "1"; 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client6/src/main/java/com/bage/study/micro/services/eureka/client6/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client6; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | @GetMapping("/feign") 30 | public String feign() { 31 | System.out.println("111111"); 32 | return "1"; 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/java/com/bage/study/micro/services/hystrix/StoreIntegration.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.hystrix; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | import java.net.URI; 10 | import java.util.Map; 11 | 12 | @Component 13 | public class StoreIntegration { 14 | 15 | @Autowired 16 | RestTemplate restTemplate; 17 | 18 | @HystrixCommand(fallbackMethod = "defaultStores") 19 | public Object getStores(Map parameters) { 20 | //do stuff that might fail 21 | String url = "http://CLOUD-CLIENT/eurekaClient/someUrl"; 22 | if(parameters == null){ 23 | url = "http://CLOUD-CLIENT/eurekaClient/feign"; 24 | } 25 | ResponseEntity res = restTemplate.getForEntity(url , String.class); 26 | return res.getBody(); 27 | } 28 | 29 | public Object defaultStores(Map parameters) { 30 | return "defaultStores"; /* something useful */ 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client6/src/main/java/com/bage/study/micro/services/eureka/client6/Client6Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client6; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestParam; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.concurrent.atomic.AtomicInteger; 13 | 14 | @SpringBootApplication 15 | @EnableDiscoveryClient 16 | @RestController 17 | public class Client6Application { 18 | 19 | 20 | AtomicInteger counter = new AtomicInteger(); 21 | 22 | @RequestMapping("/") 23 | public String home(@RequestParam(value = "param",required = false) String param) { 24 | System.out.println(counter.incrementAndGet() + ":这是client 6 " + param); 25 | return "Hello 6 " + param; 26 | } 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(Client6Application.class, args); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /study-micro-services-turbine/src/main/java/com/bage/study/micro/services/turbine/TurbineApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.turbine; 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.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 8 | import org.springframework.cloud.netflix.turbine.EnableTurbine; 9 | import org.springframework.cloud.openfeign.EnableFeignClients; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | @EnableDiscoveryClient 14 | @EnableFeignClients 15 | @EnableCircuitBreaker 16 | @SpringBootApplication 17 | @EnableTurbine 18 | @RestController 19 | @EnableHystrixDashboard 20 | public class TurbineApplication { 21 | 22 | @RequestMapping("/") 23 | public String home() { 24 | return "Hello world"; 25 | } 26 | 27 | public static void main(String[] args) { 28 | SpringApplication.run(TurbineApplication.class, args); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client1/src/main/java/com/bage/study/micro/services/eureka/client1/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client1; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | @RequestMapping("eurekaClient") 13 | public class EurekaClientController { 14 | 15 | @Autowired 16 | private DiscoveryClient discoveryClient; 17 | 18 | @GetMapping("/clients") 19 | public String client() { 20 | List list = discoveryClient.getServices(); 21 | StringBuilder stringBuilder = new StringBuilder(); 22 | for (int i = 0; i < list.size(); i++) { 23 | stringBuilder.append(list.get(i) + ","); 24 | } 25 | System.out.println(stringBuilder.toString()); 26 | return stringBuilder.toString(); 27 | } 28 | 29 | @GetMapping("/feign") 30 | public String feign() { 31 | System.out.println("111111"); 32 | return "1"; 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /study-micro-services-spring-boot/src/main/java/com/bage/study/micro/services/spring/boot/cache/AppRunner.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.spring.boot.cache; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class AppRunner implements CommandLineRunner { 10 | 11 | private static final Logger logger = LoggerFactory.getLogger(AppRunner.class); 12 | 13 | private final BookRepository bookRepository; 14 | 15 | public AppRunner(BookRepository bookRepository) { 16 | this.bookRepository = bookRepository; 17 | } 18 | 19 | public void run(String... args) throws Exception { 20 | logger.info(".... Fetching books"); 21 | logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); 22 | logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567")); 23 | logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); 24 | logger.info("isbn-4567 -->" + bookRepository.getByIsbn("isbn-4567")); 25 | logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); 26 | logger.info("isbn-1234 -->" + bookRepository.getByIsbn("isbn-1234")); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /study-micro-services-config-client/src/main/java/com/bage/study/micro/services/eureka/register/MessageRestController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.register; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.cloud.context.config.annotation.RefreshScope; 6 | import org.springframework.cloud.context.refresh.ContextRefresher; 7 | import org.springframework.core.env.ConfigurableEnvironment; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RefreshScope 12 | @RestController 13 | public class MessageRestController { 14 | 15 | @Value("${config:Hello bage}") 16 | private String config; 17 | 18 | @Autowired 19 | private ConfigurableEnvironment environment; 20 | 21 | @Autowired 22 | private ContextRefresher refresher; 23 | 24 | @RequestMapping("/environment") 25 | String environment() { 26 | return environment.toString(); 27 | } 28 | 29 | @RequestMapping("/refresher") 30 | String refresher() { 31 | return String.valueOf(refresher.refresh()); 32 | } 33 | 34 | @RequestMapping("/config") 35 | String getConfig() { 36 | return this.config; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: ${appPort:8071} 3 | # tomcat: 4 | # max-threads: 500 5 | # max-connections: 500 6 | 7 | spring: 8 | application: 9 | name: cloud-dynamic-routes 10 | h2: 11 | console: 12 | path: /h2-console # h2 web consloe 路径 13 | enabled: true # 开启 h2 web consloe,默认为 false 14 | settings: 15 | web-allow-others: true #允许远程访问 h2 web consloe 16 | datasource: 17 | username: sa 18 | url: jdbc:h2:~/test;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE 19 | driver-class-name: org.h2.Driver 20 | schema: classpath:schema.sql 21 | data: classpath:data.sql 22 | 23 | cloud: 24 | refresh: 25 | refreshable: none 26 | eureka: 27 | instance: 28 | hostname: localhost 29 | client: 30 | serviceUrl: 31 | defaultZone: http://localhost:8761/eureka/ 32 | 33 | 34 | mybatis: 35 | # Eclipse 配置 36 | # config: 37 | # location: mybatis-config.xml 38 | 39 | # Idea 配置 40 | config-location: classpath:mybatis-config.xml 41 | 42 | 43 | zuul: 44 | host: 45 | connect-timeout-millis: 6000 # HTTP连接超时要比Hystrix的大 46 | socket-timeout-millis: 6000 # socket超时 47 | 48 | semaphore: 49 | max-semaphores: 1000 # could not acquire a semaphore for execution 50 | 51 | ribbon: 52 | ReadTimeout: 5000 53 | ConnectTimeout: 5000 54 | 55 | 56 | -------------------------------------------------------------------------------- /study-micro-services-hystrix/src/main/java/com/bage/study/micro/services/hystrix/HystrixApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.hystrix; 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.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 8 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 9 | import org.springframework.cloud.openfeign.EnableFeignClients; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RestController; 13 | import org.springframework.web.client.RestTemplate; 14 | 15 | @SpringBootApplication 16 | @EnableDiscoveryClient 17 | @RestController 18 | @EnableFeignClients 19 | @EnableCircuitBreaker 20 | @EnableHystrixDashboard 21 | public class HystrixApplication { 22 | 23 | @Bean 24 | @LoadBalanced 25 | RestTemplate restTemplate(){ 26 | return new RestTemplate(); 27 | } 28 | 29 | @RequestMapping("/") 30 | public String home() { 31 | return "Hello world"; 32 | } 33 | 34 | public static void main(String[] args) { 35 | SpringApplication.run(HystrixApplication.class, args); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/README.md: -------------------------------------------------------------------------------- 1 | # study-micro-services-zuul-dynamix-routes # 2 | 3 | ## 参考链接 ## 4 | - 动态路由 Persistent and Fault Tolerant Dynamic Routes Using Zuul, Redis, and REST API [https://dzone.com/articles/persistent-and-fault-tolerant-dynamic-routes-using](https://dzone.com/articles/persistent-and-fault-tolerant-dynamic-routes-using "Persistent and Fault Tolerant Dynamic Routes Using Zuul, Redis, and REST API") 5 | - vikasanandgit/zuul-route-redis [https://github.com/vikasanandgit/zuul-route-redis](https://github.com/vikasanandgit/zuul-route-redis "vikasanandgit/zuul-route-redis") 6 | - spring-cloud-samples/sample-zuul-filters [https://github.com/spring-cloud-samples/sample-zuul-filters/blob/master/src/main/java/org/springframework/cloud/samplezuulfilters/QueryParamServiceIdPreFilter.java](https://github.com/spring-cloud-samples/sample-zuul-filters/blob/master/src/main/java/org/springframework/cloud/samplezuulfilters/QueryParamServiceIdPreFilter.java "spring-cloud-samples/sample-zuul-filters") 7 | - Spring Cloud_24_网关Zuul/路由配置 [https://blog.csdn.net/zhaozao5757/article/details/79635069](https://blog.csdn.net/zhaozao5757/article/details/79635069 "Spring Cloud_24_网关Zuul/路由配置") 8 | - spring cloud-zuul的Filter详解 [https://www.cnblogs.com/a8457013/p/8352349.html](https://www.cnblogs.com/a8457013/p/8352349.html "spring cloud-zuul的Filter详解") 9 | - Spring Cloud Zuul路由动态配置 [https://mp.weixin.qq.com/s/7zIxKJIHrfiJ-JnSTWC0bA](https://mp.weixin.qq.com/s/7zIxKJIHrfiJ-JnSTWC0bA "Spring Cloud Zuul路由动态配置") 10 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/PreRequestLogFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; 8 | 9 | import com.netflix.zuul.ZuulFilter; 10 | import com.netflix.zuul.context.RequestContext; 11 | import com.netflix.zuul.exception.ZuulException; 12 | 13 | import java.util.concurrent.atomic.AtomicInteger; 14 | 15 | public class PreRequestLogFilter extends ZuulFilter { 16 | 17 | private static final Logger logger = LoggerFactory.getLogger(PreRequestLogFilter.class); 18 | AtomicInteger counter = new AtomicInteger(); 19 | 20 | @Override 21 | public Object run() throws ZuulException { 22 | RequestContext context = RequestContext.getCurrentContext(); 23 | HttpServletRequest request = context.getRequest(); 24 | System.out.println(counter.incrementAndGet() + ":PreRequestLogFilter "); 25 | 26 | logger.info(String.format("bage---send %s request to %s", request.getMethod(),request.getRequestURL().toString())); 27 | return null; 28 | } 29 | 30 | @Override 31 | public boolean shouldFilter() { 32 | return true; 33 | } 34 | 35 | @Override 36 | public int filterOrder() { 37 | return FilterConstants.PRE_DECORATION_FILTER_ORDER; 38 | } 39 | 40 | @Override 41 | public String filterType() { 42 | return FilterConstants.PRE_TYPE; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /study-micro-services-gateway/src/main/java/com/bage/study/micro/services/gateway/CustomerGatewayFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.cloud.gateway.filter.GatewayFilter; 6 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; 7 | import org.springframework.core.Ordered; 8 | import org.springframework.web.server.ServerWebExchange; 9 | import reactor.core.publisher.Mono; 10 | 11 | import java.time.Instant; 12 | 13 | /** 14 | * 统计某个或者某种路由的处理时长 15 | */ 16 | public class CustomerGatewayFilter implements GatewayFilter, Ordered { 17 | 18 | private static final Logger log = LoggerFactory.getLogger( CustomerGatewayFilter.class ); 19 | private static final String COUNT_START_TIME = "countStartTime"; 20 | 21 | @Override 22 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 23 | exchange.getAttributes().put(COUNT_START_TIME, Instant.now().toEpochMilli() ); 24 | return chain.filter(exchange).then( 25 | Mono.fromRunnable(() -> { 26 | long startTime = exchange.getAttribute(COUNT_START_TIME); 27 | long endTime=(Instant.now().toEpochMilli() - startTime); 28 | log.info(exchange.getRequest().getURI().getRawPath() + ": " + endTime + "ms"); 29 | }) 30 | ); 31 | } 32 | 33 | @Override 34 | public int getOrder() { 35 | return 0; 36 | } 37 | } -------------------------------------------------------------------------------- /study-micro-services-eureka-client2/src/main/java/com/bage/study/micro/services/eureka/client2/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client2; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.ServiceInstance; 7 | import org.springframework.cloud.client.discovery.DiscoveryClient; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestParam; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | @RestController 14 | @RequestMapping("/eurekaClient") 15 | public class EurekaClientController { 16 | 17 | @Autowired 18 | private DiscoveryClient discoveryClient; 19 | 20 | @GetMapping("/services") 21 | public List client(@RequestParam("serviceId") String serviceId) { 22 | return discoveryClient.getInstances(serviceId); 23 | } 24 | 25 | @GetMapping("/clients") 26 | public String client() { 27 | List list = discoveryClient.getServices(); 28 | StringBuilder stringBuilder = new StringBuilder(); 29 | for (int i = 0; i < list.size(); i++) { 30 | stringBuilder.append(list.get(i) + ","); 31 | } 32 | System.out.println(stringBuilder.toString()); 33 | return stringBuilder.toString(); 34 | } 35 | 36 | @GetMapping("/feign") 37 | public String feign() { 38 | System.out.println("22222"); 39 | return "2"; 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /study-micro-services-multi-datasources/src/main/java/com/bage/study/micro/services/multi/datasources/MultiDataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.multi.datasources; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.boot.jdbc.DataSourceBuilder; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.Primary; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | 11 | import javax.sql.DataSource; 12 | 13 | @Configuration 14 | public class MultiDataSourceConfig { 15 | 16 | @Bean(name = "multidb1") 17 | @Primary 18 | @ConfigurationProperties(prefix = "spring.datasource.db1") 19 | public DataSource multidb1DataSource() { 20 | return DataSourceBuilder.create().build(); 21 | } 22 | 23 | @Bean(name = "multidb2") 24 | @ConfigurationProperties(prefix = "spring.datasource.db2") 25 | public DataSource multidb2DataSource() { 26 | return DataSourceBuilder.create().build(); 27 | } 28 | 29 | @Primary 30 | @Bean(name = "jdbcTemplate1") 31 | public JdbcTemplate jdbcTemplate1(@Qualifier("multidb1") DataSource dataSource) { 32 | return new JdbcTemplate(dataSource); 33 | } 34 | 35 | @Bean(name = "jdbcTemplate2") 36 | public JdbcTemplate jdbcTemplate2(@Qualifier("multidb2") DataSource dataSource) { 37 | return new JdbcTemplate(dataSource); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/QueryParamPortPreFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import com.netflix.zuul.ZuulFilter; 6 | import com.netflix.zuul.context.RequestContext; 7 | import org.springframework.stereotype.Component; 8 | import org.springframework.util.ReflectionUtils; 9 | import org.springframework.web.util.UriComponentsBuilder; 10 | 11 | import java.net.URL; 12 | 13 | import static com.netflix.zuul.context.RequestContext.getCurrentContext; 14 | 15 | /** 16 | * @author Spencer Gibb 17 | */ 18 | @Component 19 | public class QueryParamPortPreFilter extends ZuulFilter { 20 | 21 | public int filterOrder() { 22 | // run after PreDecorationFilter 23 | return 5 + 1; 24 | } 25 | 26 | public String filterType() { 27 | return "pre"; 28 | } 29 | 30 | @Override 31 | public boolean shouldFilter() { 32 | RequestContext ctx = getCurrentContext(); 33 | return ctx.getRequest().getParameter("port") != null; 34 | } 35 | 36 | public Object run() { 37 | RequestContext ctx = getCurrentContext(); 38 | HttpServletRequest request = ctx.getRequest(); 39 | // put the serviceId in `RequestContext` 40 | String port = request.getParameter("port"); 41 | try { 42 | URL url = UriComponentsBuilder.fromUri(ctx.getRouteHost().toURI()) 43 | .port(new Integer(port)) 44 | .build().toUri().toURL(); 45 | ctx.setRouteHost(url); 46 | } catch (Exception e) { 47 | ReflectionUtils.rethrowRuntimeException(e); 48 | } 49 | return null; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/ModifyResponseDataStreamFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.nio.charset.Charset; 7 | 8 | import org.springframework.stereotype.Component; 9 | import org.springframework.util.StreamUtils; 10 | 11 | import com.netflix.zuul.ZuulFilter; 12 | import com.netflix.zuul.context.RequestContext; 13 | 14 | import static com.netflix.zuul.context.RequestContext.getCurrentContext; 15 | import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; 16 | 17 | /** 18 | * @author Spencer Gibb 19 | */ 20 | @Component 21 | public class ModifyResponseDataStreamFilter extends ZuulFilter { 22 | public String filterType() { 23 | return "post"; 24 | } 25 | 26 | public int filterOrder() { 27 | return 999; 28 | } 29 | 30 | public boolean shouldFilter() { 31 | RequestContext context = getCurrentContext(); 32 | return context.getRequest().getParameter("dao") != null; 33 | } 34 | 35 | public Object run() { 36 | try { 37 | RequestContext context = getCurrentContext(); 38 | InputStream stream = context.getResponseDataStream(); 39 | String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); 40 | body = "Modified via setResponseDataStream(): " + body; 41 | context.setResponseDataStream(new ByteArrayInputStream(body.getBytes("UTF-8"))); 42 | } 43 | catch (IOException e) { 44 | rethrowRuntimeException(e); 45 | } 46 | return null; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/aop/UserAspect.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes.aop; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.*; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * 参考 9 | * @author luruihua 10 | * 11 | */ 12 | @Aspect 13 | @Component 14 | public class UserAspect { 15 | 16 | @Pointcut("execution(* com.bage.study.micro.services.organization.user.controller..*.*(..))") 17 | public void pointcutControllerMethods(){ 18 | 19 | } 20 | 21 | @Before("pointcutControllerMethods()") 22 | public void before(){ 23 | System.out.println("before() is work"); 24 | } 25 | 26 | @After("pointcutControllerMethods()") 27 | public void after(){ 28 | System.out.println("after() is work"); 29 | } 30 | 31 | @Around("pointcutControllerMethods()") 32 | public Object around(ProceedingJoinPoint pjp) throws Throwable { 33 | System.out.println("around() is work -- 1"); 34 | Object retVal = pjp.proceed(); 35 | // stop stopwatch 36 | // Logger.log("com.bage.advice.Example.doBasicProfilingAround(ProceedingJoinPoint pjp) stop stopwatch is work"); 37 | System.out.println("around() is work -- 2"); 38 | 39 | return retVal; 40 | } 41 | 42 | @AfterThrowing("pointcutControllerMethods()") 43 | public void afterThrowing(){ 44 | System.out.println("afterThrowing() is work"); 45 | } 46 | 47 | @AfterReturning("pointcutControllerMethods()") 48 | public void afterReturning(){ 49 | System.out.println("afterReturning() is work"); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/service/RouteServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes.service; 2 | 3 | import com.bage.study.micro.services.zuul.dynamix.routes.dao.RouteMapper; 4 | import com.bage.study.micro.services.zuul.dynamix.routes.domain.Route; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | @Service 12 | public class RouteServiceImpl implements RouteService{ 13 | 14 | @Autowired 15 | RouteMapper routeMapper; 16 | 17 | List cacheList = new ArrayList<>(); 18 | 19 | @Override 20 | public int insert(Route route) { 21 | return routeMapper.insert(route); 22 | } 23 | 24 | @Override 25 | public int delete(String id) { 26 | return routeMapper.delete(id); 27 | } 28 | 29 | @Override 30 | public int update(Route route) { 31 | return routeMapper.update(route); 32 | } 33 | 34 | @Override 35 | public Route query(String id) { 36 | return null; 37 | } 38 | 39 | @Override 40 | public List queryAll() { 41 | return routeMapper.queryAll(); 42 | } 43 | 44 | @Override 45 | public List queryAllFromCache() { 46 | if(cacheList.isEmpty()){ 47 | cacheList = queryAll(); 48 | } 49 | return cacheList; 50 | } 51 | 52 | @Override 53 | public List refresh() { 54 | cacheList = routeMapper.queryAll(); 55 | return cacheList; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/ModifyResponseBodyFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.nio.charset.Charset; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | import org.springframework.stereotype.Component; 9 | import org.springframework.util.StreamUtils; 10 | 11 | import com.netflix.zuul.ZuulFilter; 12 | import com.netflix.zuul.context.RequestContext; 13 | 14 | import static com.netflix.zuul.context.RequestContext.getCurrentContext; 15 | import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; 16 | 17 | /** 18 | * @author Spencer Gibb 19 | */ 20 | @Component 21 | public class ModifyResponseBodyFilter extends ZuulFilter { 22 | 23 | AtomicInteger counter = new AtomicInteger(); 24 | 25 | public String filterType() { 26 | return "post"; 27 | } 28 | 29 | public int filterOrder() { 30 | return 999; 31 | } 32 | 33 | public boolean shouldFilter() { 34 | RequestContext context = getCurrentContext(); 35 | return context.getRequest().getParameter("dao") == null; 36 | } 37 | 38 | public Object run() { 39 | try { 40 | System.out.println(counter.incrementAndGet() + ":ModifyResponseBodyFilter "); 41 | RequestContext context = getCurrentContext(); 42 | InputStream stream = context.getResponseDataStream(); 43 | String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); 44 | context.setResponseBody("Modified via setResponseBody(): "+body); 45 | } 46 | catch (IOException e) { 47 | rethrowRuntimeException(e); 48 | } 49 | return null; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client5/src/main/java/com/bage/study/micro/services/eureka/client5/Client5Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client5; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.concurrent.atomic.AtomicInteger; 13 | 14 | @SpringBootApplication 15 | @EnableDiscoveryClient 16 | @RestController 17 | public class Client5Application { 18 | 19 | AtomicInteger counter = new AtomicInteger(); 20 | 21 | @RequestMapping("/") 22 | public String home(HttpServletRequest request) { 23 | System.out.println(counter.incrementAndGet() + ":这是client 5 "); 24 | return "Hello 5 " + request.getContextPath(); 25 | } 26 | 27 | @RequestMapping("/client5") 28 | public String hoclient5me(HttpServletRequest request) { 29 | System.out.println("这是client 5 "); 30 | return "client5 5 " + request.getContextPath(); 31 | } 32 | 33 | 34 | @GetMapping("/timeoutRequest") 35 | public String timeoutRequest() { 36 | try { 37 | Thread.sleep(10000); 38 | } catch (InterruptedException e) { 39 | e.printStackTrace(); 40 | } 41 | return "1"; 42 | } 43 | public static void main(String[] args) { 44 | SpringApplication.run(Client5Application.class, args); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/UppercaseRequestEntityFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.nio.charset.Charset; 7 | 8 | import com.netflix.zuul.ZuulFilter; 9 | import org.springframework.stereotype.Component; 10 | import org.springframework.util.StreamUtils; 11 | 12 | import com.netflix.zuul.context.RequestContext; 13 | 14 | import static com.netflix.zuul.context.RequestContext.getCurrentContext; 15 | import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; 16 | 17 | /** 18 | * @author Spencer Gibb 19 | */ 20 | @Component 21 | public class UppercaseRequestEntityFilter extends ZuulFilter { 22 | public String filterType() { 23 | return "pre"; 24 | } 25 | 26 | public int filterOrder() { 27 | return 6; 28 | } 29 | 30 | public boolean shouldFilter() { 31 | RequestContext context = getCurrentContext(); 32 | return context.getRequest().getParameter("dao") == null; 33 | } 34 | 35 | public Object run() { 36 | try { 37 | RequestContext context = getCurrentContext(); 38 | InputStream in = (InputStream) context.get("requestEntity"); 39 | if (in == null) { 40 | in = context.getRequest().getInputStream(); 41 | } 42 | String body = StreamUtils.copyToString(in, Charset.forName("UTF-8")); 43 | // body = "request body modified via set('requestEntity'): "+ body; 44 | body = body.toUpperCase(); 45 | context.set("requestEntity", new ByteArrayInputStream(body.getBytes("UTF-8"))); 46 | } 47 | catch (IOException e) { 48 | rethrowRuntimeException(e); 49 | } 50 | return null; 51 | } 52 | } -------------------------------------------------------------------------------- /study-micro-services-ribbon/src/main/java/com/bage/study/micro/services/ribbon/EurekaClientController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.ribbon; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.client.RestTemplate; 13 | 14 | @RestController 15 | @RequestMapping("/eurekaClient") 16 | public class EurekaClientController { 17 | 18 | @Autowired 19 | private DiscoveryClient discoveryClient; 20 | 21 | @Autowired 22 | private RestTemplate restTemplate; 23 | 24 | 25 | @GetMapping("/clients/{clientName}") 26 | public String doSomeThing(@PathVariable("clientName") String clientName) { 27 | 28 | List list = discoveryClient.getServices(); 29 | StringBuilder stringBuilder = new StringBuilder(); 30 | for (int i = 0; i < list.size(); i++) { 31 | stringBuilder.append(list.get(i) + ","); 32 | } 33 | System.out.println(stringBuilder.toString()); 34 | 35 | String url = "http://" + clientName; 36 | ResponseEntity res = restTemplate.getForEntity(url , String.class); 37 | return res.getBody(); 38 | } 39 | 40 | @GetMapping("/not-springboot/clients/") 41 | public String notboot() { 42 | String url = "http://cloud-client-not-springboot/hello"; 43 | ResponseEntity res = restTemplate.getForEntity(url , String.class); 44 | return res.getBody(); 45 | } 46 | 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /study-micro-services-stream/src/main/java/com/bage/study/micro/services/stream/StreamApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.stream; 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.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 8 | import org.springframework.cloud.netflix.turbine.EnableTurbine; 9 | import org.springframework.cloud.openfeign.EnableFeignClients; 10 | import org.springframework.cloud.stream.annotation.EnableBinding; 11 | import org.springframework.cloud.stream.annotation.StreamListener; 12 | import org.springframework.cloud.stream.messaging.Sink; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | @EnableDiscoveryClient 17 | @SpringBootApplication 18 | @RestController 19 | @EnableBinding(Sink.class) 20 | public class StreamApplication { 21 | 22 | @RequestMapping("/") 23 | public String home() { 24 | return "Hello world"; 25 | } 26 | 27 | 28 | public static void main(String[] args) { 29 | SpringApplication.run(StreamApplication.class, args); 30 | } 31 | 32 | @StreamListener(Sink.INPUT) 33 | public void handle(Person person) { 34 | System.out.println("Received: " + person); 35 | } 36 | 37 | public static class Person { 38 | private String name; 39 | public String getName() { 40 | return name; 41 | } 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | public String toString() { 46 | return this.name; 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/controller/RouteController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes.controller; 2 | 3 | import com.bage.study.micro.services.zuul.dynamix.routes.domain.Route; 4 | import com.bage.study.micro.services.zuul.dynamix.routes.service.RouteService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; 7 | import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @RestController 13 | @RequestMapping("/route") 14 | public class RouteController { 15 | 16 | @Autowired 17 | RouteService routeService; 18 | Route route = new Route("10086","/client7/*","cloud-client7"); 19 | 20 | @RequestMapping("/insert") 21 | public Object insert(){ 22 | return routeService.insert(route); 23 | } 24 | 25 | @RequestMapping("/delete/{id}") 26 | public Object delete(@PathVariable("id") String id){ 27 | return routeService.delete(id); 28 | } 29 | 30 | @RequestMapping("/update") 31 | public Object update(){ 32 | route.setServiceId("cloud-service7-new"); 33 | return routeService.update(route); 34 | } 35 | 36 | @RequestMapping("/query/{id}") 37 | public Object query(@PathVariable("id") String id){ 38 | return routeService.query(id); 39 | } 40 | 41 | @RequestMapping("/query/all") 42 | public Object queryAll(){ 43 | return routeService.queryAll(); 44 | } 45 | 46 | @RequestMapping("/refresh") 47 | public Object refresh(){ 48 | return routeService.refresh(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /study-micro-services-eureka/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.bage 8 | study-micro-services 9 | 0.0.1-SNAPSHOT 10 | 11 | study-micro-services-eureka 12 | study-micro-services-eureka 13 | http://maven.apache.org 14 | 15 | 16 | UTF-8 17 | 2.0.1.RELEASE 18 | 3.4.6 19 | 1.3.2 20 | 1.8 21 | 1.8 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-dependencies 35 | ${spring.boot.base.version} 36 | pom 37 | import 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-dependencies 42 | Finchley.SR2 43 | pom 44 | import 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /study-micro-services-gateway/src/main/java/com/bage/study/micro/services/gateway/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway; 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.gateway.route.RouteLocator; 7 | import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @EnableDiscoveryClient 13 | @SpringBootApplication 14 | @RestController 15 | public class GatewayApplication { 16 | 17 | @RequestMapping("/") 18 | public String home() { 19 | return "Hello Spring Gateway"; 20 | } 21 | 22 | public static void main(String[] args) { 23 | SpringApplication.run(GatewayApplication.class, args); 24 | } 25 | 26 | @Bean 27 | public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) { 28 | return builder.routes() 29 | .route(r -> r.path("/customer/**") 30 | .filters(f -> f.filter(new CustomerGatewayFilter()) 31 | .addResponseHeader("X-Response-test", "test")) 32 | .uri("http://httpbin.org:80/get") 33 | .id("customer_filter_router") 34 | ) 35 | .route(r -> r.path("/throttle/**") 36 | .filters(f -> f.filter(new ThrottleGatewayFilter()) 37 | .addResponseHeader("X-throttle-test", "throttle")) 38 | .uri("http://localhost:8888/eurekaClient/clients") 39 | .id("throttle") 40 | ) 41 | .build(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /study-micro-services-eureka-client4/src/main/java/com/bage/study/micro/services/eureka/client4/MyInstanceConfig.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.eureka.client4; 2 | 3 | import com.netflix.appinfo.MyDataCenterInstanceConfig; 4 | 5 | import javax.management.MBeanServer; 6 | import javax.management.ObjectName; 7 | import javax.management.Query; 8 | import java.lang.management.ManagementFactory; 9 | import java.net.InetAddress; 10 | import java.net.UnknownHostException; 11 | import java.util.Set; 12 | 13 | /** 14 | * 15 | */ 16 | public class MyInstanceConfig extends MyDataCenterInstanceConfig { 17 | 18 | private String serviceId = ""; 19 | 20 | public MyInstanceConfig(String serviceId){ 21 | this.serviceId = serviceId; 22 | } 23 | 24 | @Override 25 | public String getInstanceId() { 26 | return this.getHostName(false) + ":" + serviceId + ":" + getNonSecurePort() ; 27 | } 28 | 29 | /** 30 | * 优先使用ip 替换 hostname 31 | * @param refresh 32 | * @return 33 | */ 34 | @Override 35 | public String getHostName(boolean refresh) { 36 | try { 37 | return InetAddress.getLocalHost().getHostAddress(); 38 | } catch (UnknownHostException e) { 39 | return super.getHostName(refresh); 40 | } 41 | } 42 | 43 | 44 | /*** 45 | *获取本机启动中tomcat端口号 46 | * @return 47 | */ 48 | @Override 49 | public int getNonSecurePort(){ 50 | int tomcatPort; 51 | try { 52 | MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); 53 | Set objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), 54 | Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))); 55 | 56 | tomcatPort = Integer.valueOf(objectNames.iterator().next().getKeyProperty("port")); 57 | }catch (Exception e){ 58 | return super.getNonSecurePort(); 59 | } 60 | return tomcatPort; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/user/aop/UserAspect.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization.user.aop; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.After; 5 | import org.aspectj.lang.annotation.AfterReturning; 6 | import org.aspectj.lang.annotation.AfterThrowing; 7 | import org.aspectj.lang.annotation.Around; 8 | import org.aspectj.lang.annotation.Aspect; 9 | import org.aspectj.lang.annotation.Before; 10 | import org.aspectj.lang.annotation.Pointcut; 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * 参考 15 | * https://github.com/bage2014/spring-study/blob/master/spring-study/spring-study-aop/src/main/java/com/bage/advice/Example.java 16 | * @author luruihua 17 | * 18 | */ 19 | @Aspect 20 | @Component 21 | public class UserAspect { 22 | 23 | @Pointcut("execution(* com.bage.study.micro.services.organization.user.controller..*.*(..))") 24 | public void pointcutControllerMethods(){ 25 | 26 | } 27 | 28 | @Before("pointcutControllerMethods()") 29 | public void before(){ 30 | System.out.println("before() is work"); 31 | } 32 | 33 | @After("pointcutControllerMethods()") 34 | public void after(){ 35 | System.out.println("after() is work"); 36 | } 37 | 38 | @Around("pointcutControllerMethods()") 39 | public Object around(ProceedingJoinPoint pjp) throws Throwable { 40 | System.out.println("around() is work -- 1"); 41 | Object retVal = pjp.proceed(); 42 | // stop stopwatch 43 | // Logger.log("com.bage.advice.Example.doBasicProfilingAround(ProceedingJoinPoint pjp) stop stopwatch is work"); 44 | System.out.println("around() is work -- 2"); 45 | 46 | return retVal; 47 | } 48 | 49 | @AfterThrowing("pointcutControllerMethods()") 50 | public void afterThrowing(){ 51 | System.out.println("afterThrowing() is work"); 52 | } 53 | 54 | @AfterReturning("pointcutControllerMethods()") 55 | public void afterReturning(){ 56 | System.out.println("afterReturning() is work"); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/service/RouteServiceImp.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.service; 2 | 3 | import com.bage.study.micro.services.gateway.route.dao.MyRouteDefinitionRepository; 4 | import com.bage.study.micro.services.gateway.route.domain.Route; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cloud.gateway.event.RefreshRoutesEvent; 7 | import org.springframework.context.ApplicationEventPublisher; 8 | import org.springframework.context.ApplicationEventPublisherAware; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.util.List; 12 | 13 | @Service 14 | public class RouteServiceImp implements RouteService, ApplicationEventPublisherAware{ 15 | 16 | private ApplicationEventPublisher publisher; 17 | 18 | @Autowired 19 | private MyRouteDefinitionRepository routeDefinitionRepository; 20 | 21 | @Override 22 | public void refresh() { 23 | this.publisher.publishEvent(new RefreshRoutesEvent(this)); 24 | } 25 | 26 | @Override 27 | public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { 28 | this.publisher = applicationEventPublisher; 29 | } 30 | 31 | @Override 32 | public int insert(Route route) { 33 | int result = routeDefinitionRepository.insert(route); 34 | // refresh(); 35 | return result; 36 | } 37 | 38 | @Override 39 | public int delete(String id) { 40 | int result = routeDefinitionRepository.delete(id); 41 | // refresh(); 42 | return result; 43 | } 44 | 45 | @Override 46 | public int update(Route route) { 47 | int result = routeDefinitionRepository.update(route); 48 | // refresh(); 49 | return result; 50 | } 51 | 52 | @Override 53 | public int query(String id) { 54 | return routeDefinitionRepository.query(id); 55 | } 56 | 57 | @Override 58 | public List queryAll() { 59 | return routeDefinitionRepository.queryAll(); 60 | } 61 | } -------------------------------------------------------------------------------- /.gitignore.md: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Example user template template 3 | ### Example user template 4 | 5 | # IntelliJ project files 6 | .idea 7 | *.iml 8 | out 9 | gen### Java template 10 | # Compiled class file 11 | *.class 12 | 13 | # Log file 14 | *.log 15 | 16 | # BlueJ files 17 | *.ctxt 18 | 19 | # Mobile Tools for Java (J2ME) 20 | .mtj.tmp/ 21 | 22 | # Package Files # 23 | *.jar 24 | *.war 25 | *.nar 26 | *.ear 27 | *.zip 28 | *.tar.gz 29 | *.rar 30 | 31 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 32 | hs_err_pid* 33 | ### Maven template 34 | target/ 35 | pom.xml.tag 36 | pom.xml.releaseBackup 37 | pom.xml.versionsBackup 38 | pom.xml.next 39 | release.properties 40 | dependency-reduced-pom.xml 41 | buildNumber.properties 42 | .mvn/timing.properties 43 | 44 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) 45 | !/.mvn/wrapper/maven-wrapper.jar 46 | study-micro-services-eureka-client1/study-micro-services-eureka-client1.iml 47 | study-micro-services-eureka-client1/target/ 48 | study-micro-services-eureka-client2/study-micro-services-eureka-client2.iml 49 | study-micro-services-eureka-client2/target/ 50 | study-micro-services-eureka/study-micro-services-eureka.iml 51 | study-micro-services-eureka/target/ 52 | study-micro-services-h2/study-micro-services-h2.iml 53 | study-micro-services-h2/target/ 54 | study-micro-services-multi-datasources/study-micro-services-multi-datasources.iml 55 | study-micro-services-organization/study-micro-services-organization.iml 56 | study-micro-services-organization/target/ 57 | study-micro-services-ribbon/study-micro-s 58 | ervices-ribbon.iml 59 | study-micro-services-ribbon/target/ 60 | study-micro-services-spring-boot/study-micro-services-spring-boot.iml 61 | study-micro-services-spring-boot/target/ 62 | study-micro-services-spring-cloud-netflix/src/main/resources/ 63 | study-micro-services-spring-cloud-netflix/study-micro-services-spring-cloud-netflix.iml 64 | study-micro-services-zuul/src/test/ 65 | study-micro-services-zuul/study-micro-services-zuul.iml 66 | study-micro-services-zuul/target/ 67 | .idea 68 | -------------------------------------------------------------------------------- /study-micro-services-gateway/src/test/java/com/bage/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.bage; 2 | 3 | import static com.github.tomakehurst.wiremock.client.WireMock.*; 4 | import static org.junit.Assert.assertThat; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | import org.springframework.test.web.reactive.server.WebTestClient; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 17 | properties = {"httpbin=http://localhost:${wiremock.server.port}"}) 18 | @AutoConfigureWireMock(port = 0) 19 | public class AppTest { 20 | 21 | @Autowired 22 | private WebTestClient webClient; 23 | 24 | @Test 25 | public void contextLoads() throws Exception { 26 | //Stubs 27 | stubFor(get(urlEqualTo("/get")) 28 | .willReturn(aResponse() 29 | .withBody("{\"headers\":{\"Hello\":\"World\"}}") 30 | .withHeader("Content-Type", "application/json"))); 31 | stubFor(get(urlEqualTo("/delay/3")) 32 | .willReturn(aResponse() 33 | .withBody("no fallback") 34 | .withFixedDelay(3000))); 35 | 36 | webClient 37 | .get().uri("/get") 38 | .exchange() 39 | .expectStatus().isOk() 40 | .expectBody() 41 | .jsonPath("$.headers.Hello").isEqualTo("World"); 42 | /* 43 | webClient 44 | .get().uri("/delay/3") 45 | .header("Host", "www.hystrix.com") 46 | .exchange() 47 | .expectStatus().isOk() 48 | .expectBody() 49 | .consumeWith( 50 | response -> assertThat(response.getResponseBody()).isEqualTo("fallback".getBytes())); 51 | */ 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /study-micro-services-eureka/README.md: -------------------------------------------------------------------------------- 1 | # study-micro-services-eureka # 2 | 3 | ## 参考链接 ## 4 | - Spring Cloud集成Eureka [https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.M3/single/spring-cloud-netflix.html](https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.M3/single/spring-cloud-netflix.html "Spring Cloud 集成 Eureka") 5 | - [https://github.com/Netflix/eureka/wiki](https://github.com/Netflix/eureka/wiki "") 6 | - 基于Eureka的服务治理 [https://www.cnblogs.com/demodashi/p/8509931.html](https://www.cnblogs.com/demodashi/p/8509931.html "基于Eureka的服务治理") 7 | - SpringCloud学习1-服务注册与发现(Eureka) [https://www.cnblogs.com/woshimrf/p/springclout-eureka.html](https://www.cnblogs.com/woshimrf/p/springclout-eureka.html "SpringCloud学习1-服务注册与发现(Eureka)") 8 | - EureKa:服务注册与发现 [https://blog.csdn.net/ycd500756/article/details/80955485](https://blog.csdn.net/ycd500756/article/details/80955485 "EureKa:服务注册与发现") 9 | - Eureka 2.0 开源工作宣告停止,继续使用风险自负 [https://www.oschina.net/news/97521/eureka-2-0-discontinued](https://www.oschina.net/news/97521/eureka-2-0-discontinued "Eureka 2.0 开源工作宣告停止,继续使用风险自负") 10 | - Eureka的工作原理以及它与ZooKeeper的区别 [https://www.cnblogs.com/snowjeblog/p/8821325.html](https://www.cnblogs.com/snowjeblog/p/8821325.html "Eureka的工作原理以及它与ZooKeeper的区别") 11 | 12 | ## 项目模块说明 ## 13 | - study-micro-services-eureka spring cloud与eureka的server服务 14 | - study-micro-services-eureka-server2 server服务2,与cloud-server3相互注册,服务名称为:cloud-server2 15 | - study-micro-services-eureka-server3 server服务3,与cloud-server2相互注册,服务名称为:cloud-server3 16 | 17 | ## 启动顺序 ## 18 | - 启动注册中心 study-micro-services-eureka 19 | - 启动注册中心2 study-micro-services-eureka-server2 20 | - 启动注册中心3 study-micro-services-eureka-server3 21 | - 其他项目,随机启动即可 22 | 23 | ## 项目请求路径 ## 24 | #### 注册中心 #### 25 | - 端口 8761 [http://localhost:8761/](http://localhost:8761/ "注册中心") 26 | #### 注册中心群2 #### 27 | - 端口 8762 [http://localhost:8762/](http://localhost:8762/ "注册中心") 28 | #### 注册中心群3 #### 29 | - 端口 8763 [http://localhost:8763/](http://localhost:8763/ "注册中心") 30 | 31 | 32 | ## 整体架构 ## 33 | 34 | ## 服务注册过程 ## 35 | 36 | ## 服务续约过程 ## 37 | 38 | ## 服务下线过程 ## 39 | 40 | ## 服务剔除过程 ## 41 | 42 | ## 服务保护模式 ## 43 | 44 | ## 服务同步过程 ## 45 | 46 | ## 客户端服务拉群过程 ## 47 | 48 | ## 客户端优雅关闭服务 ## 49 | 50 | 51 | -------------------------------------------------------------------------------- /study-micro-services-distribute-transaction/README - lock.md: -------------------------------------------------------------------------------- 1 | # study-micro-services-distribute-lock # 2 | 3 | 可以保证在分布式部署的应用集群中,同一个方法在同一时间只能被一台机器-上的一个线程执行。 4 | 5 | 这把锁要是一把可重入锁(避免死锁) 6 | 7 | 这把锁最好是一把阻塞锁(根据业务需求考虑要不要这条) 8 | 9 | 这把锁最好是一把公平锁(根据业务需求考虑要不要这条) 10 | 11 | 有高可用的获取锁和释放锁功能 12 | 13 | 获取锁和释放锁的性能要好 14 | 15 | 16 | ## 参考链接 ## 17 | - 分布式锁的几种实现方式 [https://www.cnblogs.com/austinspark-jessylu/p/8043726.html](https://www.cnblogs.com/austinspark-jessylu/p/8043726.html) 18 | - 分布式锁简单入门以及三种实现方式介绍 [https://blog.csdn.net/xlgen157387/article/details/79036337](https://blog.csdn.net/xlgen157387/article/details/79036337) 19 | 20 | ## 实现方式 ## 21 | 22 | ### 基于数据库实现分布式锁 ### 23 | 要实现分布式锁,最简单的方式可能就是直接创建一张锁表,然后通过操作该表中的数据来实现了。 24 | 当我们要锁住某个方法或资源时,我们就在该表中增加一条记录,想要释放锁的时候就删除这条记录。 25 | 26 | **存在的问题** 27 | 28 | - 这把锁强依赖数据库的可用性,数据库是一个单点,一旦数据库挂掉,会导致业务系统不可用。 29 | - 这把锁没有失效时间,一旦解锁操作失败,就会导致锁记录一直在数据库中,其他线程无法再获得到锁。 30 | - 这把锁只能是非阻塞的,因为数据的insert操作,一旦插入失败就会直接报错。没有获得锁的线程并不会进入排队队列,要想再次获得锁就要再次触发获得锁操作。 31 | - 这把锁是非重入的,同一个线程在没有释放锁之前无法再次获得该锁。因为数据中数据已经存在了。 32 | - 操作数据库需要一定的开销,性能问题需要考虑。 33 | 34 | **处理思路** 35 | 36 | - 数据库是单点?搞两个数据库,数据之前双向同步。一旦挂掉快速切换到备库上。 37 | - 没有失效时间?只要做一个定时任务,每隔一定时间把数据库中的超时数据清理一遍。 38 | - 非阻塞的?搞一个while循环,直到insert成功再返回成功。 39 | - 非重入的?在数据库表中加个字段,记录当前获得锁的机器的主机信息和线程信息,那么下次再获取锁的时候先查询数据库,如果当前机器的主机信息和线程信息在数据库可以查到的话,直接把锁分配给他就可以了。 40 | 41 | ## 基于缓存实现分布式锁 ## 42 | 43 | (1)获取锁的时候,使用setnx加锁,并使用expire命令为锁添加一个超时时间,超过该时间则自动释放锁,锁的value值为一个随机生成的UUID,通过此在释放锁的时候进行判断。 44 | 45 | (2)获取锁的时候还设置一个获取的超时时间,若超过这个时间则放弃获取锁。 46 | 47 | (3)释放锁的时候,通过UUID判断是不是该锁,若是该锁,则执行delete进行锁释放。 48 | 49 | 50 | ## 基于ZooKeeper的实现方式 ## 51 | 52 | 基于zookeeper临时有序节点可以实现的分布式锁。 53 | 54 | 大致思想即为:每个客户端对某个方法加锁时,在zookeeper上的与该方法对应的指定节点的目录下,生成一个唯一的瞬时有序节点。 判断是否获取锁的方式很简单,只需要判断有序节点中序号最小的一个。 当释放锁的时候,只需将这个瞬时节点删除即可。同时,其可以避免服务宕机导致的锁无法释放,而产生的死锁问题。 55 | 56 | - 锁无法释放?使用Zookeeper可以有效的解决锁无法释放的问题,因为在创建锁的时候,客户端会在ZK中创建一个临时节点,一旦客户端获取到锁之后突然挂掉(Session连接断开),那么这个临时节点就会自动删除掉。其他客户端就可以再次获得锁。 57 | - 非阻塞锁?使用Zookeeper可以实现阻塞的锁,客户端可以通过在ZK中创建顺序节点,并且在节点上绑定监听器,一旦节点有变化,Zookeeper会通知客户端,客户端可以检查自己创建的节点是不是当前所有节点中序号最小的,如果是,那么自己就获取到锁,便可以执行业务逻辑了。 58 | - 不可重入?使用Zookeeper也可以有效的解决不可重入的问题,客户端在创建节点的时候,把当前客户端的主机信息和线程信息直接写入到节点中,下次想要获取锁的时候和当前最小的节点中的数据比对一下就可以了。如果和自己的信息一样,那么自己直接获取到锁,如果不一样就再创建一个临时的顺序节点,参与排队。 59 | - 单点问题?使用Zookeeper可以有效的解决单点问题,ZK是集群部署的,只要集群中有半数以上的机器存活,就可以对外提供服务。 -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/filters/PrefixRequestEntityFilter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.filters; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.nio.charset.Charset; 6 | 7 | import javax.servlet.ServletInputStream; 8 | import javax.servlet.http.HttpServletRequestWrapper; 9 | 10 | import org.springframework.stereotype.Component; 11 | import org.springframework.util.StreamUtils; 12 | 13 | import com.netflix.zuul.ZuulFilter; 14 | import com.netflix.zuul.context.RequestContext; 15 | import com.netflix.zuul.http.ServletInputStreamWrapper; 16 | 17 | import static com.netflix.zuul.context.RequestContext.getCurrentContext; 18 | import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; 19 | 20 | /** 21 | * @author Spencer Gibb 22 | */ 23 | @Component 24 | public class PrefixRequestEntityFilter extends ZuulFilter { 25 | public String filterType() { 26 | return "pre"; 27 | } 28 | 29 | public int filterOrder() { 30 | return 6; 31 | } 32 | 33 | public boolean shouldFilter() { 34 | RequestContext context = getCurrentContext(); 35 | return context.getRequest().getParameter("dao") != null; 36 | } 37 | 38 | public Object run() { 39 | try { 40 | RequestContext context = getCurrentContext(); 41 | InputStream in = (InputStream) context.get("requestEntity"); 42 | if (in == null) { 43 | in = context.getRequest().getInputStream(); 44 | } 45 | String body = StreamUtils.copyToString(in, Charset.forName("UTF-8")); 46 | body = "request body modified via request wrapper: "+ body; 47 | byte[] bytes = body.getBytes("UTF-8"); 48 | context.setRequest(new HttpServletRequestWrapper(getCurrentContext().getRequest()) { 49 | @Override 50 | public ServletInputStream getInputStream() throws IOException { 51 | return new ServletInputStreamWrapper(bytes); 52 | } 53 | 54 | @Override 55 | public int getContentLength() { 56 | return bytes.length; 57 | } 58 | 59 | @Override 60 | public long getContentLengthLong() { 61 | return bytes.length; 62 | } 63 | }); 64 | } 65 | catch (IOException e) { 66 | rethrowRuntimeException(e); 67 | } 68 | return null; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/domain/Filter.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.domain; 2 | 3 | import org.springframework.cloud.gateway.support.NameUtils; 4 | import org.springframework.util.StringUtils; 5 | 6 | import java.util.LinkedHashMap; 7 | import java.util.Map; 8 | import java.util.Objects; 9 | 10 | public class Filter { 11 | private String name; 12 | 13 | private Map args = new LinkedHashMap(); 14 | 15 | public Filter() { 16 | } 17 | 18 | public Filter(String text) { 19 | int eqIdx = text.indexOf(61); 20 | if (eqIdx <= 0) { 21 | this.setName(text); 22 | } else { 23 | this.setName(text.substring(0, eqIdx)); 24 | String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ","); 25 | 26 | for(int i = 0; i < args.length; ++i) { 27 | this.args.put(NameUtils.generateName(i), args[i]); 28 | } 29 | 30 | } 31 | } 32 | 33 | public String getName() { 34 | return this.name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public Map getArgs() { 42 | return this.args; 43 | } 44 | 45 | public void setArgs(Map args) { 46 | this.args = args; 47 | } 48 | 49 | public void addArg(String key, String value) { 50 | this.args.put(key, value); 51 | } 52 | 53 | public boolean equals(Object o) { 54 | if (this == o) { 55 | return true; 56 | } else if (o != null && this.getClass() == o.getClass()) { 57 | Filter that = (Filter)o; 58 | return Objects.equals(this.name, that.name) && Objects.equals(this.args, that.args); 59 | } else { 60 | return false; 61 | } 62 | } 63 | 64 | public int hashCode() { 65 | return Objects.hash(new Object[]{this.name, this.args}); 66 | } 67 | 68 | public String toString() { 69 | StringBuilder sb = new StringBuilder("Filter{"); 70 | sb.append("name='").append(this.name).append('\''); 71 | sb.append(", args=").append(this.args); 72 | sb.append('}'); 73 | return sb.toString(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /study-micro-services-h2/src/main/java/com/bage/study/micro/services/h2/demo/mybatis/SampleMybatisApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.h2.demo.mybatis; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.stream.Collectors; 14 | 15 | @SpringBootApplication 16 | public class SampleMybatisApplication implements CommandLineRunner { 17 | 18 | private static final Logger log = LoggerFactory.getLogger(SampleMybatisApplication.class); 19 | 20 | private final CustomerMapper customerMapper; 21 | 22 | public SampleMybatisApplication(CustomerMapper customerMapper) { 23 | this.customerMapper = customerMapper; 24 | } 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(SampleMybatisApplication.class, args); 28 | } 29 | 30 | @Autowired 31 | JdbcTemplate jdbcTemplate; 32 | 33 | @Override 34 | public void run(String... strings) throws Exception { 35 | 36 | log.info("Creating tables"); 37 | 38 | jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); 39 | jdbcTemplate.execute("CREATE TABLE customers(" + 40 | "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); 41 | 42 | // Split up the array of whole names into an array of first/last names 43 | List splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() 44 | .map(name -> name.split(" ")) 45 | .collect(Collectors.toList()); 46 | 47 | // Use a Java 8 stream to print out each tuple of the list 48 | splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); 49 | 50 | // Uses JdbcTemplate's batchUpdate operation to bulk load data 51 | jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); 52 | 53 | log.info("--------查询数据-------"); 54 | System.out.println(this.customerMapper.queryById("1")); 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /study-micro-services-gateway/src/main/java/com/bage/study/micro/services/gateway/ThrottleGatewayFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.bage.study.micro.services.gateway; 18 | 19 | import java.util.Objects; 20 | import java.util.concurrent.TimeUnit; 21 | 22 | import org.apache.commons.logging.Log; 23 | import org.apache.commons.logging.LogFactory; 24 | import reactor.core.publisher.Mono; 25 | 26 | import org.springframework.cloud.gateway.filter.GatewayFilter; 27 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; 28 | import org.springframework.http.HttpStatus; 29 | import org.springframework.web.server.ServerWebExchange; 30 | 31 | /** 32 | * Sample throttling filter. See https://github.com/bbeck/token-bucket 33 | */ 34 | public class ThrottleGatewayFilter implements GatewayFilter { 35 | 36 | private static final Log log = LogFactory.getLog(ThrottleGatewayFilter.class); 37 | 38 | @Override 39 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 40 | 41 | String token = exchange.getRequest().getHeaders().getFirst("token"); 42 | System.out.println(token); 43 | String first = exchange.getRequest().getQueryParams().getFirst("first"); 44 | System.out.println(first); 45 | // TokenBucket tokenBucket = TokenBuckets.builder().withCapacity(capacity) 46 | // .withFixedIntervalRefillStrategy(refillTokens, refillPeriod, refillUnit) 47 | // .build(); 48 | // 49 | // // TODO: get a token bucket for a key 50 | // log.debug("TokenBucket capacity: " + tokenBucket.getCapacity()); 51 | // boolean consumed = tokenBucket.tryConsume(); 52 | if (Objects.nonNull(first)) { 53 | return chain.filter(exchange); 54 | } else { 55 | exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS); 56 | return exchange.getResponse().setComplete(); 57 | } 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.bage 6 | study-micro-services 7 | 0.0.1-SNAPSHOT 8 | pom 9 | 10 | 11 | 12 | website 13 | scp://webhost.company.com/www/website 14 | 15 | 16 | 17 | 18 | UTF-8 19 | 20 | 21 | study-micro-services-spring-boot 22 | study-micro-services-organization 23 | study-micro-services-h2 24 | study-micro-services-multi-datasources 25 | study-micro-services-spring-cloud-netflix 26 | study-micro-services-eureka 27 | study-micro-services-eureka-client1 28 | study-micro-services-eureka-client2 29 | study-micro-services-ribbon 30 | study-micro-services-zuul 31 | study-micro-services-feign 32 | study-micro-services-eureka-server2 33 | study-micro-services-eureka-server3 34 | study-micro-services-eureka-client3 35 | study-micro-services-hystrix 36 | study-micro-services-turbine 37 | study-micro-services-config 38 | study-micro-services-sleuth 39 | study-micro-services-gateway 40 | study-micro-services-zuul-dynamic-routes 41 | study-micro-services-eureka-client4 42 | study-micro-services-distribute-transaction 43 | study-micro-services-eureka-client5 44 | study-micro-services-eureka-client6 45 | study-micro-services-gateway-dynamic 46 | study-micro-services-bus 47 | study-micro-services-bus-client 48 | study-micro-services-config-client 49 | study-micro-services-stream 50 | 51 | 52 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/domain/Predicate.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.domain; 2 | 3 | import org.springframework.cloud.gateway.support.NameUtils; 4 | import org.springframework.util.StringUtils; 5 | 6 | import javax.validation.ValidationException; 7 | import java.util.LinkedHashMap; 8 | import java.util.Map; 9 | import java.util.Objects; 10 | 11 | public class Predicate { 12 | 13 | private String name; 14 | private Map args = new LinkedHashMap(); 15 | 16 | public Predicate() { 17 | } 18 | 19 | public Predicate(String text) { 20 | int eqIdx = text.indexOf(61); 21 | if (eqIdx <= 0) { 22 | throw new ValidationException("Unable to parse Predicate text '" + text + "', must be of the form name=value"); 23 | } else { 24 | this.setName(text.substring(0, eqIdx)); 25 | String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ","); 26 | 27 | for(int i = 0; i < args.length; ++i) { 28 | this.args.put(NameUtils.generateName(i), args[i]); 29 | } 30 | 31 | } 32 | } 33 | 34 | public String getName() { 35 | return this.name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | public Map getArgs() { 43 | return this.args; 44 | } 45 | 46 | public void setArgs(Map args) { 47 | this.args = args; 48 | } 49 | 50 | public void addArg(String key, String value) { 51 | this.args.put(key, value); 52 | } 53 | 54 | public boolean equals(Object o) { 55 | if (this == o) { 56 | return true; 57 | } else if (o != null && this.getClass() == o.getClass()) { 58 | Predicate that = (Predicate)o; 59 | return Objects.equals(this.name, that.name) && Objects.equals(this.args, that.args); 60 | } else { 61 | return false; 62 | } 63 | } 64 | 65 | public int hashCode() { 66 | return Objects.hash(new Object[]{this.name, this.args}); 67 | } 68 | 69 | public String toString() { 70 | StringBuilder sb = new StringBuilder("Predicate{"); 71 | sb.append("name='").append(this.name).append('\''); 72 | sb.append(", args=").append(this.args); 73 | sb.append('}'); 74 | return sb.toString(); 75 | } 76 | } -------------------------------------------------------------------------------- /study-micro-services-h2/src/main/java/com/bage/study/micro/services/h2/demo/official/OfficialApplication.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.h2.demo.official; 2 | 3 | import com.bage.study.micro.services.h2.domain.Customer; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.CommandLineRunner; 8 | import org.springframework.boot.SpringApplication; 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; 10 | import org.springframework.jdbc.core.JdbcTemplate; 11 | 12 | import java.util.Arrays; 13 | import java.util.List; 14 | import java.util.stream.Collectors; 15 | 16 | @SpringBootApplication 17 | public class OfficialApplication implements CommandLineRunner { 18 | 19 | private static final Logger log = LoggerFactory.getLogger(OfficialApplication.class); 20 | 21 | public static void main(String args[]) { 22 | SpringApplication.run(OfficialApplication.class, args); 23 | } 24 | 25 | @Autowired 26 | JdbcTemplate jdbcTemplate; 27 | 28 | @Override 29 | public void run(String... strings) throws Exception { 30 | 31 | log.info("Creating tables"); 32 | 33 | jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); 34 | jdbcTemplate.execute("CREATE TABLE customers(" + 35 | "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); 36 | 37 | // Split up the array of whole names into an array of first/last names 38 | List splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() 39 | .map(name -> name.split(" ")) 40 | .collect(Collectors.toList()); 41 | 42 | // Use a Java 8 stream to print out each tuple of the list 43 | splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); 44 | 45 | // Uses JdbcTemplate's batchUpdate operation to bulk load data 46 | jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); 47 | 48 | log.info("Querying for customer records where first_name = 'Josh':"); 49 | jdbcTemplate.query("SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[] { "Josh" }, 50 | (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")) 51 | ).forEach(customer -> log.info(customer.toString())); 52 | } 53 | } -------------------------------------------------------------------------------- /study-micro-services-zuul/src/main/java/com/bage/study/micro/services/zuul/provider/MyFallbackProvider.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.provider; 2 | 3 | import com.netflix.hystrix.exception.HystrixTimeoutException; 4 | import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; 5 | import org.springframework.http.HttpHeaders; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.client.ClientHttpResponse; 9 | import org.springframework.stereotype.Component; 10 | 11 | import java.io.ByteArrayInputStream; 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | 15 | @Component 16 | public class MyFallbackProvider implements FallbackProvider { 17 | 18 | /** 19 | * If you would like to provide a default fallback for all routes,
20 | * you can create a bean of type FallbackProvider and have the getRoute method return * or null, 21 | * @return 22 | */ 23 | @Override 24 | public String getRoute() { 25 | return "cloud-client5"; 26 | } 27 | 28 | @Override 29 | public ClientHttpResponse fallbackResponse(String route, final Throwable cause) { 30 | if (cause instanceof HystrixTimeoutException) { 31 | return response(HttpStatus.GATEWAY_TIMEOUT); 32 | } else { 33 | return response(HttpStatus.INTERNAL_SERVER_ERROR); 34 | } 35 | } 36 | 37 | private ClientHttpResponse response(final HttpStatus status) { 38 | return new ClientHttpResponse() { 39 | @Override 40 | public HttpStatus getStatusCode() throws IOException { 41 | return status; 42 | } 43 | 44 | @Override 45 | public int getRawStatusCode() throws IOException { 46 | return status.value(); 47 | } 48 | 49 | @Override 50 | public String getStatusText() throws IOException { 51 | return status.getReasonPhrase(); 52 | } 53 | 54 | @Override 55 | public void close() { 56 | } 57 | 58 | @Override 59 | public InputStream getBody() throws IOException { 60 | return new ByteArrayInputStream("fallback".getBytes()); 61 | } 62 | 63 | @Override 64 | public HttpHeaders getHeaders() { 65 | HttpHeaders headers = new HttpHeaders(); 66 | headers.setContentType(MediaType.APPLICATION_JSON); 67 | return headers; 68 | } 69 | }; 70 | } 71 | } -------------------------------------------------------------------------------- /study-micro-services-organization/src/main/java/com/bage/study/micro/services/organization/Application.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.organization; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.CommandLineRunner; 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 13 | import org.springframework.cache.annotation.EnableCaching; 14 | import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 15 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 16 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 17 | import org.springframework.cloud.openfeign.EnableFeignClients; 18 | import org.springframework.context.annotation.ComponentScan; 19 | import org.springframework.jdbc.core.JdbcTemplate; 20 | 21 | @EnableAutoConfiguration 22 | @ComponentScan(basePackageClasses=Application.class) 23 | @EnableCaching 24 | @EnableDiscoveryClient 25 | @EnableFeignClients 26 | @EnableCircuitBreaker 27 | @EnableHystrixDashboard 28 | public class Application implements CommandLineRunner { 29 | private static final Logger log = LoggerFactory.getLogger(Application.class); 30 | 31 | public static void main(String[] args) { 32 | SpringApplication.run(Application.class, args); 33 | } 34 | 35 | @Autowired 36 | JdbcTemplate jdbcTemplate; 37 | 38 | public void run(String... strings) throws Exception { 39 | 40 | log.info("-------------初始化数据库-----------"); 41 | 42 | jdbcTemplate.execute("DROP TABLE users IF EXISTS"); 43 | jdbcTemplate.execute("CREATE TABLE users(" 44 | + "id SERIAL, " 45 | + "name VARCHAR(255)" 46 | + ")"); 47 | 48 | // Split up the array of whole names into an array of first/last names 49 | List names = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long"); 50 | 51 | List params = new ArrayList(names.size()); 52 | for(int i = 0;i < names.size(); i++) { 53 | params.add(new Object[] {names.get(i)}); 54 | } 55 | // Uses JdbcTemplate's batchUpdate operation to bulk load data 56 | jdbcTemplate.batchUpdate("INSERT INTO users(name) VALUES (?)", params ); 57 | 58 | log.info("-------------初始化结束------------"); 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /study-micro-services-distribute-transaction/README.md: -------------------------------------------------------------------------------- 1 | # study-micro-services-distribute-transaction # 2 | 3 | ## 参考链接 ## 4 | - 聊聊分布式事务,再说说解决方案 [https://www.cnblogs.com/savorboard/p/distributed-system-transaction-consistency.html](https://www.cnblogs.com/savorboard/p/distributed-system-transaction-consistency.html) 5 | - 第一次有人把“分布式事务”讲的这么简单明了 [https://www.jianshu.com/p/16b1baf015e8](https://www.jianshu.com/p/16b1baf015e8) 6 | - 微服务架构下分布式事务解决方案——阿里GTS [https://www.cnblogs.com/jiangyu666/p/8522547.html](https://www.cnblogs.com/jiangyu666/p/8522547.html) 7 | 8 | ## 两阶段提交(2PC) ## 9 | 在 XA 协议中分为两阶段: 10 | 11 | 事务管理器要求每个涉及到事务的数据库预提交(precommit)此操作,并反映是否可以提交。 12 | 13 | 事务协调器要求每个数据库提交数据,或者回滚数据。 14 | 15 | ### 优点 ### 16 | - 尽量保证了数据的强一致,实现成本较低,在各大主流数据库都有自己实现,对于 MySQL 是从 5.5 开始支持。 17 | ### 缺点 ### 18 | - 单点问题:事务管理器在整个流程中扮演的角色很关键,如果其宕机,比如在第一阶段已经完成,在第二阶段正准备提交的时候事务管理器宕机,资源管理器就会一直阻塞,导致数据库无法使用。 19 | - 同步阻塞:在准备就绪之后,资源管理器中的资源一直处于阻塞,直到提交完成,释放资源。 20 | - 数据不一致:两阶段提交协议虽然为分布式数据强一致性所设计,但仍然存在数据不一致性的可能。比如在第二阶段中,假设协调者发出了事务 Commit 的通知,但是因为网络问题该通知仅被一部分参与者所收到并执行了 Commit 操作,其余的参与者则因为没有收到通知一直处于阻塞状态,这时候就产生了数据的不一致性。总的来说,XA 协议比较简单,成本较低,但是其单点问题,以及不能支持高并发(由于同步阻塞)依然是其最大的弱点。 21 | 22 | ## 补偿事务(TCC) ## 23 | 24 | 对于 TCC 的解释: 25 | - Try 阶段:尝试执行,完成所有业务检查(一致性),预留必需业务资源(准隔离性)。 26 | - Confirm 阶段:确认真正执行业务,不作任何业务检查,只使用 Try 阶段预留的业务资源,Confirm 操作满足幂等性。要求具备幂等设计,Confirm 失败后需要进行重试。 27 | - Cancel 阶段:取消执行,释放 Try 阶段预留的业务资源,Cancel 操作满足幂等性。Cancel 阶段的异常和 Confirm 阶段异常处理方案基本上一致。 28 | 29 | 举个简单的例子:如果你用 100 元买了一瓶水, 30 | Try 阶段:你需要向你的钱包检查是否够 100 元并锁住这 100 元,水也是一样的。如果有一个失败,则进行 Cancel(释放这 100 元和这一瓶水),如果 Cancel 失败不论什么失败都进行重试 Cancel,所以需要保持幂等。如果都成功,则进行 Confirm,确认这 100 元被扣,和这一瓶水被卖,如果 Confirm 失败无论什么失败则重试(会依靠活动日志进行重试)。 31 | 32 | 33 | ## 本地消息表(异步确保) ## 34 | 此方案的核心是将需要分布式处理的任务通过消息日志的方式来异步执行。消息日志可以存储到本地文本、数据库或消息队列,再通过业务规则自动或人工发起重试。 35 | 36 | 人工重试更多的是应用于支付场景,通过对账系统对事后问题的处理。 37 | 38 | 39 | ## MQ 事务消息 ## 40 | 在 RocketMQ 中实现了分布式事务,实际上是对本地消息表的一个封装,将本地消息表移动到了 MQ 内部。 41 | 42 | 基本流程如下:第一阶段 Prepared 消息,会拿到消息的地址。第二阶段执行本地事务。第三阶段通过第一阶段拿到的地址去访问消息,并修改状态。消息接受者就能使用这个消息。如果确认消息失败,在 RocketMQ Broker 中提供了定时扫描没有更新状态的消息。如果有消息没有得到确认,会向消息发送者发送消息,来判断是否提交,在 RocketMQ 中是以 Listener 的形式给发送者,用来处理。 43 | 44 | ## GTS--分布式事务解决方案 ## 45 | GTS的核心优势 46 | 47 | - 性能超强 48 | 49 | GTS通过大量创新,解决了事务ACID特性与高性能、高可用、低侵入不可兼得的问题。单事务分支的平均响应时间在2ms左右,3台服务器组成的集群可以支撑3万TPS以上的分布式事务请求。 50 | 51 | - 应用侵入性极低 52 | 53 | GTS对业务低侵入,业务代码最少只需要添加一行注解(@TxcTransaction)声明事务即可。业务与事务分离,将微服务从事务中解放出来,微服务关注于业务本身,不再需要考虑反向接口、幂等、回滚策略等复杂问题,极大降低了微服务开发的难度与工作量。 54 | 55 | - 完整解决方案 56 | 57 | GTS支持多种主流的服务框架,包括EDAS,Dubbo,Spring Cloud等。 58 | 有些情况下,应用需要调用第三方系统的接口,而第三方系统没有接入GTS。此时需要用到GTS的MT模式。GTS的MT模式可以等价于TCC模式,用户可以根据自身业务需求自定义每个事务阶段的具体行为。MT模式提供了更多的灵活性,可能性,以达到特殊场景下的自定义优化及特殊功能的实现。 59 | 60 | - 容错能力强 61 | 62 | GTS解决了XA事务协调器单点问题,实现真正的高可用,可以保证各种异常情况下的严格数据一致。 63 | -------------------------------------------------------------------------------- /study-micro-services-bus/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | study-micro-services 7 | com.bage 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | study-micro-services-bus 13 | 14 | study-micro-services-bus 15 | http://www.example.com 16 | 17 | 18 | UTF-8 19 | 2.0.1.RELEASE 20 | 3.4.6 21 | 1.3.2 22 | 1.8 23 | 1.8 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter 33 | 34 | 35 | 36 | org.springframework.cloud 37 | spring-cloud-starter-netflix-eureka-client 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-starter-config 42 | 43 | 44 | 45 | org.springframework.cloud 46 | spring-cloud-starter-bus-amqp 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-dependencies 56 | ${spring.boot.base.version} 57 | pom 58 | import 59 | 60 | 61 | org.springframework.cloud 62 | spring-cloud-dependencies 63 | Finchley.SR2 64 | pom 65 | import 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /study-micro-services-bus-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | study-micro-services 7 | com.bage 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | study-micro-services-bus-client 13 | 14 | study-micro-services-bus-client 15 | 16 | http://www.example.com 17 | 18 | 19 | UTF-8 20 | 2.0.1.RELEASE 21 | 3.4.6 22 | 1.3.2 23 | 1.8 24 | 1.8 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter 34 | 35 | 36 | 37 | org.springframework.cloud 38 | spring-cloud-starter-netflix-eureka-client 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-starter-config 43 | 44 | 45 | 46 | org.springframework.cloud 47 | spring-cloud-starter-bus-amqp 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-dependencies 57 | ${spring.boot.base.version} 58 | pom 59 | import 60 | 61 | 62 | org.springframework.cloud 63 | spring-cloud-dependencies 64 | Finchley.SR2 65 | pom 66 | import 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /study-micro-services-gateway-dynamic/src/main/java/com/bage/study/micro/services/gateway/route/controller/RouteController.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.gateway.route.controller; 2 | 3 | import com.bage.study.micro.services.gateway.route.domain.Predicate; 4 | import com.bage.study.micro.services.gateway.route.domain.Route; 5 | import com.bage.study.micro.services.gateway.route.dao.RouteMapper; 6 | import com.bage.study.micro.services.gateway.route.service.RouteService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.Arrays; 13 | import java.util.Collections; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | 17 | @RestController 18 | @RequestMapping("/route") 19 | public class RouteController { 20 | 21 | @Autowired 22 | RouteService routeService; 23 | 24 | Route route = new Route(); 25 | 26 | @RequestMapping("/insert") 27 | public Object insert(){ 28 | route.setId("cloud-client6"); 29 | route.setUri("lb://cloud-client6"); 30 | Predicate predicate = new Predicate(); 31 | predicate.setName("Path"); 32 | Map args = new HashMap<>(); 33 | args.put("pattern", "/client6/**"); 34 | predicate.setArgs(args); 35 | route.setPredicates(Arrays.asList(predicate)); 36 | return routeService.insert(route); 37 | } 38 | 39 | @RequestMapping("/delete/{id}") 40 | public Object delete(@PathVariable("id") String id){ 41 | route.setId("cloud-client6"); 42 | route.setUri("lb://cloud-client66"); 43 | Predicate predicate = new Predicate(); 44 | predicate.setName("Path"); 45 | Map args = new HashMap<>(); 46 | args.put("pattern", "/client6/**"); 47 | predicate.setArgs(args); 48 | route.setPredicates(Arrays.asList(predicate)); 49 | return routeService.delete(id); 50 | } 51 | 52 | @RequestMapping("/update") 53 | public Object update(){ 54 | route.setId("cloud-client6"); 55 | route.setUri("lb://cloud-client7"); 56 | Predicate predicate = new Predicate(); 57 | predicate.setName("Path"); 58 | Map args = new HashMap<>(); 59 | args.put("pattern", "/client6/**"); 60 | predicate.setArgs(args); 61 | route.setPredicates(Arrays.asList(predicate)); 62 | return routeService.update(route); 63 | } 64 | 65 | @RequestMapping("/query/{id}") 66 | public Object query(@PathVariable("id") String id){ 67 | return routeService.query(id); 68 | } 69 | 70 | @RequestMapping("/query/all") 71 | public Object queryAll(){ 72 | return routeService.queryAll(); 73 | } 74 | 75 | @RequestMapping("/refresh") 76 | public Object refresh(){ 77 | routeService.refresh(); 78 | return 1; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /study-micro-services-zuul-dynamic-routes/src/main/java/com/bage/study/micro/services/zuul/dynamix/routes/CustomRouteLocator.java: -------------------------------------------------------------------------------- 1 | package com.bage.study.micro.services.zuul.dynamix.routes; 2 | 3 | import com.bage.study.micro.services.zuul.dynamix.routes.service.RouteServiceImpl; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator; 6 | import org.springframework.cloud.netflix.zuul.filters.Route; 7 | import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator; 8 | import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; 9 | 10 | import java.util.Collection; 11 | import java.util.LinkedHashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | public class CustomRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator { 16 | 17 | private ZuulProperties zuulProperties; 18 | 19 | @Autowired 20 | private RouteServiceImpl routeService; 21 | 22 | public CustomRouteLocator(String servletPath, ZuulProperties properties) { 23 | super(servletPath, properties); 24 | this.zuulProperties = properties; 25 | } 26 | 27 | @Override 28 | public void refresh() { 29 | doRefresh(); 30 | } 31 | 32 | @Override 33 | protected Map locateRoutes() { 34 | Map routesMap = new LinkedHashMap(); 35 | 36 | zuulProperties.getRoutes().clear(); // 先清空 37 | 38 | routesMap.putAll(super.locateRoutes()); 39 | 40 | routesMap.putAll(locateRoutesFromDb()); 41 | 42 | zuulProperties.getRoutes().putAll(routesMap); // 放入配置的路由 43 | 44 | return routesMap; 45 | } 46 | 47 | private Map locateRoutesFromDb() { 48 | List list = routeService.queryAllFromCache(); 49 | Map routesMap = new LinkedHashMap(); 50 | for (int i = 0; i < list.size(); i++) { 51 | com.bage.study.micro.services.zuul.dynamix.routes.domain.Route item = list.get(i); 52 | routesMap.put(item.getPathPattern(),new ZuulProperties.ZuulRoute( 53 | item.getId(), 54 | item.getPathPattern(), 55 | item.getServiceId(), 56 | item.getUrl(), 57 | item.isStripPrefix(), 58 | item.getRetryable(), 59 | item.getSensitiveHeaders() 60 | )); 61 | } 62 | return routesMap; 63 | } 64 | 65 | @Override 66 | public Collection getIgnoredPaths() { 67 | return super.getIgnoredPaths(); 68 | } 69 | 70 | @Override 71 | public List getRoutes() { 72 | return super.getRoutes(); 73 | } 74 | 75 | @Override 76 | public Route getMatchingRoute(String path) { 77 | return super.getMatchingRoute(path); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /study-micro-services-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | study-micro-services 7 | com.bage 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | study-micro-services-config 13 | 14 | study-micro-services-config 15 | http://www.example.com 16 | 17 | 18 | 19 | UTF-8 20 | 2.0.1.RELEASE 21 | 3.4.6 22 | 1.3.2 23 | 1.8 24 | 1.8 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter 34 | 35 | 36 | org.springframework.cloud 37 | spring-cloud-starter-netflix-eureka-client 38 | 39 | 40 | org.springframework.cloud 41 | spring-cloud-starter-config 42 | 43 | 44 | org.springframework.cloud 45 | spring-cloud-config-server 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-actuator 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-dependencies 59 | ${spring.boot.base.version} 60 | pom 61 | import 62 | 63 | 64 | org.springframework.cloud 65 | spring-cloud-dependencies 66 | Finchley.SR2 67 | pom 68 | import 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /study-micro-services-sleuth/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | INFO 20 | 21 | 22 | 23 | ${CONSOLE_LOG_PATTERN} 24 | utf8 25 | 26 | 27 | 28 | 29 | 31 | 192.168.146.133:8044 32 | 33 | 35 | 36 | 37 | UTC 38 | 39 | 40 | 41 | { 42 | "severity": "%level", 43 | "appName":"${springAppName:-}", 44 | "service": "${springAppName:-}", 45 | "trace": "%X{X-B3-TraceId:-}", 46 | "span": "%X{X-B3-SpanId:-}", 47 | "exportable": "%X{X-Span-Export:-}", 48 | "pid": "${PID:-}", 49 | "thread": "%thread", 50 | "class": "%logger{40}", 51 | "rest": "%message" 52 | } 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | --------------------------------------------------------------------------------