ribbon在有eureka的情況下, 可以不使用eureka, 挺簡單, 直接上代碼
application.xml
server:port: 7002 spring:# 設置eureka中注冊的名稱, 全小寫, 否則大小寫混雜出現問題application:name: microservice-consumer-movie-ribben-ymllogging:level:root: INFOorg.hibernate: INFOorg.hibernate.type.descriptor.sql.BasicBinder: TRACEorg.hibernate.type.descriptor.sql.BasicExtractor: TRACEcom.itmuch: DEBUG eureka:client:serviceUrl:defaultZone: http://wenbronk:abc@localhost:8761/eureka # 添加注冊中心中的ip代替主機名instance:prefer-ip-c: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}statusPageUrlPath: ${management.context-path}/infohealthCheckUrlPath: ${management.context-path}/health# 在ribbon中禁用eureka ribbon:eureka:enabled: false# 自定義ribbonclient, 不使用eureka, 使用這個配置 microservice-provider-user:ribbon:listOfServers: localhost:7901
2, MovieController中使用此方法測試:
@RequestMapping("/movie/{id}")public User findById(@PathVariable Long id) {ServiceInstance instance = this.loadBalancerClient.choose("microservice-provider-user");URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));System.out.println("111: " + instance.getServiceId() + ": " + instance.getHost() + ": " + instance.getPort());return null; // return restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);}
客戶端發起請求后 , 可看到日志里面只有 7901 端口在被訪問, 注釋掉yml中的最后2項配置, 就可以看到eureka默認的輪詢配置
?