天天看点

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

从零搭建springcloud(一)注册中心Eureka

  • 什么是springcloud
  • 创建第一个服务-Eureka-Server(注册中心)
    • springboot版本
    • 创建eureka-server项目
    • 配置pom.xml文件
    • 编写application配置文件。
    • 在启动类上添加注解
    • 启动项目

什么是springcloud

要说springcloud,就不得不说到微服务。微服务并不是一种技术,而是一种思想。他将一个大的项目拆分成许多个子项目,每一个子项目都只提供自身的功能,他们之间通过某种方式(通常是RestfulApi)来互相通信,这就是微服务的思想。

而springcloud,则是在微服务的基础上,给一些专属功能的服务提供了具体的实现,如注册中心、配置中心、网关、熔断器、负载均衡等等

创建第一个服务-Eureka-Server(注册中心)

springboot版本

此后的springcloud教程中所用的springboot版本都是2.0.3.RELEASE。

创建eureka-server项目

eureka-server,即注册中心。我们先创建eureka的服务端,用于其他辅助的注册与发现。

首先选择创建新项目

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

选择Spring Initializr →设置jdk为1.8 →下一步

在这一步中,由于网络等原因可能会卡住,多尝试几次即可

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

设置一些参数

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

选择普通的springcloud项目,下一步(或者什么都不选,因为后面我们会自己配置pom.xml文件)

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)
从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

创建的项目结构如图:

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

配置pom.xml文件

创建好后的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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yang</groupId>
    <artifactId>springcloud-eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--引入springboot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--引入test starter  包括junit、assertj等依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--引入maven插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

           

在此基础上,我们需要修改部分配置,如下所示:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yang</groupId>
    <artifactId>springcloud-eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
		<!--去掉spring-boot-starter依赖,否则会与springcloud依赖冲突-->
        <!--引入test starter  包括junit、assertj等依赖 去掉子依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--引入eureka-server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--
                注意:在springcloud F版本后,
                Spring Boot 1.5.x 升级为 Spring Boot 2.0.x
                Spring Cloud Edgware SR4 => Spring Cloud Finchley.RELEASE
                依赖也由
                spring-cloud-starter-eureka-server
                升级为
                spring-cloud-starter-netflix-eureka-server
            -->
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <!--引入springcloud的配置管理,按住Ctrl+左键单击artifactId可以查看其中具体的组件版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!--指定springcloud版本为Finchley.RELEASE-->
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--引入maven插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

配置好后,点击右下角的导入修改

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

编写application配置文件。

个人习惯原因,比较喜欢yml语法,所以先将application.properties文件重命名为application.yml。

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)
从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

配置文件内容如下:

#指定启动的端口号
server:
  port: 8700
eureka:
  #定义主机名称
  instance:
    hostname: localhost
#下面为eureka客户端的配置信息
  client:
    #代表不向注册中心注册自己
    register-with-eureka: false
    #不需要去检索服务
    fetch-registry: false
    #指定注册中心的路径
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#定义这个服务的名字
spring:
  application:
    name: eureka-server
           

在启动类上添加注解

进入启动类

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

在启动类上添加注解@EnableEurekaServer,表明这个服务是eureka服务端

package com.yang.springcloudeurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaServerApplication {

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

}
           

启动项目

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

启动项目后,在浏览器中访问http://localhost:8700/

可以看到如下界面

从零搭建springcloud(一)注册中心 Eureka-Server (F版本)什么是springcloud创建第一个服务-Eureka-Server(注册中心)

表明注册中心启动成功。

下一期为:从零搭建springcloud(二)注册中心 Eureka-Client (F版本)