简体   繁体   English

Spring Eureka LoadBalanced RestTemplate 未连接时

[英]Spring Eureka LoadBalanced RestTemplate when not connected

Here are my dependencies:这是我的依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

When my Spring Boot application is registered with Eureka, I can define a RestTemplate bean like this:当我的 Spring Boot 应用程序注册到 Eureka 时,我可以像这样定义一个RestTemplate bean:

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
  return new RestTemplate();
}

And in my services I can make requests to other services using their registered spring.application.name :在我的服务中,我可以使用他们注册的spring.application.name向其他服务发出请求:

restTemplate.getForEntity("http://application1/test", String.class);

How do I define where http://application1/ is located with Eureka disabled?如何在禁用 Eureka 的情况下定义http://application1/的位置?

eureka.client.enabled=false

Current Implementation test:当前实施测试:

@Configuration
public class RibbonConfig {

  @Bean
  public ServerList<Server> serverServerList() {
    return new ConfigurationBasedServerList();
  }
}

@Configuration
public class WebConfig {
  @Bean
  @LoadBalanced
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
}

@Component
public class TestService implements CommandLineRunner {
  @Autowired
  private RestTemplate restTemplate;

  @Override
  public void run(String... args) throws Exception {
    ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://application1/test", String.class);

    System.out.println(responseEntity);
  }
}

@RibbonClient(value = "application1", configuration = RibbonConfig.class)
@SpringBootApplication
public class Demo5Application {

  public static void main(String[] args) {
    SpringApplication.run(Demo5Application.class, args);
  }
}

bootstrap.yml引导程序.yml

eureka:
  client:
    enabled: false

application1:
  ribbon:
    list-of-servers: http://localhost:8081/

you can define a new RibbonClient configuration for your service this way:您可以通过这种方式为您的服务定义一个新的 RibbonClient 配置:

@Configuration
@RibbonClient(name = "application1", configuration = Application1RibbonClientConfiguration.class)
public class Application {
...
}

class Application1RibbonClientConfiguration{
    @Bean
    public ServerList<Server> ribbonServerList() {
        return new ConfigurationBasedServerList(); 
    }
}

you can skip all the configuration above if you don't have eureka on classPath如果您在 classPath 上没有 eureka,则可以跳过上面的所有配置

next in your properties file you can list all the servers location like this:接下来在您的属性文件中,您可以列出所有服务器的位置,如下所示:

application1.ribbon.listOfServers=..,..,..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring RestTemplate无法解析尤里卡名称 - spring RestTemplate not resolving eureka names Spring Ribbon @LoadBalanced不适用于restTemplate的@scope(“ prototype”) - Spring Ribbon @LoadBalanced doesn't work with @scope(“prototype”) for restTemplate Spring Ribbon RestTemplate - 混合 Eureka 和真实 URL - Spring Ribbon RestTemplate - Mix Eureka and Real URLs 无法注入LoadBalanceed带注释的OAuth2RestTemplate - Cannot inject LoadBalanced annotated OAuth2RestTemplate 当包含Eureka时,自动装配RestTemplate会导致“参数类型不匹配” - Autowiring RestTemplate causes “argument type mismatch” when including Eureka Spring Cloud LoadBalanced userInfoUri问题 - Spring cloud loadBalanced userInfoUri problems 连接到 Office VPN 的笔记本电脑。 无法连接到 spring eureka 服务 - Laptop connected to Office VPN. Unable to connect to spring eureka services 使用 RestTemplate 的尤里卡客户端的 UnknownHostException - UnknownHostException for Eureka Client using RestTemplate 发布时Spring RestTemplate消息转换器优先级 - Spring RestTemplate message converter priority when posting 使用Spring时获取HttpMessageNotReadableException异常-RestTemplate - Get HttpMessageNotReadableException Exception when using Spring - RestTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM