cloud的Eureka服务中心的建立和模块注册进服务中心

1.Eureka服务注册中心构建

-加入服务端依赖

<dependency>
     <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

-配置yml

server:
  port: 7001
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己
    fetch-registry: false           #false表示自己就是注册中心,职责是维护实例,不参加检索
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置eureka server的交互地址,即对外暴露的地址

-添加启动类

  • ==注意:要在类前加@EnableEurekaServer注解==

  •  1 package com.XXX;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
     6 
     7 @SpringBootApplication
     8 @EnableEurekaServer
     9 public class Eureka7001_APP {
    10     public static void main(String[] args) {
    11         SpringApplication.run(Eureka7001_APP.class,args);
    12     }
    13 }

    -验证是否构建成功

    启动主程序,访问该服务地址即可

向Eureka注册中心注册微服务

1.在要注册的模块的pom.xml中增加依赖

1 <dependency>
2             <groupId>org.springframework.cloud</groupId>
3             <artifactId>spring-cloud-starter-eureka</artifactId>
4         </dependency>
5         <dependency>
6             <groupId>org.springframework.cloud</groupId>
7             <artifactId>spring-cloud-starter-config</artifactId>
8         </dependency>    

2.修改yml

  • 在application.yml中增加以内容,将客户端注册到服务列表内

  • ==理解:小区用户要找到物业管理处的地址进行注册==

    1 eureka:
    2   client:
    3     service-url:
    4       defaultZone: http://localhost:7001/eureka

    3.主启动类增加注解

  • 增加@EnableEurekaClient注解
    1 @SpringBootApplication
    2 @EnableEurekaClient//注表名自己是注册方,将服务注入eureka
    3 public class Provider8001_APP { public static void main(String[] args) { SpringApplication.run(Provider8001_APP.class,args); 
    }
    }

    actuator与微服务注册完善

    主机名称与服务名称的修改

  • 修改服务名称,在yml中eureka节点下添加如下内容
  •  eureka:
      instance:
        instance-id: dept8001        #修改别名
        prefer-ip-address: true        #显示IP地址

猜你喜欢

转载自www.cnblogs.com/best-lwj/p/9248791.html