使用Idea简单搭建springcloud项目

时间:2021-1-8 作者:admin

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

(1)删掉src目录
使用Idea简单搭建springcloud项目
(2)定义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>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.chen.test</groupId>
    <artifactId>springcloud-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
</project>

二.搭建eureka-server注册中心
1.
使用Idea简单搭建springcloud项目
2.
使用Idea简单搭建springcloud项目
3.
使用Idea简单搭建springcloud项目
4.
使用Idea简单搭建springcloud项目
5.
使用Idea简单搭建springcloud项目
6.
(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--引入父工程依赖-->
    <parent>
        <groupId>com.chen.test</groupId>
        <artifactId>springcloud-test</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.chen.test</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-server</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>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>
        </repository>
    </repositories>

</project>

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

package com.chen.test.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

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

server:
  port: 8080

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

(5)测试
使用Idea简单搭建springcloud项目
三.搭建提供者服务
1.
使用Idea简单搭建springcloud项目
2.
使用Idea简单搭建springcloud项目
3.
使用Idea简单搭建springcloud项目
4.
使用Idea简单搭建springcloud项目
5.
使用Idea简单搭建springcloud项目
6.
(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--引入父工程依赖-->
    <parent>
        <groupId>com.chen.test</groupId>
        <artifactId>springcloud-test</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <groupId>com.chen.test</groupId>
    <artifactId>cloud-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cloud-provider</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>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>${spring-cloud.version}</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>
        </repository>
    </repositories>

</project>

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

spring:
  application:
    name: cloud-provider
server:
  port: 8081

eureka:
  client:
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: false
    #实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true,即自己注册自己。
    register-with-eureka: true
    service-url:
      #defaultZone 这个是不会提示的,此处需要自己写
      #实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
      #但是自己还需要写一个defaultZone;
      defaultZone: http://localhost:8080/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: 127.0.0.1
    prefer-ip-address: true
    instance-id: 127.0.0.1:8081

(4)启动类加注解@EnableEurekaClient

package com.chen.test.cloudprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class CloudProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudProviderApplication.class, args);
    }

}

(5)测试
使用Idea简单搭建springcloud项目
四.搭建消费者服务
1.
使用Idea简单搭建springcloud项目
2.
使用Idea简单搭建springcloud项目
3.
使用Idea简单搭建springcloud项目
4.
使用Idea简单搭建springcloud项目
5.
使用Idea简单搭建springcloud项目
(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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--引入父工程依赖-->
    <parent>
        <groupId>com.chen.test</groupId>
        <artifactId>springcloud-test</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <groupId>com.chen.demo</groupId>
    <artifactId>cloud-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cloud-client</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>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</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>
        </repository>
    </repositories>

</project>

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

server:
    #定义端口号
  port: 8082
spring:
  application:
    #定义应用名称,即服务名称
    name: cloud-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka
  #服务注册中心实例的主机名
  instance:
    hostname: 127.0.0.1
    prefer-ip-address: true
    instance-id: 127.0.0.1:8082

(4)启动类加注解@EnableEurekaClient

package com.chen.demo.cloudclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class CloudClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudClientApplication.class, args);
    }

}

(5)测试
使用Idea简单搭建springcloud项目
(6)在父pom中加上

    <modules>
        <module>eureka-server</module>
        <module>cloud-provider</module>
        <module>cloud-client</module>
    </modules>

使用Idea简单搭建springcloud项目

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

package com.chen.test.cloudprovider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chen.test.cloudprovider.service.HelloService;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private HelloService helloService;
    @GetMapping("/getHello")
    public String getHello(){

        return helloService.getHello();
    }
}

package com.chen.test.cloudprovider.service;

public interface HelloService {

    String getHello();
}

package com.chen.test.cloudprovider.service.impl;

import com.chen.test.cloudprovider.service.HelloService;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String getHello() {
        return "你好兄弟";
    }
}

(2)测试
使用Idea简单搭建springcloud项目
(3)在cloud-client中创建controller包和service包

package com.chen.demo.cloudclient.controller;

import com.chen.demo.cloudclient.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/Hello")
public class HelloClient {

    @Autowired
    private HelloService helloService;

    @GetMapping("/getClient")
    public String getClient(){
        return helloService.getProduct();
    }
}

package com.chen.demo.cloudclient.service;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

//name 为product项目中application.yml配置文件中的application.name;
//path 为product项目中application.yml配置文件中的context.path;
@FeignClient(name = "cloud-provider",path ="/hello" )
//@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
@Component
public interface HelloService {

    @RequestMapping(value = "getHello")
    String getProduct();
}

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

package com.chen.demo.cloudclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class CloudClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudClientApplication.class, args);
    }

}

(4)测试
使用Idea简单搭建springcloud项目

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。