初学SpringCloud:使用Eureka作为服务注册中心,写服务注册中心的微服务模块

我的上一篇博客地址:

初学SpringCloud,我的controller层对service层的调用

1、背景介绍

我目前已经完成了微服务框架下的服务提供者的Controlle层,Service层,Dao层和mapper.xml的代码编写(其中的POM文件和application文件我后期会详细解析的)

2、本篇博客的目的

本篇博客我要创建一个完整的服务注册中心的微服务,使用Eureka的技术

3、下面是我的这个微服务的文件夹分布情况:

4、全部代码的详细讲解

         4、1我的主启动类

package com.springcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

           服务注册中心并没有什么业务逻辑的代码,因此也就比较的简单,只有一个主启动类。

           SpringBootApplication注解是必须的

           EnableEurekaServer注解,表明这个微服务是一个使用Eureka技术的服务注册中心,加上了这个注解才能使得这个微服务成为注册中心。

          接下来就是main方法,是一切程序的入口,这是不需要多解释的。

          然后就是run方法。

          4、2我的application.xml文件 

server:
  port: 7001

eureka:
  instance:
    hostname: localhost  #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
     service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
 
 

首先就是设置端口,这是不需要讲解的

还需要设置服务端的实例名字(这是为了方便我们将来在浏览器的地址栏中访问服务注册中心的某一个服务,其实在商业开发中,服务注册中心往往不止一个,因此需要起名字,来区分它们)

register-with-eureka:这个属性表示是否向注册中心注册自己,这是没有必要的,所以设置为false

fetch-registry(获取-注册):这个属性表示 是否获取注册信息,但自己就是服务注册中心,因此不需要可以的去获取注册信息

defaultZone:这个属性是必须有的,如果微服务的注册中心是单机版的,那么服务注册中心的这个属性值就是像我的代码那样写,服务提供者的这个属性值就是http://localhost:7001/eureka  (表示注册到这个地址),服务消费者的这个属性值就是http://localhost:7001/eureka(表示注册到这个地址)。至于微服务的注册中心是集群版的话,我后期会出博客进行讲解的。

        4、3我的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">
    <parent>
        <artifactId>cloud2023</artifactId>
        <groupId>com.lanse.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7001</artifactId>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.lanse.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

    </dependencies>
    <!--<dependencyManagement>-->
        <!--<dependencies>-->
            <!--<dependency>-->
                <!--<groupId>org.springframework.cloud</groupId>-->
                <!--<artifactId>spring-cloud-dependencies</artifactId>-->
                <!--<version>Hoxton.SR1</version>-->
                <!--<type>pom</type>-->
                <!--<scope>import</scope>-->
            <!--</dependency>-->
        <!--</dependencies>-->
    <!--</dependencyManagement>-->


</project>

我从上到下依次解析:

     第一个dependency第一次学微服务的话,应该是第一次见到。只有加上了这个依赖,才能证明这个微服务是一个注册中心

    第二个是我的自己写的一个依赖,由于我们全部的微服务模块都会用到共同的实体类,所以我就把这个共同的部分给抽出来了,单独成为了一个微服务模块,打成了一个包,然后我们在别的微服务模块中引入这个依赖就可以了。这个我后期会讲解的。

   下面的依赖就都是Web启动的依赖,热部署的依赖,LomBok插件的依赖,测试的依赖等,都是比较常规的,因此我在这里就不再过多的讲解了。

5、查看是否成功

我们启动这个作为服务注册中心的微服务模块,然后打开浏览器,在浏览器的地址栏中输入:

http://localhost:7001/

就会出现下面的界面了:

6、总结

到此为止,我们的一个单机版的服务注册中心就已经完成了。

接下来需要做额就是把我们编写过的目前还是半成品的服务提供方的微服务模块进一步完善,然后注册进我们的服务注册中心去。

我的下一篇博客地址:

初学SpringCloud:将我们每一个微服务模块中都会用到的一些实体类的代码抽取出来,组成一个微服务,然后install一下。其他的微服务模块可以写依赖调用,减少代码冗余。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你是我的日月星河

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

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

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

打赏作者

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

抵扣说明:

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

余额充值