1. 版本选择
cloud和boot版本到cloud官方网站查找
2. 技术升级选型
3. 服务注册中心
3.1 Eureka: 配置多个Eureka服务,相互注册,遵循CAP原则的AP
服务端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
启动类加注解: @EnableEurekaServer // 设置为服务注册中心 EurekaServer
配置yml文件
server:
port: 7001
# 单机版
#eureka:
# instance:
# hostname: localhost #eureka服务端的实例名字
# client:
# register-with-eureka: false #表识不向注册中心注册自己
# fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
# service-url:
# #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群版
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
# server:
## 关闭自我保护机制,保证不可用服务被及时踢除
# enable-self-preservation: false
# eviction-interval-timer-in-ms: 2000
消费端:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置文件yml
server:
port: 80
spring:
application:
name: cloud-order-service
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1
eureka:
client:
register-with-eureka: true #是否向注册中心注册自己
fetchRegistry: true #是否从注册中心抓取已有的注册信息 默认true,集群必须设置为true
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
#defaultZone: http://localhost:7001/eureka #单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
消费端启动类加注解:@EnableEurekaClient
3.2 Zookeeper:遵循CAP中的CP原则
服务器上直接安装Zookpeer服务,消费端直接引入包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
配置很简单
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.30.87:2181
消费端启动注册到zookeeper后可以到zookeeper的服务器上去查看zookeeper下面的服务
4. 服务调用
4.1 Ribbon: Ribbon主要是做负载均衡的,微服务调用用restTemplate,Ribbon是跟Eureka结合的。引入Eureka的eureka-client包后,点进去,你会发现。已经整合了Ribbon的包
首先添加restTemplate Bean的配置
@Configuration
public class ApplicationContextConfig {
// 配置bean 不然后面没法依赖注入,就像以前ssm整合时配置依赖注入一样,
// 需要在配置文件配置之后,代码中才可以依赖注入
// 当前文件就是spring的配置文件
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Controller里调用其他微服务时就可以直接用了
@Resource
private RestTemplate restTemplate;
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
restTemplate.postForObject(PAYMENT_URL+"接口路径", reqParam, ReturnResult.class);
如果我们的微服务提供者是集群。这样写的话,调用会报错。因为我们的微服务名CLOUD-PAYMENT-SERVICE下面会有多台服务。restTemplate调用这个微服务不知道找哪个。所以会报错。这时候我们要再上面的RestTemplate的Bean上加个Ribbon的注解@LoadBalanced
@Configuration
public class ApplicationContextConfig {
// 配置bean 不然后面没法依赖注入,就像以前ssm整合时配置依赖注入一样,
// 需要在配置文件配置之后,代码中才可以依赖注入
// 当前文件就是spring的配置文件
@Bean
@LoadBalanced //让这个RestTemplate在请求时拥有客户端负载均衡的能力
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
这个注解默认的是轮询的算法
如何修改成其他算法?
这里面主要有IRule接口,有7种实现,有轮询的,随机的等等
实现:
我们的启动类默认有个@CompantScan注解,是扫描包的。默认是当前启动类所在的包以及子包,我们要建个它扫描不到的包,所以要放在启动类所在包的上一层
然后建个配置类
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
// 此处将ribbon默认使用的轮询策略改为随机策略
return new RandomRule();
}
}
最后启动类上再加个注解: @RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MySelfRule.class) // 启动该服务时去加载自定义的ribbon配置
4.2 openFeign
很简单。首先引入feign的包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
这个包里也自动集成了Ribbon。
启动类添加注解: @EnableFeignClients // 启动feign
新建个接口
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeout();
}
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") 此注解就指明了这个service接口都是feign调用。指定下微服务名称
下面的方法就是微服务的接口,要保证mapping的value值和方法的参数值跟微服务里的接口一致就行。方法名不同没关系
Controller里直接调用这个service就行
public class OrderFeignController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
}
openFeign 调用默认超时时间为1s我们可以设置调用超时时间,以及日志打印记录
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下, 两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
logging:
level:
# feign日志以 debug 级别监控 com.atguigu.springcloud.service.PaymentFeignService 接口
com.atguigu.springcloud.service.PaymentFeignService: debug
5. 服务降级
5.1 Hystrix :
服务降级: 目标系统不可用了,需要给一个兜底的解决方法,例如,向调用方返回一个预期的,可处理的备选响应(fallback)
服务降级的场景:1. 程序运行异常
2. 超时
3. 服务熔断触发服务降级
4. 线程池打满
服务熔断: 就是保险丝,当服务达到最大访问时,直接拒绝访问,拉闸限电,然后调用服务降级的方法返回友好提示
限流: 秒杀,高并发操作,严禁一窝蜂涌入进来,大家排队,限制1s N个。有序进行
待补充