1、Eureka常見問
1.1、Eureka Enviroment 的配置
eureka.enviroment=product
參考 https://github.com/Netflix/eureka/wiki/Configuring-Eureka
1.2、Eureka Datacenter 的配置
eureka.datacenter=cloud
配置eureka.datacenter=cloud,這樣eureka就知道是在AWS雲上
1.3、Eureka 開啟自我保護的提示
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
Eureka進入保護模式時,不會踢出已關閉的節點。
1.4、Eureka 注冊服務慢的問題如何解決?
1.5、如何解決Eureka Server不踢出已關閉的節點的問題?
Server端
#關閉eureka的自我保護
eureka.server.enable-self-preservation=false
#清理間隔時間,單位為毫秒
eureka.server.eviction-interval-timer-in-ms=0 Client端
#開啟健康檢查(需要spring-boot-starter-actuator依賴)
eureka.client.healthcheck.enabled=true
#租期到期時間,默認90秒
eureka.instance.lease-expiration-duration-in-seconds=30
#租賃更新時間間隔,默認30,即30秒發送一次心跳
eureka.instance.lease-renewal-interval-in-seconds=10
注意:更改Eureka更新頻率將打破服務器的自我保護能力,生產環境中一般不推薦修改。
1.6、Eureka 配置instanceId顯示IP
eureka.client.serviceUrl.defaultZone=http://user:password@localhost:8761/eureka
#設置注冊ip eureka.instance.prefer-ip-address=true eureka.instance.instanceId=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
2、Ribbon
2.1、自定義配置時,@Configuration和@ComponentScan所在包不應該重疊
2.2、使用RestTemplate時,想要獲得一個List時,應該用數組,而不應該直接用List。
在microservice-provider-user項目中添加如下代碼,返回list
@GetMapping("/list-all") public List<User> listAll(){ List<User> list = new ArrayList<User>(); User user = new User(1,"zahngsan"); User user2 = new User(2,"zahngsan2"); User user3 = new User(3,"zahngsan3"); list.add(user); list.add(user2); list.add(user3); return list; }
直接訪問此項目,得到如下結果:
在microservice-consumer-movie-ribbon-list項目中通過RestTemplate訪問以上內容:
@GetMapping("/list-all") public List<User> list() { List<User> list = this.template.getForObject("http://microservice-provider-user:7900/list-all", List.class); for(User u:list){ System.out.println(u.getId()); } return list; }
報錯:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.wyl.microservicesimpleconsumemovie.entity.User
可看出RestTemplate得到的是LinkedHashMap對象,不是List對象。則通過以下代碼解析:
User[] list = this.template.getForObject("http://microservice-provider-user:7900/list-all", User[].class); for(User u:list){ System.out.println(u.getId()); }
3、Feign
3.1、自定義配置時,@Configuration和@Component所在包不應該重疊
3.2、@FeignClient所在的接口中,不支持@GetMapping等組合注解
3.3、使用@PathVariable時,需要制定其value
3.4、Feign暫時不支持復雜對象作為一個參數