SpringCloud-Eureka 服务注册与发现,搭建 服务端和客户端

注册中心服务端搭建

1 新建springboot项目,添加 Spring-cloud-Discovery》》》》》》》Eureka Server依赖

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

2 配置Application.yml文件

server:
  port: 8761

eureka:
  instance:
    #指定主机地址
    hostname: 192.168.0.12
  client:
    #声明自己是个服务端
    #指定是否要注册到注册中心(注册中心服务 不需要开启)
    registerWithEureka: false
    #指定是否要从注册中心获取服务(注册中心不需要开启)
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

#  server:
#    #关闭保护模式 (默认是开启)
#    enable-self-preservation: false

#服务的名称
spring:
  application:
    name: eureka-service

3 启动类上添加注解 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer //开启Eureka注册中心服务
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Eureka客户端搭建 (所有其它的服务都按这个模式来)
1 新建springboot项目,添加 Spring-cloud-Discovery》》》》》》》Eureka Discovery Client依赖

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

2 配置 Application.yml文件

server:
  port: 8771


#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.0.12:8761/eureka/

#服务的名称
spring:
  application:
    name: product-service

3 启动类上添加注解 @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient //Eureka客户端
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

}

 


版权声明:本文为qq_41712271原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。