Controller:
package com.example.nacosdiscoverycloudprovider.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class NacosController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping("/echo/app-name")
public String echoAppName() {
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-producer");
String url = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
System.out.println("request url:"+url);
return restTemplate.getForObject(url,String.class);
}
}
报错:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field loadBalancerClient in com.example.nacosdiscoverycloudprovider.controller.NacosController required a bean of type 'org.springframework.cloud.client.loadbalancer.LoadBalancerClient' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.cloud.client.loadbalancer.LoadBalancerClient' in your configuration.
可以问题是我是完全官方文档写的,为啥会报错呢?
官方文档:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery
按我理解,是IOC容器里找不到LoadBalancerClient的实现类,我按ctrl+alt+B,也显示没有这个类的实现类。那应该如何解决,手动实现这个接口吗?