【问题标题】:Access microservice using Spring Boot使用 Spring Boot 访问微服务
【发布时间】:2016-10-08 04:18:56
【问题描述】:

我必须将我的项目实现为微服务架构。为此,我正在使用添加两个 no 的 Spring Boot 做一个示例应用程序。我有三项服务。这是我的registration-server.yml。同样我有account-server.ymluser-service.yml。我想在没有 RMI 概念的情况下使用 UserService.java 调用 add(),因为我使用的是 Spring Boot。我也不想要 REST 调用,因为这对我的项目来说代价高昂。如何在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

【问题讨论】:

  • 如果你不想使用任何远程技术,那么就不要使用微服务。使用 eureka,您可以获得服务的 ip/port,并且您需要以某种方式调用它。通过使用 Spring Remoting(RMI、HTTP 等)或使用 REST。如果这不是您想要的,那么微服务不适合您。
  • 因为我从 eureka 获取位置详细信息,我可以调用它。但它没有必要像 rmi 一样创建远程 obj。远程 obj 调用 rmi.Cant 的内部查找()方法我创建自己的lookup() 并调用它?
  • 你真的读过spring remoting一章吗?从你在这里解释的情况来看,你没有......
  • 其实我还没有。我已经通过了spring boot。我可以使用spring remoting以及spring boot和eureka registry吗?
  • 查看弹簧参考指南...

标签: java spring spring-boot microservices


【解决方案1】:

我会说Spring Cloud guide on this 是最好的起点。

但简而言之,由于您使用的是 Spring Cloud(即@EnableDiscoveryClient),我个人会使用 Spring Cloud 的 feign 客户端支持来执行调用。这将为您执行实际的发现服务 (eureka) 查找和 HTTP 调用。

首先,您需要在配置类中添加 @EnableFeignClients 注释,以及以下依赖项(假设是 Maven):

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

然后在你的用户服务项目中,可以添加如下接口:

@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);

}

基本上就是这样。然后您可以自动连接AddServiceClient 并使用它:

@Autowired
private AddServiceClient addServiceClient;

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

这假设您在添加服务中公开 /add/{x}/{y} 作为 POST 端点(例如,通过 @RestController@RequestMapping

编辑:对不起,我刚刚看到你说 REST 会很昂贵。为什么你这么想? :)

【讨论】:

  • 我的项目要求使用 REST 作为最后一个选项。我不知道确切的原因,所以我试图在没有 REST 的情况下实现这一点。使用 REST 虽然通过使用注释来实现它很容易
  • 好的..在与我的团队讨论后,它得出结论,因为在 REST 调用中,每次创建新连接,因此每次都必须使用新实例访问服务。我必须使用 spring boot、eureka 服务器和用于访问服务的没有 REST 的 http 协议。
  • 关于“每次创建新连接,因此每次都必须使用新实例访问服务” - 嗯,虽然所有 Spring bean(例如服务)默认情况下都是单例,但不确定我是否误解了,所以不需要创造新的东西吗?我个人认为远程对象和/或 rmi 实际上会比相当轻量级的 REST 进程更占用资源。
猜你喜欢
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 2018-04-06
  • 2019-03-22
  • 2018-03-18
  • 2020-03-11
相关资源
最近更新 更多