0

i'm a newbie and i'm developing a sample spring cloud gateway application and i'm trying to access my microservices via service name. Below i have added my dependencies. There is a similar question asked before in here but there is no accepted answer and the given answer and comments didn't work on me.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.6</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes</artifactId>
            <version>1.1.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
            <version>1.1.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            <version>4.0.2</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Then my application yaml file looks like below.

spring:
  application.name: gateway
  cloud:
    kubernetes:
      discovery:
        enabled: true
        all-namespaces: false
        include-not-ready-addresses: true
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true

server:
  port: 8088
logging:
  level:
    org.springframework.cloud.gateway: TRACE
    org.springframework.cloud.loadbalancer: TRACE
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      enabled: true
    info:
      enabled: true

I added the @EnableDiscoveryClient annotation like below in my main file.

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {

    @Autowired
    private DiscoveryClient discoveryClient;

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

    @GetMapping("/services")
    public List<String> services() {
        return this.discoveryClient.getServices();
    }
}

But when i try to clean install, it gives below error.

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-04-11 13:03:56.489 ERROR 8096 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'discoveryClientRouteDefinitionLocator' defined in class path resource [org/springframework/cloud/gateway/discovery/GatewayDiscoveryClientAutoConfiguration$ReactiveDiscoveryClientRouteDefinitionLocatorConfiguration.class]: Unsatisfied dependency expressed through method 'discoveryClientRouteDefinitionLocator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reactiveCompositeDiscoveryClient' defined in class path resource [org/springframework/cloud/client/discovery/composite/reactive/ReactiveCompositeDiscoveryClientAutoConfiguration.class]: Unsatisfied dependency expressed through method 'reactiveCompositeDiscoveryClient' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kubernetesReactiveDiscoveryClient' defined in class path resource [org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfiguration$Reactive.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.client.discovery.ReactiveDiscoveryClient]: Factory method 'kubernetesReactiveDiscoveryClient' threw exception; nested **exception is org.springframework.cloud.kubernetes.discovery.DiscoveryServerUrlInvalidException: spring.cloud.kubernetes.discovery-server-url must be specified and a valid URL.**
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.18.jar:5.3.18]

I saw there is a configuration like this -> kubernetes.discovery.discovery-server-url: "". But what i cant understand is what should i put as the server-url here. Please note - I'm using minikube to locally deploy my applications.

Dushan
  • 1,365
  • 20
  • 26

1 Answers1

0

You need spring cloud discovery server in your namespace. There is ready to run image distribution, you can run this image in your namespace.

It will provide service list within your namespace. It is a proxy server between your pod and k8s apis.

You can find this docker image at docker hub, here is the link: dockerhub