SpringCloud中使用eureka

  微服务中可以使用eureka实现服务的注册与发现,下面演示过程

一、服务端配置

  1、引入依赖

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

  2、添加配置

eureka:
  instance:
    hostname: localhost    #eureka服务端的实例名称
  server:
    enable-self-preservation: false  #关闭注册中心自我保护
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。
    fetch-registry: false    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #eureka地址

  3、启动类添加EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class Eureka7001_App {

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

  启动应用,浏览器输入http://localhost:7001(我设置的端口)即可出现管理界面

二、客户端配置

  1、引入依赖

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

  2、添加配置

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: provider8001-dept
    prefer-ip-address: true

  3、启动类添加EnableEurekaClient注解

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

  启动应用,浏览器输入http://localhost:7001,发现该应用已经注册进去了

三、服务的发现

@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
    public Object discovery(){
        List<String> list = client.getServices();
        System.out.println("**********" + list);
        List<ServiceInstance> srvList = client.getInstances("provider8001");
        for (ServiceInstance element : srvList) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
                    + element.getUri());
        }
        return this.client;
    }

 四、配置eureka集群

  这里演示配置3台eureka服务,端口分别是7001、7002、7003

  1、准备本地域名映射

  给C:\Windows\System32\drivers\etc\hosts添加如下配置

127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com
127.0.0.1  eureka7003.com

  2、配置文件application.yml分别如下(依次7001、7002、7003、8001)

eureka:
  instance:
    hostname: eureka7001.com #集群
  server:
    enable-self-preservation: false #关闭注册中心自我保护
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。
    fetch-registry: false    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #集群
eureka:
  instance:
    hostname: eureka7002.com #集群
  server:
    enable-self-preservation: false #关闭注册中心自我保护
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。
    fetch-registry: false    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/ #集群
eureka:
  instance:
    hostname: eureka7003.com #集群
  server:
    enable-self-preservation: false #关闭注册中心自我保护
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。
    fetch-registry: false    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #集群
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: provider8001-dept
    prefer-ip-address: true

  依次启动7001、7002、7003、8001程序,发现8001服务注册到了3个eureka中了

  可通过地址http://localhost:7001、http://localhost:7002、http://localhost:7003查看

posted @ 2019-11-09 22:38  雷雨客  阅读(1057)  评论(0)    收藏  举报