springcloud微服务架构之搭建注册中心Eureka与注册中心的互相注册

前言
    springboot作为当下最流行的微服务框架,并且提供了代码的运行环境。然而springcloud是一套微服务管理框架,提供了服务的注册与发现,负载均衡等组件。这两个框架结合起来会很容易开发出一套微服务系统。
    注册中心的互相发现可以有效的解决当一个注册中心挂掉以后整个服务就丢失服务的状态,当一个注册中心挂掉后,还会有一个相同的服务继续工作。

    这篇博客主要说一下如何自己搭建一个注册中心与互相注册。

前期准备

    IDEA编辑器

    JDK1.8

搭建过程
    1.首先在开发工具中创建一个maven根项目,File-New-Project-Maven,选择Project SDK 中的1.8,然后点击下一步。

    2.输入包名GroupId、项目唯一标识ArtifactId、以及版本号Version,点击下一步。    3.输入项目名,一般和ArtifactId保持一致,然后选择项目路径,点击下一步,创建成功。    4.然后在pom文件中添加父文件的依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    5.然后点击项目右键,New-Module,重复刚才的步骤,创建一个Eureka子项目,来作为注册中心。在新创建项目的pom文件中添加依赖:
    <!--依赖管理-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <!--依赖管理中心-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--构建中心-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- defined in spring-cloud-starter-parent pom (as documentation hint),
                    but needs to be repeated here -->
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-core</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-client</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>
    6.在项目中创建包架构,然后创建启动类Application.java。需要在启动类上加上@SpringBootApplication(springboot的启动类)和@EnableEurekaServer(对外提供注册与发现服务)
    7.在resources中创建一个配置文件application.yml、application-8000.yml、application-8001.yml,用来配置注册中心的地址与端口号
application.yml:
#配置文件管理
spring:
  application:
    #配置项目名,会在注册中心application中显示
    name: eureka
  profiles:
    #配置使用的配置文件
    active: 8000
application-8000.yml:
#server 端口号设置
server:
  port: 8000

#注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/
application-8001.yml:
#server 端口号设置
server:
  port: 8001

#注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

    8.这里用两个配置文件分别写不同的端口号,然后注册到另一个端口号的注册中心里,实现了注册中心的互相注册。使用application.yml控制使用的配置文件,减少更改配置文件次数。
    9.分别使用两个配置文件启动两次服务,然后访问其中的一个端口号,比如:http://localhost:8000/,若看到如下页面则说明注册中心搭建成功,在Status中看到同一个机器的两个端口号。

   

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊猫猫猫猫猫猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值