SpringCloud-基于SpringBoot 2.1.3.RELEASE版本从零搭建分布式框架之注册中心(二)

上一篇

SpringCloud-基于SpringBoot 2.1.3.RELEASE版本从零搭建分布式框架之注册中心(二)

单注册中心Eureka

SpringCloud注册中心服务种类比较多,有Eureka、Zookeeper、Consul、Cloud Foundry等,今天用的工具是Eureka。注册中心的概念不必多讲,直接上干货!

构建Maven聚合项目

  1. 创建父工程
    在这里插入图片描述
  2. 删掉无用结构,如下图
    在这里插入图片描述
  3. 新建Module-注册中心
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  4. 添加Eureka-Server依赖
    依赖地址
    在这里插入图片描述
    解压demo.zip,复制pom.xml里面的内容,把中的内容放在父pom文件中:
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

父pom
Eureka-Server的pom文件内容

<?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">
  <parent>
      <artifactId>spring-cloud</artifactId>
      <groupId>com.spring.cloud</groupId>
      <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>eureka-server</artifactId>
  <name>eureka-server</name>
  <description>注册中心</description>
  <packaging>jar</packaging>

  <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>Greenwich.RELEASE</spring-cloud.version>
  </properties>

  <dependencies>
      <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>
          <exclusions>
              <exclusion>
                  <groupId>org.mockito</groupId>
                  <artifactId>mockito-core</artifactId>
              </exclusion>
          </exclusions>
      </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>
  1. 创建启动类
    在这里插入图片描述
    EurekaServerApplication 代码:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  1. 配置application.properties
spring.application.name=spring-cloud-eureka-server
server.port = 8001
eureka.instance.hostname = localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone = \
http://${eureka.instance.hostname}:${server.port}/eureka/

注意:
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
是取消Eureka-Server自身注册发现机制,默认为true,如不设置为false,启动就会报错,但是不会挂掉,可以正常使用。
7. 运行EurekaServerApplication.java
访问:http://localhost:8001 ,如下图所示,说明启动成功
在这里插入图片描述

多注册中心配置

例子:三个注册中心
复制 application.properties文件三份,分别命名为
application-server1.properties

spring.application.name=spring-cloud-eureka-server
server.port = 8001
eureka.instance.hostname = localhost
eureka.client.serviceUrl.defaultZone = \
  http://localhost:8002/eureka/, http://localhost:8003/eureka/

application-server2.properties

spring.application.name=spring-cloud-eureka-server
server.port = 8002
eureka.instance.hostname = localhost
eureka.client.serviceUrl.defaultZone = \
  http://localhost:8001/eureka/, http://localhost:8003/eureka/

application-server3.properties

spring.application.name=spring-cloud-eureka-server
server.port = 8003
eureka.instance.hostname = localhost
eureka.client.serviceUrl.defaultZone = \
  http://localhost:8001/eureka/, http://localhost:8002/eureka/

然后清空application.properties的内容,打成jar包
在win10 cmd窗口找到jar包位置,开启三个cmd窗口,启动三个jar包,访问http://localhost:8001,http://localhost:8002,http://localhost:8001都可以看到,注册中心集群已启动

java -jar eureka-server.jar --spring.profiles.active=service1
java -jar eureka-server.jar --spring.profiles.active=service2
java -jar eureka-server.jar --spring.profiles.active=service3

下一篇

原创文章 29 获赞 82 访问量 1万+

猜你喜欢

转载自blog.csdn.net/cuixhao110/article/details/88353714