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
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
Application.java
- package com.itmuch.cloud;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- @SpringBootApplication
- <span style="color:#ff0000;">@EnableEurekaServer</span>
- public class EurekaApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaApplication.class, args);
- }
- }
- security:
- basic:
- enabled: true #验证
- user:
- name: user #用户名
- password: password123 #密码
- server:
- port: 8761 #端口号
- eureka:
- client:
- register-with-eureka: false #表示是否将自己注册到Eureka Server,默认为true
- fetch-registry: false #表示是否从eureka服务器获取注册信息
- service-url:
- defaultZone: http://user:[email protected]:8761/eureka #配置eureka服务地址
eureka client项目
pom.xml
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
application.yml
- server:
- port: 7900
- spring:
- jpa:
- generate-ddl: false
- show-sql: true
- hibernate:
- ddl-auto: none
- datasource:
- platform: h2
- schema: classpath:schema.sql
- data: classpath:data.sql
- application:
- name: microservice-provider-user # 指定应用的名称
- logging:
- level:
- root: INFO
- org.hibernate: INFO
- org.hibernate.type.descriptor.sql.BasicBinder: TRACE
- org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
- com.itmuch: DEBUG
- eureka:
- client:
- healthcheck:
- enabled: true #健康检查
- serviceUrl:
- defaultZone: http://user:[email protected]:8761/eureka
- instance:
- prefer-ip-address: true #表示使用IP进行配置
- instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
- metadata-map:
- zone: ABC # eureka可以理解的元数据
- lilizhou: BBC # 不会影响客户端行为
- lease-renewal-interval-in-seconds: 5
Application.java
- @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
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7801
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7800
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7801
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7800
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7801
- 111:microservice-provider-user:192.168.32.134:7901
- 222:microservice-provider-user2:192.168.32.134:7800
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7801
- 111:microservice-provider-user:192.168.32.134:7901
- 222:microservice-provider-user2:192.168.32.134:7800
- 111:microservice-provider-user:192.168.32.134:7901
- 222:microservice-provider-user2:192.168.32.134:7801
- 111:microservice-provider-user:192.168.32.134:7900
- 222:microservice-provider-user2:192.168.32.134:7800
- 111:microservice-provider-user:192.168.32.134:7900
- 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
- users:
- ribbon:
- NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
- NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
6.Ribbon脱离Eureka使用
application.yml
- spring:
- application:
- name: microservice-consumer-movie-ribbon
- microservice-provider-user:
- ribbon:
- listOfServers: localhost:7900
ribbon调用方: http://localhost:8010/movie/1
- ===:microservice-provider-user:localhost:7900
- ===:microservice-provider-user:localhost:7900
- ===:microservice-provider-user:localhost:7900
- ===:microservice-provider-user:localhost:7900
- ===:microservice-provider-user:localhost:7900
结果发现全部都是7900
7.Feign的简介及基础使用
Declarative REST Client:Feign声明试的RestClient
pom.xml
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-feign</artifactId>
- </dependency>
feign接口
- package com.itmuch.cloud.feign;
- import org.springframework.cloud.netflix.feign.FeignClient;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import com.itmuch.cloud.entity.User;
- @FeignClient("microservice-provider-user")
- public interface UserFeignClient {
- @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
- public User findById(@PathVariable("id") Long id); // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
- @RequestMapping(value = "/user", method = RequestMethod.POST)
- public User postUser(@RequestBody User user);
- // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。
- // 如勘误,请@lilizhou2008 [email protected]
- @RequestMapping(value = "/get-user", method = RequestMethod.GET)
- public User getUser(User user);
- }