SpringCloud(1)Eureka 服务注册中心

Eureka提供服务的发现注册,心跳,健康检查功能。分布式的基础功能

一. Eureka server

(1)pom文件

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

(2) application.yml

eureka:
  client:
    service-url:
      #本身自带客户端,注册服务端地址,多台server端时互相注册,建议使用多台防止挂了
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
    # 单点的时候设置为 false 禁止注册自身
    register-with-eureka: false 
  server:
    #开发环境可以关闭自我保护,不要出现告警
    enable-self-preservation: false
spring:
  application:
    name: eureka
server:
  port: 8761

(3)EurekaApplication.java 使用 @EnableEurekaServer

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

二。Eureka client

(1) pom.xml

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

(2) application.yml

eureka:
  client:
    service-url:
      #注册服务端地址,多台server时,都注册
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:873/eureka
spring:
  application:
    name: client

(3)ClientApplication.java 使用@EnableEurekaClient

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

三。 Eureka server一般有多台应用防止某台挂了

server端的该配置,配其他server端点的。client端配置所有server端点的

eureka:
  client:
    service-url:
      #本身自带客户端,注册服务端地址,多台server端时互相注册,建议使用多台防止挂了
      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka

猜你喜欢

转载自www.cnblogs.com/t96fxi/p/12658861.html