1、認識java
1. Monolith(單體應用)架構web
在編譯時,這些項目將被打包成爲一個個JAR包,並最終合併在一塊兒造成一個WAR包。算法
最終部署的時候只有一份war包,其餘的以jar包的方式依賴來.spring
缺點:在項目很小的狀況下這種單體應用比較簡單,可是隨着項目越變越大,代碼愈來愈多。就會存在如下缺點。springboot
①編譯難,部署難,測試難 ②技術選擇難 ③擴展難架構
單體應用中多個模塊的負載不均衡,咱們擴容高負載的時候,也把低負載的模塊也擴容,極大浪費了資源.app
2.MicroService(微服務)架構負載均衡
微服務就是把一個單體項目,拆分爲多個微服務,每一個微服務能夠獨立技術選型,獨立開發,獨立部署,獨立運維.而且多個服務相互協調,相互配合,最終完成用戶的價值.框架
優點:運維
獨立部署、技術選型靈活、容錯、擴展、
3.springcloud
Spring Cloud是一系列框架的有序集合。
經過Spring Boot風格進行再封裝屏蔽掉了複雜的配置和實現原理,最終給開發者留出了一套簡單易懂、易部署和易維護的分佈式系統開發工具包。
五大神獸:
服務發現——Netflix Eureka
客服端負載均衡——Netflix Ribbon/Feign
服務網關——Netflix Zuul
斷路器——Netflix Hystrix
分佈式配置——Spring Cloud Config
2、springcloud入門之服務發現
以maven多模塊化的方法搭建
1.搭建maven項目 springcloud_parent
1.1.配置pom.xml
1 <!-- 指定版本--> 2 <properties> 3 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 4 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 5 <java.version>1.8</java.version> 6 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 7 <springboot.version>2.0.5.RELEASE</springboot.version> 8 </properties> 9 10 <dependencyManagement> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework.cloud</groupId> 14 <artifactId>spring-cloud-dependencies</artifactId> 15 <version>${spring-cloud.version}</version> 16 <type>pom</type> 17 <scope>import</scope> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-dependencies</artifactId> 22 <version>${springboot.version}</version> 23 <type>pom</type> 24 <scope>import</scope> 25 </dependency> 26 </dependencies> 27 </dependencyManagement>
2.單機註冊中心搭建 springcloud_eureka_7001
2.1配置pom.xml
1 <dependencies> 2 <!--springboot支持--> 3 <dependency> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-starter-web</artifactId> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-test</artifactId> 10 </dependency> 11 12 <!--Eureka服務端支持--> 13 <dependency> 14 <groupId>org.springframework.cloud</groupId> 15 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 16 </dependency> 17 </dependencies>
2.2 配置application.yml
1 server: 2 port: 7001 3 eureka: 4 instance: 5 hostname: eureka-7001.com 6 client: 7 registerWithEureka: false #做爲一個註冊中心不須要註冊本身是否要註冊到eureka 8 fetchRegistry: false #表示是否從Eureka Server獲取註冊信息 9 serviceUrl: 10 #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #單機配置 11 defaultZone: http://eureka-7002.com:7002/eureka/ #集羣配置,若是有多個,有逗號分割,不要包含本身1
2.3啓動項
1 @SpringBootApplication 2 @EnableEurekaServer//標識註冊中心 3 public class EurekaServerApp_7001 { 4 public static void main(String[] args) { 5 SpringApplication.run(EurekaServerApp_7001.class); 6 } 7 }
2.4效果圖 訪問 localhost:7001
Eureka Server 在運行期間會去統計心跳失敗比例在 15 分鐘以內是否低於 85%,若是低於 85%,Eureka Server 會將這些實例保護起來,讓這些實例不會過時,可是在保護期內若是服務恰好這個服務提供者非正常下線了,此時服務消費者就會拿到一個無效的服務實例,此時會調用失敗,對於這個問題須要服務消費者端要有一些容錯機制,如重試,斷路器等。
3.服務提供者 springcloud_provider_8001
3.1配置pom.xml
1 <dependencies> 2 <!--公共代碼依賴--> 3 <dependency> 4 <groupId>cn.su</groupId> 5 <artifactId>user_interface</artifactId> 6 <version>1.0-SNAPSHOT</version> 7 </dependency> 8 9 <!--springboot支持--> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 <dependency> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-test</artifactId> 17 </dependency> 18 19 <!--eureka客戶端支持 --> 20 <dependency> 21 <groupId>org.springframework.cloud</groupId> 22 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 23 </dependency> 24 </dependencies>
3.2 配置 application.yml
1 server: 2 port: 8001 3 spring: 4 application: 5 name: USER-PROVIDER #不要使用下劃線 6 eureka: 7 client: 8 service-url: 9 defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka 10 #defaultZone: http://localhost:7001/eureka 11 instance: 12 prefer-ip-address: true #告訴服務提供者要把服務註冊到哪兒
3.3 配置提供者服務方法 UserController
1 @RestController 2 @RequestMapping("/provider") 3 public class UserController { 4 5 @RequestMapping("/user/{id}") 6 public User getUser(@PathVariable("id") Long id) { 7 return new User(id, "8001su"); 8 } 9 }
3.4啓動項
1 @SpringBootApplication 2 @EnableEurekaClient 3 public class UserServerProviderApp_8001 { 4 5 public static void main(String[] args) { 6 SpringApplication.run(UserServerProviderApp_8001.class); 7 } 8 }
3.5 效果圖
4.服務消費者 springcloud_consumer_9001
4.1準備domain模塊 user_interface
1 public class User { 2 private Long id; 3 private String name; 4 5 //省略構造器和get、set 6 }
4.2配置pom.xml
1 <dependencies> 2 <!--公共代碼依賴--> 3 <dependency> 4 <groupId>cn.su</groupId> 5 <artifactId>user_interface</artifactId> 6 <version>1.0-SNAPSHOT</version> 7 </dependency> 8 9 <!--springboot支持--> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 <dependency> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-test</artifactId> 17 </dependency> 18 <!--eureka客戶端,服務消費者也要從註冊中心獲取可用服務列表--> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 22 </dependency> 23 <!--客戶端負載均衡實現 ribbon--> 24 <dependency> 25 <groupId>org.springframework.cloud</groupId> 26 <artifactId>spring-cloud-starter-ribbon</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.cloud</groupId> 30 <artifactId>spring-cloud-starter-config</artifactId> 31 </dependency> 32 </dependencies>
4.3 配置application.yml
1 server: 2 port: 9001 3 spring: 4 application: 5 name: USER_CONSUMER 6 eureka: 7 client: 8 registerWithEureka: false #不註冊到Eureka,不在註冊中心顯示 9 service-url: 10 #defaultZone: http://localhost:7001/eureka 11 defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka
4.4配置類
1 @Configuration 2 public class CfgBean { 3 4 @Bean 5 public RestTemplate getRestTemplate(){ 6 return new RestTemplate(); 7 } 8 }
4.5userController
1 @RestController 2 @RequestMapping("/consumer") 3 public class UserController { 4 5 @Autowired 6 private RestTemplate restTemplate; 7 8 @RequestMapping("/user/{id}") 9 public User getUser(@PathVariable("id") Long id){ 10 String url="http://localhost:8001/provider/user/"+id; 11 //String url="http://USER-PROVIDER/provider/user/"+id; 12 return restTemplate.getForObject(url, User.class); 13 } 14 }
4.6啓動項
1 @SpringBootApplication 2 @EnableEurekaClient 3 public class UserConsumerApp_9001 { 4 public static void main(String[] args) { 5 SpringApplication.run(UserConsumerApp_9001.class); 6 } 7 }
4.5效果圖
3、springcloud 入門之 負載均衡
方法一:客戶端負載均衡實現 ribbon
1.1寫一個類配置負載均衡
1 @Configuration 2 public class CfgBean { 3 4 @Bean 5 @LoadBalanced//負載均衡 輪循 6 public RestTemplate getRestTemplate(){ 7 return new RestTemplate(); 8 } 9 10 //負載均衡配置 11 @Bean 12 public IRule myRule(){ 13 return new RandomRule();//隨機算法替換輪訓 14 } 15 }
1.2 userController
1 @RestController 2 @RequestMapping("/consumer") 3 public class UserController { 4 5 @Autowired 6 private RestTemplate restTemplate; 7 8 @RequestMapping("/user/{id}") 9 public User getUser(@PathVariable("id") Long id){ 10 //String url="http://localhost:8001/provider/user/"+id; 11 String url="http://USER-PROVIDER/provider/user/"+id; 12 return restTemplate.getForObject(url, User.class); 13 } 14 }
方法二:feign
2.1建立maven模塊 springcloud_consumer_9002
2.2.配置pom.xml
1 <dependencies> 2 <!--公共代碼依賴--> 3 <dependency> 4 <groupId>cn.su</groupId> 5 <artifactId>user_interface</artifactId> 6 <version>1.0-SNAPSHOT</version> 7 </dependency> 8 9 <!--springboot支持--> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 <dependency> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-test</artifactId> 17 </dependency> 18 19 <!--eureka客戶端,服務消費者也要從註冊中心獲取可用服務列表--> 20 <dependency> 21 <groupId>org.springframework.cloud</groupId> 22 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 23 </dependency> 24 25 <!--feign的支持--> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-starter-openfeign</artifactId> 29 </dependency> 30 </dependencies>
2.3.配置application.yml
1 server: 2 port: 9002 3 spring: 4 application: 5 name: USER_CONSUMER 6 eureka: 7 client: 8 registerWithEureka: false #不註冊到Eureka,不在註冊中心顯示 9 service-url: 10 #defaultZone: http://localhost:7001/eureka 11 defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka
2.4配置接口 UserInterface實現負載均衡
1 @FeignClient(value = "USER-PROVIDER") 2 @RequestMapping("/provider") 3 public interface UserInterface { 4 5 @RequestMapping("/user/{id}") 6 public User gertUser(@PathVariable("id") Long id); 7 }
2.5寫userController
1 @RestController 2 @RequestMapping("/consumer") 3 public class UserController { 4 5 @Autowired 6 private UserInterface userInterface; 7 8 @RequestMapping("/user/{id}") 9 public User getUser(@PathVariable("id") Long id){ 10 System.out.println(userInterface.getClass()); 11 return userInterface.gertUser(id); 12 } 13 }
2.6啓動項 注意須要掃描
1 @SpringBootApplication 2 @EnableFeignClients(basePackages ="cn.su.feign") 3 public class UserConsumerApp_9002 { 4 public static void main(String[] args) { 5 SpringApplication.run(UserConsumerApp_9002.class); 6 } 7 }