Sudheej
Sudheej

Reputation: 2019

Issue executing Zuul proxy in Spring (bean type not found)

Spring Boot Starter : 2.1.4.RELEASE

i am trying to use spring-cloud-starter-netflix-zuul to setup service proxy in spring boot, however the jar throws the below exception during startup

***************************
APPLICATION FAILED TO START
***************************

Description:

Field server in org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.ServerProperties' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=false)


Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.web.ServerProperties' in your configuration.

Below is my Main

package com.xxx.serviceproxy;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Controller
@EnableZuulProxy
@SpringBootApplication
public class ServiceProxyApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ServiceProxyApplication.class).web(WebApplicationType.NONE).run(args);
    }

}

Upvotes: 0

Views: 4108

Answers (1)

Francesc Recio
Francesc Recio

Reputation: 2235

This happens because you are putting the annotation @EnableAutoConfiguration.

This annotation causes the class ZuulServerAutoConfiguration to be searched for.

You can see here:

https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/ZuulServerAutoConfiguration.java

which has an autowired from

@Autowired
protected ServerProperties server;

And in the header of the class you can see the comments:

// Make sure to get the ServerProperties from the same place as a normal web app would
// FIXME @Import(ServerPropertiesAutoConfiguration.class)
public class ZuulServerAutoConfiguration {

which means it's not auto-configuring.

If you remove the annotation @EnableAutoConfiguration, it will no longer look for zuul autoconfiguration and you can configure zuul paths and other features in your application.yml (or application.properties), example:

 zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users_service

Here the documentation to configure zuul:

https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html

Upvotes: 1

Related Questions