Springcloud之服务注册中心-----Eureka开发步骤

一、Eureka(netflix开发的)

  A. 开发 服务端 server

    1.创建项目 并 引入依赖 

    <!--sprinngboot-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    2.编写配置文件 application.properties

      server.port=8671                                                     # 指定服务端口
      spring.application.name=eurekaserver                                 #  指定eureka服务名称  | 唯一标识
      eureka.client.service-url.defaultZone=http://localhost:8671/eureka   # 暴露服务中心地址      
      eureka.client.register-with-eureka=false                     #关闭自己注册自己
      eureka.client.fetch-registry=false                             #关闭 启动时把自己作为客户端立即注册,初始化好了在注册

    3.入口类加入注解 @EnableEurekaServer   

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

    4.浏览器访问eureka的服务注册页面

      http://localhost:8671

 

  B.开发 客户端 client(服务提供者)

    1.创建项目 并 引入依赖

        <!--springboot web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


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

    2.编写配置文件 application.properties 

      server.port=9999                                 #服务端口号
      spring.application.name=eurekaclient                        #服务名称唯一标识
      eureka.client.service-url.defaultZone=http://localhost:8761/eureka    #eureka注册中心地址

    3.入口类加入注解  

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

 

  C.浏览器端server查看客户端是否注册成功

 
posted @ 2022-11-06 00:55  向大海  阅读(56)  评论(0编辑  收藏  举报