天天看点

spring cloud-Eureka学习

服务端

  • 引入依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
           
  • 启动类加入注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer_6001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_6001.class, args);
    }
}
           
  • 配置文件
server:
  port: 6002
eureka:
  instance:
    hostname: eureka6002.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
    #单机版的
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群版的。配置其他Eureka Server服务端访问地址,多个地址以逗号分隔
      defaultZone: http://eureka6001.com:6001/eureka/
  server:
    enable-self-preservation: false #禁用自我保护模式,生产上应该启动

           

客户端

  • 引入依赖
<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
           
  • 启动类加入注解
@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.liyiruo.springcloud.mapper")
public class ProductProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ProductProvider_8001.class, args);
    }
}
           
  • 配置文件
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      #单机版服务注册中心
#defaultZone: http://localhost:6001/eureka
      #集群版Eureka 服务注册中心
      defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/
  instance:
    instance-id: ${spring.application.name}:${server.port} #指定实例ID 就不会显示主机名了
    prefer-ip-address: true #访问路经可以显示ip地址```

           

继续阅读