覆盖功能区服务器列表以从领事那里获取主机名列表

时间:2018-09-14 19:11:38

标签: java spring-boot spring-cloud spring-cloud-netflix netflix-ribbon

我正在尝试覆盖功能区服务器列表以从领事那里获取主机名列表。我让领事工作正常(使用硬编码值进行测试时)以获取服务的主机名和端口。我遇到的问题是尝试在IClientConfig中自动装配时。我收到一个异常,找不到IClientConfig bean。如何覆盖Ribbon配置并自动连接RibbonServerList方法中的IClientConfig。

我尝试按照http://projects.spring.io/spring-cloud/spring-cloud.html#_customizing_the_ribbon_client此处的说明进行操作,以了解如何自定义功能区客户端配置。我不断收到以下错误: 描述: com.intradiem.enterprise.keycloak.config.ConsulRibbonSSLConfig中的ribbonServerList方法的参数0需要一个类型为“ com.netflix.client.config.IClientConfig”的bean。 这导致spring-boot失败。 贝娄是我试图用来创建的类 自动配置类:

@Configuration
@EnableConfigurationProperties
@ConditionalOnBean(SpringClientFactory.class)
@ConditionalOnProperty(value = "spring.cloud.com.intradiem.service.apirouter.consul.ribbon.enabled", matchIfMissing = true)
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = ConsulRibbonSSLConfig.class)
//@RibbonClient(name = "question-answer-provider", configuration = ConsulRibbonSSLConfig.class)
public class ConsulRibbonSSLAutoConfig
{
}

配置类:

@Component
public class ConsulRibbonSSLConfig
{
@Autowired
private ConsulClient client;

private String serviceId = "client";

public ConsulRibbonSSLConfig() {
}

public ConsulRibbonSSLConfig(String serviceId) {
    this.serviceId = serviceId;
}

@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig clientConfig) {
    ConsulSSLServerList serverList = new ConsulSSLServerList(client);
    serverList.initWithNiwsConfig(clientConfig);
    return serverList;
}
}

ServerList代码:

public class ConsulSSLServerList extends AbstractServerList<Server>
{
private final ConsulClient client;

private String serviceId = "client";

public ConsulSSLServerList(ConsulClient client) {
    this.client = client;
}

@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    this.serviceId = clientConfig.getClientName();
}

@Override
public List<Server> getInitialListOfServers() {
    return getServers();
}

@Override
public List<Server> getUpdatedListOfServers() {
    return getServers();
}

private List<Server> getServers() {
    List<Server> servers = new ArrayList<>();
    Response<QueryExecution> results = client.executePreparedQuery(serviceId, QueryParams.DEFAULT);
    List<QueryNode> nodes = results.getValue().getNodes();
    for (QueryNode queryNode : nodes) {
        QueryNode.Node node = queryNode.getNode();
        servers.add(new Server(node.getMeta().containsKey("secure") ? "https" : "http", node.getNode(), queryNode.getService().getPort()));
    }
    return servers;
}

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder("ConsulSSLServerList{");
    sb.append("serviceId='").append(serviceId).append('\'');
    sb.append('}');
    return sb.toString();
}
}

0 个答案:

没有答案