idea创建一个springboot多模块项目之springcloud简单项目测试

    xiaoxiao2025-01-18  70

    一、创建空maven项目

    1.第一步

    2.第二步

    3.第三步

    4.第四步

    5.第五步

    二、创建子项目demo-eureka

    1.第一步

    2.第二步

    3.第三步

    4.第四步

    5.第五步

    6.项目demo-service-member、项目demo-service-order跟上面的一样的步骤。

    7.最终的项目结构图

    三、项目详解

    1.项目demo-eureka

    1.1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-eureka</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- spring boot test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

    1.2.application.yml

    server: port: 8888 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    1.3.启动类

    @SpringBootApplication @EnableEurekaServer public class DemoEurekaApplication { public static void main(String[] args) { SpringApplication.run(DemoEurekaApplication.class, args); } }

    2.项目demo-service-member

    2.1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-service-member</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-service-member</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

    2.2.application.yml

    eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/ server: port: 8763 spring: application: name: demo-service-member

    2.3.controller

    @RestController public class MemberController { @Value("${server.port}") private String serverPort; private static int count = 0; @RequestMapping("/getMemberAll") public List<String> getMemberAll() { try{ Thread.sleep(3000); }catch (Exception e){ } count++; List<String> listUser = new ArrayList<String>(); listUser.add("zhangsan"); listUser.add("lisi"); listUser.add("cmx"); listUser.add("count"+count); System.out.println("count:" + count); listUser.add("serverPort:"+serverPort); return listUser; } @RequestMapping("/getMemberServiceApi") public String getMemberServiceApi(){ return "this is 会员服务工程"; } }

    2.4.启动类

    @SpringBootApplication @EnableEurekaClient public class DemoServiceMemberApplication { public static void main(String[] args) { SpringApplication.run(DemoServiceMemberApplication.class, args); } }

    3.项目demo-service-order

    3.1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-service-order</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-service-order</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

    3.2.application.yml

    eureka: client: serviceUrl: defaultZone: http://localhost:8888/eureka/ server: port: 8764 spring: application: name: demo-service-order

    3.3.controller

    @RestController public class OrderController { @Autowired private OrderMemberService orderMemberService; @RequestMapping("/getOrderUserAll") public List<String> getOrderUserAll() { System.out.println("订单服务开始调用会员服务"); return orderMemberService.getOrderUserAll(); } @RequestMapping("/getOrderServiceApi") public String getOrderServiceApi(){ return "this is order 服务工程"; } }

    3.4.service

    @Service public class OrderMemberService { @Autowired private RestTemplate restTemplate; public List<String> getOrderUserAll() { return restTemplate.getForObject("http://demo-service-member/getMemberAll", List.class); } }

    3.5.启动类

    @SpringBootApplication @EnableEurekaClient public class DemoServiceOrderApplication { public static void main(String[] args) { SpringApplication.run(DemoServiceOrderApplication.class, args); } @Bean //开启负载均衡 @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }

    四、Eureka注册中心

    五、启动三个项目

    六、结果测试

    七、结束

    共勉!!!

    最新回复(0)