简体   繁体   中英

Access microservice using Spring Boot

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

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

But in short, since you're using Spring Cloud (ie @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 (eg via @RestController and @RequestMapping )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM