SpringCloud学习笔记
服务注册中心Eureka
Eureka配置
http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html
具体配置大佬的文章都会有,这里只做踩坑记录。
PS:版本不同,注解也会不同
@EnableEurekaServer
之前版本@EnableEurekaServer中会包含@EnableDiscoveryClient注解
@EnableEurekaServer注解详解 https://www.jianshu.com/p/3a3a1a5891ec
客户端配置:
@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
PS:软件包名更新
把
spring-cloud-starter-eureka
spring-cloud-starter-eureka-server
分别改为
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
PS 注册到eureka实例名称问题
application.yml配置
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${spring.application.instance_id:${server.port}}
配置之后可能无法正确显示 ,需要引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
eureka.instance.instance-id必须写在application.properties或application.yml中。不能写在bootstrap.properties和bootstrap.yml中。可以去了解下springboot加载默认配置文件的顺序,bootstrap是在application之前的。写在bootstrap里的话,自定义的instance-id并不会在eureka界面生效,原理没研究过。
Spring Cloud Config配置中心
坑一:配置发现
把配置中心作为服务注册到Eureka中心实现高可用后,客户端获取配置的配置可以从URL改为配置中心的application.name
service-id名称是config项目的application.name(即config项目eureka界面注册的名称),config.name是你git地址上配置的名称如。
选择的配置文件的名称。这里容易混淆。
坑二:Spring Cloud Bus消息总线自动刷新
Hystrix熔断器
熔断器参数
坑:feign的bug
package com.example.eurekaclient.web;
import com.example.eurekaclient.common.HelloRemote;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author : yy
* @date :Created in 19-8-21 下午2:16
* @description:
* @modified By:
* @version: $
*/
@RestController
public class HystrixTestController {
@Autowired
private HelloRemote HelloRemote;
private RestTemplate restTemplate = new RestTemplate();
@GetMapping("/hello")
public String hello(){
return HelloRemote.hello("66666");
}
@GetMapping("/sleep3s")
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
})
/**
* 使用feign熔断器会失效,即使配置了feign.hystrix.enabled= true 也不会生效,,,使用restTemplate就不会有问题
*/
public String sleep3s(){
return HelloRemote.sleep3s();
}
@GetMapping("/sleep3sRest")
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
})
public String sleep3s2(){
String result = restTemplate.getForObject("http://localhost:8081/sleep3s",String.class);
return result;
}
}