spring-cloud_Eureka服务注册与发现

一、Eureka的搭建

  Eureka的服务注册与发现,Eureka分为两个Server和Client,所以存在两个maven

Client依赖

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

server依赖

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

Eureka存在两个身份状态,我们在配置时先配置一个server,Eureka服务注册和发现。需要一个注册中心,和多个client客户端,

一、我们先配置一个注册中心server,注册中心是一个单独的module

 配置步骤:

  1、由于创建的是Eureka注册中心,所以,我们在启动类上加@EnableEurekaServer,且pom文件中加Eureka的server的依赖

//pom文件中加入依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

//主启动类
@SpringBootApplication
//标注的启动该新组件技术的相关注解标签
@EnableEurekaServer    // 服务器端启动类,接收其他微服务注册进来
public class EurekaStater {
    public static void main(String[] args) {
        
        SpringApplication.run(EurekaStater.class,args);
    }
}

  2、yml文件中

server:
  port: 6001    #指定
eureka:
  instance:
    hostname: Eureka6001    #对应的是域名,此域名要在C:\Windows\System32\drivers\etc   host文件
  client:
    register-with-eureka: false #服务注册 false 自己不注册到服务中心
    fetchRegistry: false   #服务发现  表示自己端就是注册中心 我的职责就是维护服务实例 false  自己不获取注册信息
    service-url:    #集群的情况下,服务端之间要互相注册,指向对方
      defaultZone: http://localhost:6001/eureka/   #绑定的是自己的端口

 二、将对应模块,微服务注册进该服务中心

 1、在对应的微服务模块中加入的是Eurake的client依赖,且主启动类上加@EnableEurekaClient

//pom文件中
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

//主启动类中
@SpringBootApplication
@MapperScan("com.ghh.mapper")
@EnableEurekaClient   //表名客户端
public class ProductStart {
    public static void main(String[] args) {
        SpringApplication.run(ProductStart.class,args);
    }
}

2、yml文件中

eureka:
  instance:
    hostname: Eureka-client
  client:
    register-with-eureka: true #服务注册 true 注册到服务中心
    fetchRegistry: true   # true 获取注册信息
    service-url:    #注册到指定的注册中心或集群
      defaultZone: http://Eureka6001.com:6001/eureka/

3、先启动服务中心的主启动类,在启动客户端,以下界面表明 服务中心发现成功

此时表明Eureka的服务中心与客户端进行注册和发现成功

猜你喜欢

转载自www.cnblogs.com/guanyuehao0107/p/11816955.html