0

I have to implement my project as microservice arch. For that I am doing one sample app using Spring Boot of adding two no. I have three services. Here is my registration-server.yml.Similarly I have account-server.yml and user-service.yml. I want to call add() using UserService.java without RMI concept, since I am using Spring Boot. Also I don't want REST call since it will be costly for my project. How can I manually write code for lookup() in UserService so that it can call Adder?

@EnableAutoConfiguration
@EnableDiscoveryClient
public class AddService {

public static int add(int x,int y){
    int z=x+y;
    System.out.println("The sum of no. is "+z);
    return z;
}

public static void main(String[] args) {
    System.setProperty("spring.config.name", "add-service");
    SpringApplication.run(AddService.class, args);
}

@SpringBootApplication
@EnableEurekaServer
public class RegistrationService {

public static void main(String[] args) {
    // Tell server to look for registration.properties or registration.yml
    System.setProperty("spring.config.name", "registration-service");

    SpringApplication.run(RegistrationService.class, args);
}


@SpringBootApplication
@EnableDiscoveryClient
public class UserService {
public static void main(String[] args) {

    System.setProperty("spring.config.name", "registration-service");

    SpringApplication.run(UserService.class, args);
}




    eureka:
   instance:
    hostname: localhost
    client:  # Not a client, don't register with yourself
    registerWithEureka: false
    fetchRegistry: false

  server:
  port: 1111   # HTTP (Tomcat) port
10
  • 2
    If you don't want to use any remoting technology then don't use micro services. With eureka you get an ip/port for the service and yo need to call it somehow. Either by using Spring Remoting (RMI, HTTP etc) or by using REST. If that isn't what you want then micro services aren't for you. Commented May 26, 2016 at 10:25
  • since i am getting location details from eureka, I can call it.But its not necessary i should create remote obj as in case of rmi.That remote obj calls internal lookup() method of rmi.Cant i create my own lookup() and call it? Commented May 26, 2016 at 10:34
  • Have you actually read the spring remoting chapter? Judging from what you explain here you haven't... Commented May 26, 2016 at 10:39
  • Actually I havent.I have gone through spring boot.Can i use spring remoting along with spring boot and eureka registry ? Commented May 26, 2016 at 10:42
  • Check the spring reference guide... Commented May 26, 2016 at 10:44

1 Answer 1

1

I'd say that the Spring Cloud guide on this is the best starting point.

But in short, since you're using Spring Cloud (i.e. @EnableDiscoveryClient), I'd personally use Spring Cloud's feign client support to carry out the call. This will do the actual discovery service (eureka) lookup and HTTP calls for you.

Firstly you'll need the @EnableFeignClients annotation on your config class, and the following dependency (assuming Maven):

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

Then within your user service project, you can add the following interface:

@FeignClient("add-service")
public interface AddServiceClient {

    @RequestMapping(method = RequestMethod.POST, value = "/add/{x}/{y}", consumes="application/json")
    int addNumbers(@PathVariable("x") int x, @PathVariable("y") int y);

}

That's basically it really. You can then autowire AddServiceClient and use it:

@Autowired
private AddServiceClient addServiceClient;

void someMethod() {
    addServiceClient.addNumbers(2, 4);
}

This assumes that you expose /add/{x}/{y} as a POST endpoint within your add service (e.g. via @RestController and @RequestMapping)

EDIT: Sorry, I just seen where you said REST would be costly. Why do you think that? :)

Sign up to request clarification or add additional context in comments.

3 Comments

Its my proj requirement to use REST as last option.I dont know the exact reason so i am trying to acheive this without REST.With REST its very easy though by playing with annotations
Ok..After discussion with my team,it concluded since in REST call,everytime new connection is created so service has to be accessed everytime with new instance.I have to use spring boot,eureka server and http protocol wthout REST for accessing service.
Regarding "everytime new connection is created so service has to be accessed everytime with new instance" - uhm, not sure if I've misunderstood although all the Spring beans (e.g. services) are singletons by default, so nothing new would need to be created? I'd personally think that remote objects and/or rmi would actually be more resource heavy than a fairly light-weight REST process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.