Euryka服务注册中心的使用

2020-09-04  本文已影响0人  MaJiT

1.导包

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

2.写配置文件

#服务端口号
server:
  port: 7001
eureka:
  instance:
    hostname: localhost
  client:
    #表示不向注册中心注册自己
    register-with-eureka: false
    #表示自己就是注册中心
    fetch-registry: false
    #交互地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.写启动类

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

4.访问地址localhost:7001

5.微服务注册到eureka注册中心

        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
    #将自己注册到Eureka中
    register-with-eureka: true
    #是否从Eureka中获取到自己的信息
    fetch-registry: true
@SpringBootApplication
@EnableEurekaClient
public class PaymentApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentApplication.class,args);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读