├── chapter1 ├── eureka-client │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ ├── application.properties │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ └── MyEurekaClient.java │ └── pom.xml └── eureka-server │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── battcn │ │ │ └── EurekaServer.java │ │ └── resources │ │ └── bootstrap.yml │ └── pom.xml ├── chapter7 ├── order-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ ├── controller │ │ │ └── OrderController.java │ │ │ └── OrderApplication.java │ └── pom.xml ├── eureka-server │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── battcn │ │ │ │ └── EurekaServer.java │ │ │ └── resources │ │ │ └── bootstrap.yml │ └── pom.xml └── gateway-server │ ├── src │ └── main │ │ ├── resources │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── battcn │ │ └── GatewayApplication.java │ └── pom.xml ├── chapter4 ├── order-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ ├── interceptor │ │ │ └── FeignInterceptor.java │ │ │ ├── api │ │ │ └── ProductClient.java │ │ │ ├── OrderApplication.java │ │ │ └── controller │ │ │ └── OrderController.java │ └── pom.xml ├── product-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ └── ProductApplication.java │ └── pom.xml └── eureka-server │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── battcn │ │ │ └── EurekaServer.java │ │ └── resources │ │ └── bootstrap.yml │ └── pom.xml ├── chapter2 ├── order-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ ├── config │ │ │ └── RibbonRuleConfiguration.java │ │ │ ├── OrderApplication.java │ │ │ └── controller │ │ │ └── OrderController.java │ └── pom.xml ├── product-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ └── ProductApplication.java │ └── pom.xml └── eureka-server │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── battcn │ │ │ └── EurekaServer.java │ │ └── resources │ │ └── bootstrap.yml │ └── pom.xml ├── chapter3 ├── order-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ ├── OrderApplication.java │ │ │ ├── controller │ │ │ └── OrderController.java │ │ │ └── api │ │ │ └── ProductClient.java │ └── pom.xml ├── product-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── bootstrap.yml │ │ │ └── java │ │ │ └── com │ │ │ └── battcn │ │ │ └── ProductApplication.java │ └── pom.xml └── eureka-server │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── battcn │ │ │ └── EurekaServer.java │ │ └── resources │ │ └── bootstrap.yml │ └── pom.xml ├── chapter5 ├── eureka-server │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── battcn │ │ │ │ └── EurekaServer.java │ │ │ └── resources │ │ │ └── bootstrap.yml │ └── pom.xml └── order-server │ ├── src │ └── main │ │ ├── resources │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── battcn │ │ └── OrderApplication.java │ └── pom.xml ├── chapter6 ├── eureka-server │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── battcn │ │ │ │ └── EurekaServer.java │ │ │ └── resources │ │ │ └── bootstrap.yml │ └── pom.xml └── order-server │ ├── src │ └── main │ │ ├── resources │ │ └── bootstrap.yml │ │ └── java │ │ └── com │ │ └── battcn │ │ ├── controller │ │ └── OrderController.java │ │ ├── OrderApplication.java │ │ └── api │ │ └── ProductApi.java │ └── pom.xml ├── .gitignore └── README.md /chapter1/eureka-client/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/battcn/spring-cloud2-learning/HEAD/chapter1/eureka-client/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter7/order-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | instance-id: order-server 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:7071/eureka/ 10 | spring: 11 | application: 12 | name: order-server -------------------------------------------------------------------------------- /chapter4/order-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | instance-id: order-server 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:7071/eureka/ 10 | spring: 11 | application: 12 | name: order-server 13 | -------------------------------------------------------------------------------- /chapter4/product-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7073 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | instance-id: product-server 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:7071/eureka/ 10 | spring: 11 | application: 12 | name: product-server -------------------------------------------------------------------------------- /chapter2/order-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | spring: 4 | application: 5 | name: order-server 6 | cloud: 7 | inetutils: 8 | ignored-interfaces: 9 | - docker0 10 | - veth.* 11 | preferred-networks: 12 | - eth0 13 | eureka: 14 | instance: 15 | prefer-ip-address: true 16 | instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} 17 | client: 18 | service-url: 19 | defaultZone: http://localhost:7071/eureka/ 20 | -------------------------------------------------------------------------------- /chapter3/order-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | spring: 4 | application: 5 | name: order-server 6 | cloud: 7 | inetutils: 8 | ignored-interfaces: 9 | - docker0 10 | - veth.* 11 | preferred-networks: 12 | - eth0 13 | eureka: 14 | instance: 15 | prefer-ip-address: true 16 | instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} 17 | client: 18 | service-url: 19 | defaultZone: http://localhost:7071/eureka/ 20 | -------------------------------------------------------------------------------- /chapter2/product-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7074 3 | spring: 4 | application: 5 | name: product-server 6 | cloud: 7 | inetutils: 8 | ignored-interfaces: 9 | - docker0 10 | - veth.* 11 | preferred-networks: 12 | - eth0 13 | eureka: 14 | instance: 15 | prefer-ip-address: true 16 | instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} 17 | client: 18 | service-url: 19 | defaultZone: http://localhost:7071/eureka/ 20 | -------------------------------------------------------------------------------- /chapter3/product-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7073 3 | spring: 4 | application: 5 | name: product-server 6 | cloud: 7 | inetutils: 8 | ignored-interfaces: 9 | - docker0 10 | - veth.* 11 | preferred-networks: 12 | - eth0 13 | eureka: 14 | instance: 15 | prefer-ip-address: true 16 | instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} 17 | client: 18 | service-url: 19 | defaultZone: http://localhost:7071/eureka/ 20 | -------------------------------------------------------------------------------- /chapter1/eureka-client/src/main/java/com/battcn/MyEurekaClient.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | /** 8 | * @author Levin 9 | */ 10 | @EnableDiscoveryClient 11 | @SpringBootApplication 12 | public class MyEurekaClient { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(MyEurekaClient.class, args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /chapter1/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter2/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter3/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter4/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter5/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter6/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter7/eureka-server/src/main/java/com/battcn/EurekaServer.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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 | /** 8 | * @author Levin 9 | */ 10 | @EnableEurekaServer 11 | @SpringBootApplication 12 | public class EurekaServer { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(EurekaServer.class, args); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /chapter4/order-server/src/main/java/com/battcn/interceptor/FeignInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.battcn.interceptor; 2 | 3 | import feign.RequestInterceptor; 4 | import feign.RequestTemplate; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * @author Levin 9 | * @since 2018/9/26 0026 10 | */ 11 | @Configuration 12 | public class FeignInterceptor implements RequestInterceptor { 13 | 14 | @Override 15 | public void apply(RequestTemplate requestTemplate) { 16 | requestTemplate.header("token", "10086"); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /chapter2/order-server/src/main/java/com/battcn/config/RibbonRuleConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.battcn.config; 2 | 3 | import com.netflix.loadbalancer.IRule; 4 | import com.netflix.loadbalancer.RandomRule; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | /** 9 | * @author Levin 10 | * @since 2018/9/28 0028 11 | */ 12 | @Configuration 13 | public class RibbonRuleConfiguration { 14 | 15 | @Bean 16 | public IRule ribbonRule() { 17 | return new RandomRule(); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /chapter7/order-server/src/main/java/com/battcn/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.battcn.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | /** 8 | * @author Levin 9 | * @since 2018/9/26 0026 10 | */ 11 | @RestController 12 | @RequestMapping("/orders") 13 | public class OrderController { 14 | 15 | 16 | @GetMapping 17 | public String query() { 18 | return "{orderId : 10086}"; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /chapter7/gateway-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7073 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | instance-id: gateway-server 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:7071/eureka/ 10 | spring: 11 | application: 12 | name: gateway-server 13 | cloud: 14 | gateway: 15 | discovery: 16 | locator: 17 | enabled: true 18 | routes: 19 | #网关路由到订单服务order-service 20 | - id: order-server 21 | uri: lb://order-server 22 | predicates: 23 | - Path=/order-server/** -------------------------------------------------------------------------------- /chapter5/order-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | instance-id: order-server 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:7071/eureka/ 10 | spring: 11 | application: 12 | name: order-server 13 | # hystrix endpoint :http://localhost:7072/actuator/hystrix.stream 14 | management: 15 | endpoint: 16 | hystrix: 17 | stream: 18 | enabled: true 19 | endpoints: 20 | web: 21 | exposure: 22 | # 如果嫌麻烦,可以一次性加载所有的 23 | # include: '*' 24 | include: 'hystrix.stream' -------------------------------------------------------------------------------- /chapter3/order-server/src/main/java/com/battcn/OrderApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.openfeign.EnableFeignClients; 7 | 8 | /** 9 | * @author Levin 10 | */ 11 | @EnableFeignClients 12 | @EnableDiscoveryClient 13 | @SpringBootApplication 14 | public class OrderApplication { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(OrderApplication.class, args); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | gradle/ 5 | # maven # 6 | target 7 | logs 8 | 9 | # eclipse # 10 | .settings 11 | .project 12 | .classpath 13 | .log 14 | 15 | # windows # 16 | Thumbs.db 17 | 18 | # Mac # 19 | .DS_Store 20 | 21 | # Package Files # 22 | *.war 23 | *.ear 24 | 25 | ### STS ### 26 | .apt_generated 27 | .classpath 28 | .factorypath 29 | .project 30 | .settings 31 | .springBeans 32 | 33 | ### IntelliJ IDEA ### 34 | .idea 35 | *.iws 36 | *.iml 37 | *.ipr 38 | .idea/ 39 | overlays 40 | bin 41 | 42 | 43 | ### NetBeans ### 44 | nbproject/private/ 45 | build/ 46 | nbbuild/ 47 | dist/ 48 | nbdist/ 49 | .nb-gradle/ 50 | -------------------------------------------------------------------------------- /chapter7/order-server/src/main/java/com/battcn/OrderApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.netflix.eureka.EnableEurekaClient; 7 | 8 | /** 9 | * @author Levin 10 | */ 11 | @EnableEurekaClient 12 | @EnableDiscoveryClient 13 | @SpringBootApplication 14 | public class OrderApplication { 15 | 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(OrderApplication.class, args); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /chapter7/gateway-server/src/main/java/com/battcn/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.netflix.eureka.EnableEurekaClient; 7 | 8 | /** 9 | * @author Levin 10 | */ 11 | @EnableEurekaClient 12 | @EnableDiscoveryClient 13 | @SpringBootApplication 14 | public class GatewayApplication { 15 | 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(GatewayApplication.class, args); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /chapter4/order-server/src/main/java/com/battcn/api/ProductClient.java: -------------------------------------------------------------------------------- 1 | package com.battcn.api; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | 7 | /** 8 | * @author Levin 9 | * @since 2018/9/26 0026 10 | */ 11 | @FeignClient(name = "product-server/products") 12 | public interface ProductClient { 13 | 14 | /** 15 | * 根据产品ID查询产品信息 16 | * 17 | * @param productId ID 18 | * @return 查询结果 19 | */ 20 | @GetMapping("{product_id}") 21 | String selectProductById(@PathVariable("product_id") Long productId); 22 | } 23 | -------------------------------------------------------------------------------- /chapter6/order-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | instance-id: order-server 7 | client: 8 | serviceUrl: 9 | defaultZone: http://localhost:7071/eureka/ 10 | spring: 11 | application: 12 | name: order-server 13 | 14 | # 开启Feign Hystrix 支持 不知道为什么,F版本还不弄个 Properties 提示 15 | feign: 16 | hystrix: 17 | enabled: true 18 | # hystrix endpoint :http://localhost:7072/actuator/hystrix.stream 19 | management: 20 | endpoint: 21 | hystrix: 22 | stream: 23 | enabled: true 24 | endpoints: 25 | web: 26 | exposure: 27 | # 如果嫌麻烦,可以一次性加载所有的 28 | # include: '*' 29 | include: 'hystrix.stream' -------------------------------------------------------------------------------- /chapter2/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | server: 7 | # 关闭保护模式 8 | enable-self-preservation: false 9 | # 清理间隔(默认是60 * 1000 毫秒) 10 | eviction-interval-timer-in-ms: 60000 11 | # Eureka 拉取服务列表时间(默认:30秒) 12 | remote-region-registry-fetch-interval: 5 13 | client: 14 | register-with-eureka: false 15 | fetch-registry: false 16 | serviceUrl: 17 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 18 | defaultZone: http://localhost:${server.port}/eureka/ 19 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 20 | registry-fetch-interval-seconds: 5 21 | spring: 22 | application: 23 | name: eureka-server -------------------------------------------------------------------------------- /chapter3/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | server: 7 | # 关闭保护模式 8 | enable-self-preservation: false 9 | # 清理间隔(默认是60 * 1000 毫秒) 10 | eviction-interval-timer-in-ms: 60000 11 | # Eureka 拉取服务列表时间(默认:30秒) 12 | remote-region-registry-fetch-interval: 5 13 | client: 14 | register-with-eureka: false 15 | fetch-registry: false 16 | serviceUrl: 17 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 18 | defaultZone: http://localhost:${server.port}/eureka/ 19 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 20 | registry-fetch-interval-seconds: 5 21 | spring: 22 | application: 23 | name: eureka-server -------------------------------------------------------------------------------- /chapter4/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | server: 7 | # 关闭保护模式 8 | enable-self-preservation: false 9 | # 清理间隔(默认是60 * 1000 毫秒) 10 | eviction-interval-timer-in-ms: 60000 11 | # Eureka 拉取服务列表时间(默认:30秒) 12 | remote-region-registry-fetch-interval: 5 13 | client: 14 | register-with-eureka: false 15 | fetch-registry: false 16 | serviceUrl: 17 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 18 | defaultZone: http://localhost:${server.port}/eureka/ 19 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 20 | registry-fetch-interval-seconds: 5 21 | spring: 22 | application: 23 | name: eureka-server -------------------------------------------------------------------------------- /chapter4/order-server/src/main/java/com/battcn/OrderApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.netflix.eureka.EnableEurekaClient; 7 | import org.springframework.cloud.openfeign.EnableFeignClients; 8 | 9 | /** 10 | * @author Levin 11 | */ 12 | @EnableFeignClients 13 | @EnableEurekaClient 14 | @EnableDiscoveryClient 15 | @SpringBootApplication 16 | public class OrderApplication { 17 | 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(OrderApplication.class, args); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /chapter5/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | server: 7 | # 关闭保护模式 8 | enable-self-preservation: false 9 | # 清理间隔(默认是60 * 1000 毫秒) 10 | eviction-interval-timer-in-ms: 60000 11 | # Eureka 拉取服务列表时间(默认:30秒) 12 | remote-region-registry-fetch-interval: 5 13 | client: 14 | register-with-eureka: false 15 | fetch-registry: false 16 | serviceUrl: 17 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 18 | defaultZone: http://localhost:${server.port}/eureka/ 19 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 20 | registry-fetch-interval-seconds: 5 21 | spring: 22 | application: 23 | name: eureka-server -------------------------------------------------------------------------------- /chapter6/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | server: 7 | # 关闭保护模式 8 | enable-self-preservation: false 9 | # 清理间隔(默认是60 * 1000 毫秒) 10 | eviction-interval-timer-in-ms: 60000 11 | # Eureka 拉取服务列表时间(默认:30秒) 12 | remote-region-registry-fetch-interval: 5 13 | client: 14 | register-with-eureka: false 15 | fetch-registry: false 16 | serviceUrl: 17 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 18 | defaultZone: http://localhost:${server.port}/eureka/ 19 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 20 | registry-fetch-interval-seconds: 5 21 | spring: 22 | application: 23 | name: eureka-server -------------------------------------------------------------------------------- /chapter7/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | server: 7 | # 关闭保护模式 8 | enable-self-preservation: false 9 | # 清理间隔(默认是60 * 1000 毫秒) 10 | eviction-interval-timer-in-ms: 60000 11 | # Eureka 拉取服务列表时间(默认:30秒) 12 | remote-region-registry-fetch-interval: 5 13 | client: 14 | register-with-eureka: false 15 | fetch-registry: false 16 | serviceUrl: 17 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 18 | defaultZone: http://localhost:${server.port}/eureka/ 19 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 20 | registry-fetch-interval-seconds: 5 21 | spring: 22 | application: 23 | name: eureka-server -------------------------------------------------------------------------------- /chapter3/order-server/src/main/java/com/battcn/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.battcn.controller; 2 | 3 | import com.battcn.api.ProductClient; 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 | /** 10 | * @author Levin 11 | * @since 2018/9/26 0026 12 | */ 13 | @RestController 14 | @RequestMapping("/orders") 15 | public class OrderController { 16 | 17 | @Autowired 18 | private ProductClient productClient; 19 | 20 | 21 | @GetMapping 22 | public String query() { 23 | return this.productClient.selectProductById(10L); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /chapter6/order-server/src/main/java/com/battcn/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.battcn.controller; 2 | 3 | import com.battcn.api.ProductApi; 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 | /** 10 | * @author Levin 11 | * @since 2018/9/26 0026 12 | */ 13 | @RestController 14 | @RequestMapping("/orders") 15 | public class OrderController { 16 | 17 | @Autowired 18 | private ProductApi productApi; 19 | 20 | 21 | @GetMapping 22 | public String query() { 23 | return this.productApi.selectProductById(10L); 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /chapter4/order-server/src/main/java/com/battcn/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.battcn.controller; 2 | 3 | import com.battcn.api.ProductClient; 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 | /** 10 | * @author Levin 11 | * @since 2018/9/26 0026 12 | */ 13 | @RestController 14 | @RequestMapping("/orders") 15 | public class OrderController { 16 | 17 | @Autowired 18 | private ProductClient productClient; 19 | 20 | 21 | @GetMapping 22 | public String query() { 23 | return this.productClient.selectProductById(10L); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /chapter3/order-server/src/main/java/com/battcn/api/ProductClient.java: -------------------------------------------------------------------------------- 1 | package com.battcn.api; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | 7 | /** 8 | * @author Levin 9 | * @since 2018/9/26 0026 10 | */ 11 | @FeignClient(name = "product-server/products", decode404 = true) 12 | //@FeignClient(name = "products", url = "http://localhost:7073/products") 13 | //@FeignClient(value = "product", serviceId = "product-server", path = "/products", decode404 = true) 14 | public interface ProductClient { 15 | 16 | /** 17 | * 根据产品ID查询产品信息 18 | * 19 | * @param productId ID 20 | * @return 查询结果 21 | */ 22 | @GetMapping("/{product_id}") 23 | String selectProductById(@PathVariable("product_id") Long productId); 24 | } 25 | -------------------------------------------------------------------------------- /chapter3/product-server/src/main/java/com/battcn/ProductApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | /** 11 | * @author Levin 12 | */ 13 | @RestController 14 | @EnableDiscoveryClient 15 | @SpringBootApplication 16 | public class ProductApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(ProductApplication.class, args); 20 | } 21 | 22 | @GetMapping("/products/{id}") 23 | public String query(@PathVariable Long id) { 24 | return id + ":Spring Boot..."; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /chapter6/order-server/src/main/java/com/battcn/OrderApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.netflix.eureka.EnableEurekaClient; 7 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 8 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 9 | import org.springframework.cloud.openfeign.EnableFeignClients; 10 | 11 | /** 12 | * @author Levin 13 | */ 14 | @EnableHystrix 15 | @EnableHystrixDashboard 16 | @EnableFeignClients 17 | @EnableEurekaClient 18 | @EnableDiscoveryClient 19 | @SpringBootApplication 20 | public class OrderApplication { 21 | 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(OrderApplication.class, args); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /chapter1/eureka-server/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7071 3 | eureka: 4 | instance: 5 | prefer-ip-address: true 6 | # 生产环境中官方是不建议修改默认配置,因为那样会破坏 eureka server 的保护模式 7 | server: 8 | # 关闭保护模式(生产环境不建议修改) 9 | enable-self-preservation: false 10 | # 清理间隔(默认是60 * 1000 毫秒)(生产环境不建议修改) 11 | eviction-interval-timer-in-ms: 60000 12 | # Eureka 拉取服务列表时间(默认:30秒)(生产环境不建议修改) 13 | remote-region-registry-fetch-interval: 5 14 | client: 15 | # eureka server 没必要自己把自己注册上去,所以可以设置成 false 16 | register-with-eureka: false 17 | # 是否从Eureka Server上获取注册信息,默认为true,此处建议修改成 false (单机设置的意义不大,如果设置成 true 启动会去抓取一次注册表,获取不到更新缓存就会出错(该错误不影响 eureka 正常使用)) 18 | fetch-registry: false 19 | service-url: 20 | # 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); 21 | # 划重点:此处的 defaultZone 千万别写成 default-zone 22 | defaultZone: http://localhost:${server.port}/eureka/ 23 | # 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒) 24 | registry-fetch-interval-seconds: 5 25 | -------------------------------------------------------------------------------- /chapter6/order-server/src/main/java/com/battcn/api/ProductApi.java: -------------------------------------------------------------------------------- 1 | package com.battcn.api; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | 8 | /** 9 | * @author Levin 10 | * @since 2018/9/26 0026 11 | */ 12 | @FeignClient(name = "product-server/products", fallback = ProductApi.ProductApiFallback.class) 13 | public interface ProductApi { 14 | 15 | /** 16 | * 根据产品ID查询产品信息 17 | * 18 | * @param productId ID 19 | * @return 查询结果 20 | */ 21 | @GetMapping("{product_id}") 22 | String selectProductById(@PathVariable("product_id") Long productId); 23 | 24 | @Component 25 | class ProductApiFallback implements ProductApi { 26 | @Override 27 | public String selectProductById(Long productId) { 28 | return "feign error callback"; 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /chapter2/product-server/src/main/java/com/battcn/ProductApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 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.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * @author Levin 13 | */ 14 | @RestController 15 | @EnableDiscoveryClient 16 | @SpringBootApplication 17 | public class ProductApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(ProductApplication.class, args); 21 | } 22 | 23 | @Value("${server.port}") 24 | private int port; 25 | 26 | @GetMapping("/products") 27 | public String query() { 28 | return "port : " + port; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter1/eureka-client/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 7072 3 | # 如果不写 Eureka Server 中的界面中 Application 就会是 Unknown 尽量写 4 | spring: 5 | application: 6 | name: eureka-client 7 | cloud: 8 | inetutils: 9 | # 忽略指定网卡(支持正则),假设你的电脑有 VM 那么该选项是非常有用的一个选项 10 | ignored-interfaces: #忽略 docker0 网卡以及 veth 开头的网卡 11 | - docker0 12 | - veth.* 13 | preferred-networks: # 使用指定网络地址,选择 eth0 网卡,当然也可以直接写 IP (192.168) 14 | - eth0 15 | eureka: 16 | instance: 17 | # 此处建议写,不写默认是机器名 18 | prefer-ip-address: true 19 | # 优先级小于 application.properties ,如果你想知道当前注册上去的版本必须使用 application.properties 中的配置写法 20 | # 因为 bootstrap.yml 最早初始化,那时候还无法读取到 pom.xml 中的属性 21 | instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}} 22 | # ip-address 强行指定此实例 IP ,不是很推荐,绝大多数情况 prefer-ip-address 与 preferred-networks 配合都够用了 23 | client: 24 | service-url: 25 | # 划重点:此处的 defaultZone 千万别写成 default-zone 26 | defaultZone: http://localhost:7071/eureka/ 27 | -------------------------------------------------------------------------------- /chapter2/order-server/src/main/java/com/battcn/OrderApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.context.annotation.Configuration; 9 | import org.springframework.web.client.RestTemplate; 10 | 11 | /** 12 | * @author Levin 13 | */ 14 | @EnableDiscoveryClient 15 | @SpringBootApplication 16 | public class OrderApplication { 17 | 18 | @Configuration 19 | class MyConfiguration { 20 | 21 | @LoadBalanced 22 | @Bean 23 | RestTemplate restTemplate() { 24 | // 默认 RestTemplate 是无负载功能的,所以我们需要重新定义 RestTemplate 申请成 @LoadBalanced(默认情况是轮训) 25 | return new RestTemplate(); 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(OrderApplication.class, args); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /chapter4/product-server/src/main/java/com/battcn/ProductApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 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.netflix.eureka.EnableEurekaClient; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestHeader; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | * @author Levin 14 | */ 15 | @EnableEurekaClient 16 | @RestController 17 | @EnableDiscoveryClient 18 | @SpringBootApplication 19 | public class ProductApplication { 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(ProductApplication.class, args); 23 | } 24 | 25 | @GetMapping("/products/{id}") 26 | public String query(@PathVariable Long id, @RequestHeader String token) { 27 | return id + ":Spring Boot... token = " + token; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /chapter5/order-server/src/main/java/com/battcn/OrderApplication.java: -------------------------------------------------------------------------------- 1 | package com.battcn; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 | import org.springframework.beans.factory.annotation.Autowired; 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.cloud.netflix.eureka.EnableEurekaClient; 9 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 10 | import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; 11 | import org.springframework.stereotype.Service; 12 | import org.springframework.web.bind.annotation.GetMapping; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | /** 16 | * @author Levin 17 | */ 18 | @RestController 19 | @EnableHystrix 20 | @EnableHystrixDashboard 21 | @EnableEurekaClient 22 | @EnableDiscoveryClient 23 | @SpringBootApplication 24 | public class OrderApplication { 25 | 26 | 27 | public static void main(String[] args) { 28 | SpringApplication.run(OrderApplication.class, args); 29 | } 30 | 31 | @Autowired 32 | private ProductService productService; 33 | 34 | 35 | @GetMapping("/orders") 36 | public String query() { 37 | return productService.selectProducts(); 38 | } 39 | 40 | @Service 41 | class ProductService { 42 | @HystrixCommand(fallbackMethod = "defaultProduct") 43 | public String selectProducts() { 44 | throw new RuntimeException("假设你在使用 LB 发送请求时报错了,或者对方服务 DOWN 机了"); 45 | } 46 | 47 | public String defaultProduct() { 48 | return "error"; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter2/order-server/src/main/java/com/battcn/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package com.battcn.controller; 2 | 3 | import com.battcn.config.RibbonRuleConfiguration; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cloud.client.ServiceInstance; 6 | import org.springframework.cloud.client.discovery.DiscoveryClient; 7 | import org.springframework.cloud.netflix.ribbon.RibbonClient; 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 | import org.springframework.web.client.RestTemplate; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * @author Levin 17 | * @since 2018/9/28 0028 18 | */ 19 | @RestController 20 | @RequestMapping("/orders") 21 | @RibbonClient(name = "ribbonRule",configuration = RibbonRuleConfiguration.class) 22 | public class OrderController { 23 | 24 | private final RestTemplate restTemplate; 25 | private final DiscoveryClient discoveryClient; 26 | 27 | @Autowired 28 | public OrderController(RestTemplate restTemplate, DiscoveryClient discoveryClient) { 29 | this.restTemplate = restTemplate; 30 | this.discoveryClient = discoveryClient; 31 | } 32 | 33 | @GetMapping 34 | public String query() { 35 | final List services = discoveryClient.getServices(); 36 | for (String service : services) { 37 | List list = discoveryClient.getInstances(service); 38 | for (ServiceInstance instance : list) { 39 | System.out.println(instance.getUri() + "/" + service + " - " + instance.getServiceId()); 40 | } 41 | } 42 | return restTemplate.getForObject("http://PRODUCT-SERVER/products", String.class); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-cloud2-learning 2 | 3 | F版刚出来那会,由于事情较多就没有好好研究,期间有不少同行询问我最新版本案例及博客相关进展,故在闲暇之余编写基于 `Spring Cloud Finchley.SR1.RELEASE` 版本进行的系列教程,同时欢迎关注我的公众号 **`battcn`** 4 | 5 | # 官方文档 6 | 7 | [http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html](http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html "http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html") 8 | 9 | # 公众号 10 | 11 | ![公众号](http://image.battcn.com/assets/images/wxgzh8cm.png) 12 | 13 | # 推荐开源项目 14 | 15 | - GitHub:[https://github.com/battcn/spring-boot2-learning](https://github.com/battcn/spring-boot2-learning "https://github.com/battcn/spring-boot2-learning") 16 | - GitHub:[https://github.com/battcn/swagger-spring-boot](https://github.com/battcn/swagger-spring-boot "https://github.com/battcn/swagger-spring-boot") 17 | - 码云:[https://gitee.com/battcn/spring-boot-starter-swagger/](https://gitee.com/battcn/spring-boot-starter-swagger/ "https://gitee.com/battcn/spring-boot-starter-swagger/") 18 | - 码云:[https://gitee.com/battcn/battcn3.0](https://gitee.com/battcn/battcn3.0 "https://gitee.com/battcn/battcn3.0") 19 | - 码云:[https://gitee.com/battcn/battcn-plus](https://gitee.com/battcn/battcn-plus "https://gitee.com/battcn/battcn-plus") 20 | 21 | # 目录介绍 22 | 23 | - **chapter1:** [一起来学Spring Cloud | 第一篇:认识Eureka](http://blog.battcn.com/2018/09/27/springcloud/finchley/finchley-learn-eureka/) 24 | - **chapter2:** [一起来学Spring Cloud | 第二篇:Ribbon软负载](http://blog.battcn.com/) 25 | - **chapter3:** [一起来学Spring Cloud | 第三篇:注解式HTTP请求Feign](http://blog.battcn.com/) 26 | - **chapter4:** [一起来学Spring Cloud | 第四篇:Feign拦截器应用](http://blog.battcn.com/) 27 | - **chapter5:** [一起来学Spring Cloud | 第五篇:Hystrix 与 Ribbon](http://blog.battcn.com/) 28 | - **chapter6:** [一起来学Spring Cloud | 第六篇:Hystrix 与 Feign](http://blog.battcn.com/) 29 | - **chapter7:** [一起来学Spring Cloud | 第六篇:初识Gateway](http://blog.battcn.com/) -------------------------------------------------------------------------------- /chapter2/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | Finchley.SR1 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter3/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | Finchley.SR1 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter4/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | Finchley.SR1 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter5/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | Finchley.SR1 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter6/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | Finchley.SR1 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter7/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | Finchley.SR1 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter1/eureka-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.battcn 6 | eureka-server 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | UTF-8 18 | UTF-8 19 | 1.8 20 | true 21 | 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-server 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-dependencies 36 | Finchley.SR1 37 | pom 38 | import 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter1/eureka-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | eureka-client 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | Finchley.SR1 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | build-info 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /chapter2/order-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | order-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | Finchley.SR1 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | build-info 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /chapter2/product-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | product-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | Finchley.SR1 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | build-info 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /chapter3/product-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | product-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 2.0.1.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 1.8 22 | true 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.cloud 32 | spring-cloud-starter-netflix-eureka-client 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-dependencies 41 | Finchley.SR1 42 | pom 43 | import 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | build-info 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /chapter7/order-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | order-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-server 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | Finchley.SR1 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | build-info 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /chapter4/product-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | product-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-server 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | Finchley.SR1 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | build-info 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /chapter3/order-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | order-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-openfeign 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | Finchley.SR1 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | build-info 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /chapter7/gateway-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | gateway-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-client 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-webflux 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-gateway 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | Finchley.SR1 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | build-info 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /chapter4/order-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | order-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-server 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-openfeign 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-dependencies 44 | Finchley.SR1 45 | pom 46 | import 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | build-info 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /chapter5/order-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | order-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-server 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-netflix-hystrix 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-starter-netflix-hystrix-dashboard 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-actuator 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.cloud 51 | spring-cloud-dependencies 52 | Finchley.SR1 53 | pom 54 | import 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-maven-plugin 66 | 67 | 68 | 69 | build-info 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /chapter6/order-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.battcn 7 | order-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.0.1.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | true 22 | 23 | 24 | 25 | 26 | org.springframework.cloud 27 | spring-cloud-starter-netflix-eureka-server 28 | 29 | 30 | org.springframework.cloud 31 | spring-cloud-starter-netflix-eureka-client 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-openfeign 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-starter-netflix-hystrix 40 | 41 | 42 | org.springframework.cloud 43 | spring-cloud-starter-netflix-hystrix-dashboard 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-actuator 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.cloud 55 | spring-cloud-dependencies 56 | Finchley.SR1 57 | pom 58 | import 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-maven-plugin 70 | 71 | 72 | 73 | build-info 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | --------------------------------------------------------------------------------