1.eureka界面的environment和datacenter怎么改?
在主配置文件修改:
eureka.environment=trunk
eureka.datacenter=peer2cloud
发现界面更改了!
2.eureka界面的红框是它的自我保护模式?
自我保护模式的特性,快过期的实例进入保护模式,不再从实例列表中删除该实例。
关闭自我保护模式:
eureka.server.enable-self-preservation=false
3.解决eureka服务注册慢的问题
需要3个心跳时间,客户端和服务端的源数据才能一致,客户端才能发现服务
eureka.instance.leaseRenewalIntervalInSeconds=5,默认30秒
4.解决eureka不剔除失效节点的问题?
1.server端:配置关闭自我保护,并按需配置Eureka Server清理无效节点的时间间隔
关闭自我保护
加快清理间隔时间:
eureka.server.eviction-interval-timer-in-ms:30*1000,单位毫秒,默认60*1000(60秒)
2.客户端:
指示eureka服务器在接收到最后一个心跳之后等待的时间(秒),然后才能从此视图中删除此实例,并禁止此实例的流量 |
eureka.instance.lease-expiration-duration-in-seconds:90,单位秒,默认90秒删除无效实例
指示eureka客户端需要向eureka服务器发送心跳以指示它仍然存在的频率(以秒为单位)。如果在leaseExpirationDurationInSeconds中指定的时间段内未收到心跳线,则eureka服务器将从其视图中删除该实例,因此不允许此实例的流量。
eureka.instance.lease-renewal-interval-in-seconds:30,默认30秒,单位秒
注意90秒配置要大于30秒,是3倍关系
5.修改instance-id
Instance ID的默认值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
eureka.instance.instance-id=${spring.application.name}-${spring.cloud.client.ipAddress}:${server.port}
6.restTemplate获取list对象的坑
提供者:
@RequestMapping(value="/getList")
public List<Book> getList() {
List<Book> list = new ArrayList<Book>();
Book book = new Book(1, "zm", 2, new Date());
Book book2 = new Book(2, "zhangmin", 3, new Date());
list.add(book);
list.add(book2);
return list;
}
消费者:
@RequestMapping(value="/getList")
public List<Book> getList() {
Book[] bookArray = this.restTemplate.getForObject("http://HELLO-SERVICE/getList",Book[].class);
List<Book> list = Arrays.asList(bookArray);
System.out.println(JSONObject.toJSONString(list));
return list;
}
7.ribbon使用注意
在自定义配置时,
@Configuration和包扫描路径不重叠
8.fegin的坑
1.自定义配置时,@configuration和包扫描路径不重叠
2.@FeginClient所在接口中,不支持@GetMapping等组合注解,建议使用@Requestmapping
3.传递普通参数用@RequestParam(value="") 其中value注意写,否则会报错
4.使用复杂bean对象传递时,要么不指定post get方式,要么指定post,不支持get方式
5.错误写法示例,不支持多个@RequestBody对象,只能有一个,
Book getBookForObject(@RequestBody Book book,@RequestBody Pay pay);
但是可以这么写Book getBookForObject2(@RequestBody Book book,@RequestParam(value ="id") String id);
6.在遇到fegin请求404或者500的时候,需要检查以下情况
7.fegin第一次连接请求超时,报错:
spring cloud java.util.concurrent.TimeoutException
的解决方案:3种
# 解决第一次请求报超时异常的方案:
# hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持
8.注意fegin的契约,默认是springmvc契约,fegin契约下原来的注解不可用
@FeignClient(name="CONSUMER-COPY",configuration=Configuration2.class)
public interface FeignClient2 {
@RequestMapping(value = "/sayHello", method = RequestMethod.GET)
public String sayHello(@RequestParam(value="name") String name);
}
@FeignClient(name="HELLO-SERVICE",configuration=Configuration1.class)
public interface UserFeignClient2 {
@RequestLine("GET /sayHello/{name}")
public String sayHello(@Param(value = "name") String name);
}