idea快速搭建springcloud项目(一)

1.搭建父工程

(1)选择菜单file-new project ,弹出窗口中左侧菜单选择Maven ,点击next按钮idea快速搭建springcloud项目(一)_第1张图片

(2)填写name和Location,点击Finish按钮
(因我已创建了这个项目名,所以红框了)
idea快速搭建springcloud项目(一)_第2张图片

(3)删除src文件
在这里插入图片描述
(4)修改pom.xml 添加以下配置

  <packaging>pom</packaging>

	<name>goodstudy_parent</name>
	<description>frank_springCloud项目</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/>
	</parent>

	<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.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>
	
	<repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

(5)当IDEA右下角弹出提示 ,点击Enable Auto-Import 自动导入依赖
idea快速搭建springcloud项目(一)_第3张图片

2.搭建公共子模块

2.1 搭建子模块步骤

搭建公共子模块 goodstudy_common

(1)右键点击工程,弹出菜单选择 New -Module 弹出窗口选择Maven ,点击Finish按钮

idea快速搭建springcloud项目(一)_第4张图片

创建返回结果实体类

(1) 新建entity包,包下创建类ApiResponse,用于控制器类返回结果

package common.entity;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@Data
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = -7631036182754360705L;
    private String resultCode;
    private String resultMsg;
    private String errorCode;
    private T resultObj;
    public static final String SUCCESS="0";
    public static final String VALIDATE_FAIL="1";
    public static final String FAIL = "2";
    public static final String UNKNOWN = "9999";
    public static final String TOKEN_EXPIRED = "9998";
    public static final String AUTH_FAIL = "9997";

    public ApiResponse(String resultMsg, String errorCode) {
        this.resultMsg = resultMsg;
        this.errorCode = errorCode;
    }

    public ApiResponse() {
        this.resultCode =HttpStatus.OK.value()+"";
        this.resultMsg = "success";
    }

    public ApiResponse(T resultObj) {
        this.resultObj = resultObj;
        this.resultCode= HttpStatus.OK.value()+"";
        this.resultMsg = "success";
    }

    public ApiResponse(String resultCode, String resultMsg, String errorCode) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.errorCode = errorCode;
    }
}

(2)创建ApiRequest类,用于请求体入参

package common.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.UUID;

@Data
public class ApiRequest<T>  implements Serializable {
    private static final long serialVersionUID = -1085596225026903625L;
    private T bizParam;
    private String requestNo= UUID.randomUUID().toString();
    private String apiVersion="1.0.0";
    private String sysId;
    private String sign;
    private String encryptType;
    private String userId;
    private String password;
    private String token;
    private String ip;

    public ApiRequest() {
    }
}

(3)创建类PageResult ,用于返回分页结果

package common.entity;

import java.util.List;

/**
 * 分页实体结果对象
 */
public class PageResult<T> {
    private Long total;//总记录数
    private List<T> rows;

    public PageResult() {
    }

    public PageResult(Long total, List<T> rows) {
        this.total = total;
        this.rows = rows;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}

注:common项目不需要创建启动类

搭建Base项目

(1) 搭建基础微服务模块goodstudy_base,pom.xml引入依赖

<dependencies>
        <dependency>
            <groupId>com.goodstudy</groupId>
            <artifactId>goodstudy_common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

(2)创建启动类BseApplication

package com.goodstudy.base;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = "com.goodstudy")
@Slf4j
public class BaseApplication {
    public static void main(String[] args) {
        SpringApplication.run(BaseApplication.class);
        log.info("============BaseApplication启动完成===============");
    }
}

注:(exclude = {DataSourceAutoConfiguration.class}),因为没有加入数据源,所以要加这个,不然启动会报错

(3)在resources文件夹下,创建application.yml文件

server:
  port: 9001
spring:
  application:
    name: goodstudy-base #指定服务名

(4)启动项目, 启动成功

在这里插入图片描述

搭建eureka项目

(1) 创建goodstudy_eureka模块
(2) 引入依赖 父工程pom.xml定义SpringCloud版本

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

goodstudy模块pom.xml引入eureka-server

     <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>

(2)添加application.yml

server:
  port: 6868 #服务端口
eureka:
  client:
    registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所有无需注册
    fetchRegistry: false #是否从Eureka中获取注册信息
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
      defaultZone: http://127.0.0.1:${server.port}/eureka/

(3)编写启动类

创建包com.goodstudy.eureka ,包下建立类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer.class, args);
    }
}

(4)启动运行启动类,然后在浏览器地址栏输入 http://localhost:6868/ 运行效果如下:
idea快速搭建springcloud项目(一)_第5张图片

2.3服务注册

我们现在就将所有的微服务都注册到Eureka中,这样所有的微服务之间都可以互相调用了。
(1)将其他微服务模块添加依赖

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

(2)修改每个微服务的application.yml,添加注册eureka服务的配置

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:6868/eureka
      instance:
        prefer-ip-address: true

(3)修改每个服务类的启动类,添加注解

@EnableEurekaClient

(4)启动测试:将每个微服务启动起来,会发现eureka的注册列表中可以看到这些微服务了
idea快速搭建springcloud项目(一)_第6张图片

2.4 保护模式

如果在Eureka Server的首页看到以下这段提示,则说明Eureka已经进入了保护模式:
idea快速搭建springcloud项目(一)_第7张图片

Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告。保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。

你可能感兴趣的:(spring,cloud,eureka,spring,cloud,java)