springcloud-05-ribbon中不使用eureka

ribbon在有eureka的狀況下, 能夠不使用eureka, 挺簡單, 直接上代碼spring

application.xmlsql

server:
  port: 7002
spring:
  # 設置eureka中註冊的名稱, 全小寫, 不然大小寫混雜出現問題
  application:
    name: microservice-consumer-movie-ribben-yml

logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.itmuch: DEBUG
eureka:
  client:
    serviceUrl:
      defaultZone: http://wenbronk:abc@localhost:8761/eureka

  # 添加註冊中心中的ip代替主機名
  instance:
    prefer-ip-c: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
    statusPageUrlPath: ${management.context-path}/info
    healthCheckUrlPath: ${management.context-path}/health

# 在ribbon中禁用eureka
ribbon:
  eureka:
   enabled: false

# 自定義ribbonclient, 不使用eureka, 使用這個配置
microservice-provider-user:
  ribbon:
    listOfServers: localhost:7901

2, MovieController中使用此方法測試:app

 @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默認的輪詢配置ide

相關文章
相關標籤/搜索