使用Idea简单搭建springcloud项目

蔚落 2023-01-02 03:22 462阅读 0赞

前言:
开发工具:IntelliJ IDEA 2020版 (Ultimate Edition)
框架:spring boot 、spring cloud
搭建一套spring cloud微服务系统,实现服务之间的调用。
需要搭建一个父工程springcloud-test,一个服务注册中心eureka-server,两个微服务cloud-client,cloud-provider。
两个微服务均注册到服务注册中心。
一.搭建父项目
在这里插入图片描述

  1. 在这里插入图片描述
  2. 在这里插入图片描述

(1)删掉src目录
在这里插入图片描述
(2)定义pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.4.1</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.chen.test</groupId>
  13. <artifactId>springcloud-test</artifactId>
  14. <version>1.0-SNAPSHOT</version>
  15. <packaging>pom</packaging>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. </project>

二.搭建eureka-server注册中心

  1. 在这里插入图片描述
  2. 在这里插入图片描述
  3. 在这里插入图片描述
  4. 在这里插入图片描述
  5. 在这里插入图片描述
  6. (1)定义pom.xml文件

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0



    com.chen.test
    springcloud-test
    1.0-SNAPSHOT


    com.chen.test
    eureka-server
    0.0.1-SNAPSHOT
    jar
    eureka-server
    Demo project for Spring Boot


    UTF-8
    UTF-8
    1.8
    2020.0.0




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.cloud
    spring-cloud-dependencies
    ${ spring-cloud.version}
    pom
    import







    org.springframework.boot
    spring-boot-maven-plugin






    spring-milestones
    Spring Milestones
    https://repo.spring.io/milestone



(2)删掉test文件夹(自己设置,可有可无)
(3)启动加注解@EnableEurekaServer(开启eureka服务)

  1. package com.chen.test.eurekaserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class EurekaServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaServerApplication.class, args);
  10. }
  11. }

(4)定义配置文件
配置文件改为application.yml(可有可无,看自己喜好)

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. #应用名称(在注册中显示的)
  6. name: eureka-server
  7. eureka:
  8. client:
  9. #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
  10. fetch-registry: false
  11. #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
  12. register-with-eureka: true
  13. #与Eureka注册服务中心的通信zone和url地址
  14. serviceUrl:
  15. #http://localhost:8080/eureka/eureka
  16. defaultZone: http://${ eureka.instance.hostname}:${ server.port}/eureka
  17. #服务注册中心实例的主机名
  18. instance:
  19. hostname: 127.0.0.1
  20. prefer-ip-address: true
  21. instance-id: 127.0.0.1:8080
  22. server:
  23. #设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
  24. #会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
  25. #实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
  26. enable-self-preservation: false
  27. #扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
  28. eviction-interval-timer-in-ms: 10000

(5)测试
在这里插入图片描述
三.搭建提供者服务

  1. 在这里插入图片描述
  2. 在这里插入图片描述
  3. 在这里插入图片描述
  4. 在这里插入图片描述
  5. 在这里插入图片描述
  6. (1)定义pom.xml文件

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0


    com.chen.test
    springcloud-test
    1.0-SNAPSHOT


    com.chen.test
    cloud-provider
    0.0.1-SNAPSHOT
    jar
    cloud-provider
    Demo project for Spring Boot


    UTF-8
    UTF-8
    1.8
    2020.0.0




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.cloud
    spring-cloud-dependencies
    ${ spring-cloud.version}
    pom
    import







    org.springframework.boot
    spring-boot-maven-plugin






    spring-milestones
    Spring Milestones
    https://repo.spring.io/milestone



(2)删除test文件加(可有可无)
(3)修改配置文件为application.yml(可有可无)

  1. spring:
  2. application:
  3. name: cloud-provider
  4. server:
  5. port: 8081
  6. eureka:
  7. client:
  8. #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
  9. fetch-registry: false
  10. #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
  11. register-with-eureka: true
  12. service-url:
  13. #defaultZone 这个是不会提示的,此处需要自己写
  14. #实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
  15. #但是自己还需要写一个defaultZone;
  16. defaultZone: http://localhost:8080/eureka
  17. #服务注册中心实例的主机名
  18. instance:
  19. hostname: 127.0.0.1
  20. prefer-ip-address: true
  21. instance-id: 127.0.0.1:8081

(4)启动类加注解@EnableEurekaClient

  1. package com.chen.test.cloudprovider;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @SpringBootApplication
  6. @EnableEurekaClient
  7. public class CloudProviderApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(CloudProviderApplication.class, args);
  10. }
  11. }

