【发布时间】:2016-10-08 04:18:56
【问题描述】:
我必须将我的项目实现为微服务架构。为此,我正在使用添加两个 no 的 Spring Boot 做一个示例应用程序。我有三项服务。这是我的registration-server.yml。同样我有account-server.yml 和user-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