1
votes

I am following tutorial Spring Cloud Service Discovery with Netflix Eureka from link : https://howtodoinjava.com/spring/spring-cloud/spring-cloud-service-discovery-netflix-eureka/.

In the source code I simply used spring-boot-starter-parent version 1.5.13.BUILD-SNAPSHOT instead of 1.5.4.RELEASE, no other changes.

I was trying to call spring-eureka-client-student-service, using the http://localhost:8098/getStudentDetailsForSchool/abcschool, but I dont see any logs are getting generated, it looks like no call is going to service.

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

application.properties

server.port=8098
spring.application.name=student-service
management.security.enabled=false

logging.level.com.example.*=DEBUG

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.lease-renewal-interval-in-seconds=1
eureka.instance.lease-expiration-duration-in-seconds=2

StudentServiceController.java

@RestController
public class StudentServiceController {
    private static Map<String, List<Student>> schooDB = new HashMap<String, List<Student>>();

    static {
        schooDB = new HashMap<String, List<Student>>();

        List<Student> lst = new ArrayList<Student>();
        Student std = new Student("Sajal", "Class IV");
        lst.add(std);
        std = new Student("Lokesh", "Class V");
        lst.add(std);

        schooDB.put("abcschool", lst);

        lst = new ArrayList<Student>();
        std = new Student("Kajal", "Class III");
        lst.add(std);
        std = new Student("Sukesh", "Class VI");
        lst.add(std);

        schooDB.put("xyzschool", lst);
    }

    @RequestMapping(value = "/getStudentDetailsForSchool/{schoolname}", method = RequestMethod.GET)
    public List<Student> getStudents(@PathVariable String schoolname){
        System.out.println("Getting Student details for " + schoolname);
        List<Student> studentList = schooDB.get(schoolname);

        if (studentList == null) {
            studentList = new ArrayList<Student>();
            Student std = new Student("Not Found", "N/A");
            studentList.add(std);
        }
        return studentList;
    }
}

SpringEurekaClientStudentServiceApplication.java

@SpringBootApplication
@EnableEurekaClient
public class SpringEurekaClientStudentServiceApplication {

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

enter image description here

1
Can you post your main class? Also whats the package of main class and StudentServiceController?Shanu Gupta
@ShanuGupta -Added all required detailsJeff Cook

1 Answers

1
votes

Please move SpringEurekaClientStudentServiceApplication in package com.howtodoitinjava instead of com.howtodoitinjava.example

  • If your other packages hierarchies are below your main app with the @SpringBootApplication annotation, you’re covered by implicit components scan.

  • If there are beans/components in other packages which are not sub
    packages of the main package, you should manually add them as
    @ComponentScan

In you case you could write something like this (if you want not to move your main class):

 @ComponentScan({“com.howtodoitinjava”})