简体   繁体   English

使用Spring Boot访问微服务

[英]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. 为此,我正在使用Spring Boot做一个示例应用程序,添加两个no。 I have three services. 我有三项服务。 Here is my registration-server.yml.Similarly I have account-server.yml and user-service.yml . 这是我的registration-server.yml。类似地,我有account-server.ymluser-service.yml I want to call add() using UserService.java without RMI concept, since I am using Spring Boot. 我想使用不带RMI概念的UserService.java调用add() ,因为我使用的是Spring Boot。 Also I don't want REST call since it will be costly for my project. 另外,我不希望进行REST调用,因为这对我的项目而言会很昂贵。 How can I manually write code for lookup() in UserService so that it can call Adder? 如何在UserService手动为lookup()写代码,以便它可以调用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. 我想说的是Spring Cloud指南是最好的起点。

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. 简而言之,由于您使用的是Spring Cloud(即@EnableDiscoveryClient ),因此我将亲自使用Spring Cloud的伪装客户支持来执行此呼叫。 This will do the actual discovery service (eureka) lookup and HTTP calls for you. 这将为您执行实际的发现服务(eureka)查找和HTTP调用。

Firstly you'll need the @EnableFeignClients annotation on your config class, and the following dependency (assuming Maven): 首先,您需要在配置类上使用@EnableFeignClients批注,并需要以下依赖项(假设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: 然后,您可以自动连接AddServiceClient并使用它:

@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 ) 假设您将/ add / {x} / {y}作为添加服务中的POST端点@RestController (例如,通过@RestController@RequestMapping

EDIT: Sorry, I just seen where you said REST would be costly. 编辑:对不起,我刚刚看到您说REST的成本很高。 Why do you think that? 为什么你这么想? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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