spring cloud学习2

章节统计

1.RestTemplate
2.eureka server,eureka client
3.ribbon的基本使用
4.ribbon自定义负载均衡

5.使用配置文件自定义 Ribbon Client
6.Ribbon脱离Eureka使用
7.Feign的简介及基础使用

 容错处理方法是:断路器,超时


单体架构缺点:
1.复杂性逐渐变高
2.技术债务增加
3.部署速度逐渐变慢
4.阻碍创新
5.无法按需伸缩
微服务:


特性:
1.每个微服务可独立运行在自己的进程里
2.一系列独立运行的服务共同构建起了整个系统
3.每个服务为独立开发,一个微服务一般完成某个特定的功能,比如:订单管理,用户管理等。
4.微服务之间通过一些轻量的通信机制进行通信,例如通过REST API或者RPC的方法进行调用。


微服务设计原则
1.单一职责原则
2.服务自制原则
3.轻量级通讯原则
4.接口明确原则


微服务开发框架浅谈

spring cloud 

dubbo 

dropwizard



Angle SR6
地名 Server release:bug修复


spring cloud特点
1.约定优于配置
2.开箱即用,快速启动
3.适用与各种环境
4.轻量级的组件
5.组件的支持很丰富,功能很齐全
6.选项中文


一个maven项目转成gradle项目,运行命令:maven -gradle gradle init -type pom



学习开发 适用的软件版本
1.jdk1.8
2.maven3.3.9
3.IDE( 1,Spring Tool Suite 3.8.2 
2,IDEA)
4.Spring Boot 1.4.1
5.Spring Cloud Camden SR1


04 服务提供者与服务消费者



生成项目,把项目导入eclipse下面


项目microservice-simple-provider-user是服务提供者,


知识点:

1.RestTemplate

org.springframework.web.client.RestTemplate

return restTemplate.getForObject("http://localhost:7900/simple/" + id, User.class);//调用服务提供者提供的链接,此处的链接可以优化 



2.eureka server,eureka client

eureka server项目

pom.xml

[html]  view plain  copy
  1. <dependency>  
  2.             <groupId>org.springframework.cloud</groupId>  
  3.             <artifactId>spring-cloud-starter-eureka-server</artifactId>  
  4.         </dependency>  

Application.java

[java]  view plain  copy
  1. package com.itmuch.cloud;  
  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. <span style="color:#ff0000;">@EnableEurekaServer</span>  
  9. public class EurekaApplication {  
  10.   public static void main(String[] args) {  
  11.     SpringApplication.run(EurekaApplication.class, args);  
  12.   }  
  13. }  


application.yml

[html]  view plain  copy
  1. security:  
  2.   basic:  
  3.     enabled: true #验证  
  4.   user:  
  5.     name: user #用户名  
  6.     password: password123 #密码  
  7. server:  
  8.   port: 8761 #端口号  
  9. eureka:  
  10.   client:  
  11.     register-with-eureka: false #表示是否将自己注册到Eureka Server,默认为true  
  12.     fetch-registry: false #表示是否从eureka服务器获取注册信息  
  13.     service-url:  
  14.       defaultZone: http://user:password123@localhost:8761/eureka #配置eureka服务地址  


eureka client项目

pom.xml

[html]  view plain  copy
  1. <dependency>  
  2.             <groupId>org.springframework.cloud</groupId>  
  3.             <artifactId>spring-cloud-starter-eureka</artifactId>  
  4.         </dependency>  

application.yml

[html]  view plain  copy
  1. server:  
  2.   port: 7900  
  3. spring:  
  4.   jpa:  
  5.     generate-ddl: false  
  6.     show-sql: true  
  7.     hibernate:  
  8.       ddl-auto: none  
  9.   datasource:  
  10.     platform: h2  
  11.     schema: classpath:schema.sql  
  12.     data: classpath:data.sql  
  13.   application:  
  14.     name: microservice-provider-user # 指定应用的名称  
  15. logging:  
  16.   level:  
  17.     root: INFO  
  18.     org.hibernate: INFO  
  19.     org.hibernate.type.descriptor.sql.BasicBinder: TRACE  
  20.     org.hibernate.type.descriptor.sql.BasicExtractor: TRACE  
  21.     com.itmuch: DEBUG  
  22. eureka:  
  23.   client:  
  24.     healthcheck:  
  25.       enabled: true #健康检查  
  26.     serviceUrl:  
  27.       defaultZone: http://user:password123@localhost:8761/eureka  
  28.   instance:  
  29.     prefer-ip-address: true #表示使用IP进行配置  
  30.     instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}  
  31.     metadata-map:  
  32.       zone: ABC      # eureka可以理解的元数据  
  33.       lilizhou: BBC  # 不会影响客户端行为  
  34.     lease-renewal-interval-in-seconds: 5  

Application.java

[plain]  view plain  copy
  1. @EnableEurekaClient  


3.ribbon的基本使用

ribbon 客户端的负载均衡

pom包 group org.springframework.cloud and artifact id spring-cloud-starter-netflix-ribbon,如果引入了eureka包,这个包就不用再引入了。



4.ribbon自定义负载均衡


ribbon调用默认是轮询算法。可以自定义

微博说明http://blog.csdn.net/qwlzxx/article/details/77118634




运行效果



访问地址

http://localhost:8010/test


[plain]  view plain  copy
  1. 111:microservice-provider-user:192.168.32.134:7900  
  2. 222:microservice-provider-user2:192.168.32.134:7801  
  3. 111:microservice-provider-user:192.168.32.134:7900  
  4. 222:microservice-provider-user2:192.168.32.134:7800  
  5. 111:microservice-provider-user:192.168.32.134:7900  
  6. 222:microservice-provider-user2:192.168.32.134:7801  
  7. 111:microservice-provider-user:192.168.32.134:7900  
  8. 222:microservice-provider-user2:192.168.32.134:7800  
  9. 111:microservice-provider-user:192.168.32.134:7900  
  10. 222:microservice-provider-user2:192.168.32.134:7801  
  11. 111:microservice-provider-user:192.168.32.134:7901  
  12. 222:microservice-provider-user2:192.168.32.134:7800  
  13. 111:microservice-provider-user:192.168.32.134:7900  
  14. 222:microservice-provider-user2:192.168.32.134:7801  
  15. 111:microservice-provider-user:192.168.32.134:7901  
  16. 222:microservice-provider-user2:192.168.32.134:7800  
  17. 111:microservice-provider-user:192.168.32.134:7901  
  18. 222:microservice-provider-user2:192.168.32.134:7801  
  19. 111:microservice-provider-user:192.168.32.134:7900  
  20. 222:microservice-provider-user2:192.168.32.134:7800  
  21. 111:microservice-provider-user:192.168.32.134:7900  
  22. 222:microservice-provider-user2:192.168.32.134:7801  

得出结果,

microservice-provider-user端口号是随机的,所以负载均衡算法是随机的

microservice-provider-user2端口号是轮询的,所以负载均衡算法是轮询的。


5. 使用配置文件自定义 Ribbon Client

Customizing the Ribbon Client using properties

application.yml

[plain]  view plain  copy
  1. users:  
  2.   ribbon:  
  3.     NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList  
  4.     NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule  


6.Ribbon脱离Eureka使用

application.yml

[plain]  view plain  copy
  1. spring:  
  2.   application:  
  3.     name: microservice-consumer-movie-ribbon  
  4. microservice-provider-user:  
  5.   ribbon:  
  6.     listOfServers: localhost:7900  



ribbon调用方: http://localhost:8010/movie/1

[plain]  view plain  copy
  1. ===:microservice-provider-user:localhost:7900  
  2. ===:microservice-provider-user:localhost:7900  
  3. ===:microservice-provider-user:localhost:7900  
  4. ===:microservice-provider-user:localhost:7900  
  5. ===:microservice-provider-user:localhost:7900  

结果发现全部都是7900

7.Feign的简介及基础使用

Declarative REST Client:Feign

声明试的RestClient

pom.xml

[plain]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.springframework.cloud</groupId>  
  3.     <artifactId>spring-cloud-starter-feign</artifactId>  
  4. </dependency>  

feign接口

[java]  view plain  copy
  1. package com.itmuch.cloud.feign;  
  2.   
  3. import org.springframework.cloud.netflix.feign.FeignClient;  
  4. import org.springframework.web.bind.annotation.PathVariable;  
  5. import org.springframework.web.bind.annotation.RequestBody;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestMethod;  
  8.   
  9. import com.itmuch.cloud.entity.User;  
  10.   
  11. @FeignClient("microservice-provider-user")  
  12. public interface UserFeignClient {  
  13.   @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)  
  14.   public User findById(@PathVariable("id") Long id); // 两个坑:1. @GetMapping不支持   2. @PathVariable得设置value  
  15.   
  16.   @RequestMapping(value = "/user", method = RequestMethod.POST)  
  17.   public User postUser(@RequestBody User user);  
  18.   
  19.   // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。  
  20.   // 如勘误,请@lilizhou2008  [email protected]  
  21.   @RequestMapping(value = "/get-user", method = RequestMethod.GET)  
  22.   public User getUser(User user);  
  23. }  



猜你喜欢

转载自blog.csdn.net/fulq1234/article/details/79246755