You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

参考Spring Ribbon教程未定制,运行时出现NullPointerException求助

Troubleshooting the NullPointerException in Spring Ribbon Consumer

Hey there, let's figure out why you're hitting that NullPointerException in your ConsumerControllerClient.getEmployee() method. Since you're following the standard Spring Ribbon + Eureka setup without custom changes, here are the most common fixes to check step by step:

1. Verify Your RestTemplate Bean Has the @LoadBalanced Annotation

This is the #1 culprit for Ribbon-related null pointers. Ribbon relies on the @LoadBalanced annotation to wrap your RestTemplate with load-balancing capabilities. If you skip this, the RestTemplate won't be able to resolve service names from Eureka, and you'll get unexpected nulls when making calls.

Fix: Make sure your configuration class defines RestTemplate like this:

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

2. Double-Check the Employee-Producer Service Name in Your Consumer Calls

When using Ribbon, you need to call the producer using its spring.application.name value (not a hardcoded localhost URL). If you've misspelled the service name, Ribbon can't find the producer instance, leading to null responses or exceptions.

Example: If your producer's spring.application.name=employee-producer, your consumer's request URL should look like:

String url = "http://employee-producer/employee/get";
Employee employee = restTemplate.getForObject(url, Employee.class);

Confirm that the service name in your ConsumerControllerClient matches exactly what's set in the producer's application.properties.

3. Ensure the Employee-Producer Is Properly Registered with Eureka

Even if the producer runs without errors, it might not have registered with your Eureka Server. Let's verify:

  • Open your Eureka dashboard at http://localhost:8090
  • Look under Instances currently registered with Eureka — you should see employee-producer listed here.

If it's missing:

  • Fix the incomplete eureka.instance.instanceId in your producer's config. It looks like you cut off the value — it should be something like eureka.instance.instanceId=${spring.application.name}:${random.value}
  • Double-check the eureka.client.serviceUrl.defaultZone in the producer's config matches your Eureka Server's address (http://localhost:8090/eureka/)

4. Confirm Your Consumer Has the Right Dependencies

Missing Spring Cloud Ribbon or Eureka Client dependencies can prevent critical beans from initializing, leading to null injections.

For Maven: Ensure these are in your pom.xml:

<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Eureka Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- Ribbon -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
</dependencies>

5. Check the Consumer's Main Class Annotations

Your consumer's startup class needs to enable service discovery so it can talk to Eureka. Add either @EnableEurekaClient or @EnableDiscoveryClient to your main class:

@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

Start with the first two checks — they're the most likely causes. If those don't fix it, work through the rest one by one.

内容的提问来源于stack exchange,提问作者Jeff Cook

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:48:41