(5)测试
在这里插入图片描述
四.搭建消费者服务

  1. 在这里插入图片描述
  2. 在这里插入图片描述
  3. 在这里插入图片描述
  4. 在这里插入图片描述
  5. 在这里插入图片描述
    (1)定义pom.xml文件

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0


    com.chen.test
    springcloud-test
    1.0-SNAPSHOT


    com.chen.demo
    cloud-client
    0.0.1-SNAPSHOT
    jar
    cloud-client
    Demo project for Spring Boot


    UTF-8
    UTF-8
    1.8
    2020.0.0




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.cloud
    spring-cloud-starter-openfeign



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.cloud
    spring-cloud-dependencies
    ${ spring-cloud.version}
    pom
    import







    org.springframework.boot
    spring-boot-maven-plugin






    spring-milestones
    Spring Milestones
    https://repo.spring.io/milestone



(2)删除test文件夹(可有可无)
(3)修改配置文件为application.yml(可有可无)

  1. server:
  2. #定义端口号
  3. port: 8082
  4. spring:
  5. application:
  6. #定义应用名称,即服务名称
  7. name: cloud-client
  8. eureka:
  9. client:
  10. service-url:
  11. defaultZone: http://localhost:8080/eureka
  12. #服务注册中心实例的主机名
  13. instance:
  14. hostname: 127.0.0.1
  15. prefer-ip-address: true
  16. instance-id: 127.0.0.1:8082

(4)启动类加注解@EnableEurekaClient

  1. package com.chen.demo.cloudclient;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @SpringBootApplication
  6. @EnableEurekaClient
  7. public class CloudClientApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(CloudClientApplication.class, args);
  10. }
  11. }

(5)测试
在这里插入图片描述
(6)在父pom中加上

  1. <modules>
  2. <module>eureka-server</module>
  3. <module>cloud-provider</module>
  4. <module>cloud-client</module>
  5. </modules>

在这里插入图片描述

五.实现服务之间的调用
1.在cloud-provider中创建controller包和service包

  1. package com.chen.test.cloudprovider.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.chen.test.cloudprovider.service.HelloService;
  7. @RestController
  8. @RequestMapping("/hello")
  9. public class HelloController {
  10. @Autowired
  11. private HelloService helloService;
  12. @GetMapping("/getHello")
  13. public String getHello(){
  14. return helloService.getHello();
  15. }
  16. }
  17. package com.chen.test.cloudprovider.service;
  18. public interface HelloService {
  19. String getHello();
  20. }
  21. package com.chen.test.cloudprovider.service.impl;
  22. import com.chen.test.cloudprovider.service.HelloService;
  23. import org.springframework.stereotype.Service;
  24. @Service
  25. public class HelloServiceImpl implements HelloService {
  26. @Override
  27. public String getHello() {
  28. return "你好兄弟";
  29. }
  30. }

(2)测试
在这里插入图片描述
(3)在cloud-client中创建controller包和service包

  1. package com.chen.demo.cloudclient.controller;
  2. import com.chen.demo.cloudclient.service.HelloService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import javax.annotation.Resource;
  8. @RestController
  9. @RequestMapping("/Hello")
  10. public class HelloClient {
  11. @Autowired
  12. private HelloService helloService;
  13. @GetMapping("/getClient")
  14. public String getClient(){
  15. return helloService.getProduct();
  16. }
  17. }
  18. package com.chen.demo.cloudclient.service;
  19. import org.springframework.cloud.openfeign.FeignClient;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. //name 为product项目中application.yml配置文件中的application.name;
  23. //path 为product项目中application.yml配置文件中的context.path;
  24. @FeignClient(name = "cloud-provider",path ="/hello" )
  25. //@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
  26. @Component
  27. public interface HelloService {
  28. @RequestMapping(value = "getHello")
  29. String getProduct();
  30. }

特别注意
在启动类加上注解@EnableFeignClients

  1. package com.chen.demo.cloudclient;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication
  7. @EnableEurekaClient
  8. @EnableFeignClients
  9. public class CloudClientApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(CloudClientApplication.class, args);
  12. }
  13. }

(4)测试
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,462人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IDEASpringCloud项目

    SpringCloud简单搭建 在微服务的架构体系中,SpringBoot只能用于应用开发,而想要实现完整的微服务架构,还需要实现架构中的服务注册与发现,API网关和负载