以下是一个简化的Spring Cloud构建微服务的示例项目结构,假设我们有一个服务注册与发现的Eureka Server和一个客户端服务,使用Feign进行远程调用。
microservices-demo/
|-- eureka-server/
|   |-- src/
|   |   |-- main/
|   |   |   |-- java/
|   |   |   |   |-- com.example.eurekaserver/
|   |   |   |       |-- EurekaServerApplication.java
|   |   |   |-- resources/
|   |   |         |-- application.properties
|   |   |-- assembly/
|   |         |-- bin/
|   |              |-- start-eureka-server.sh
|   |-- pom.xml
|   
|-- service-client/
|   |-- src/
|   |   |-- main/
|   |   |   |-- java/
|   |   |   |   |-- com.example.serviceclient/
|   |   |   |       |-- ServiceClientApplication.java
|   |   |   |       |-- controller/
|   |   |   |       |    |-- ClientController.java
|   |   |   |       |-- service/
|   |   |   |            |-- ClientService.java
|   |   |   |-- resources/
|   |   |         |-- application.properties
|   |   |-- assembly/
|   |         |-- bin/
|   |              |-- start-service-client.sh
|   |-- pom.xml
|   
|-- pom.xml
在这个结构中,我们有两个模块:eureka-server和service-client。每个模块都有自己的src/main、src/assembly和pom.xml。src/main包含了源代码和资源文件,src/assembly包含了启动脚本。
eureka-server/EurekaServerApplication.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
service-client/ServiceClientApplication.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
 
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ServiceClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceClientApplication.class, args);
    }
}
service-client/ClientController.java:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestContr