
什么是eureka?
基础知识:
服务注册与发现
两个组件:

eureka实战:
单机eureka构建步骤:
-  1.建module
 cloud-eureka-server7001
- 改pom:引入eureka的相关依赖
<dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
</dependency>
- 3.写yml
server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 主启动
@springbootapplication
@enableeurekaserver
public class eurekamain7001 {
    public static void main(string[] args) {
        springapplication.run(eurekamain7001.class, args);
    }
}
- 4.测试
http://localhost:7001/
- 5.将服务注册进eureka成为提供者
dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
</dependency>
- 6.改yml
server:
  port: 80
spring:
  application:
    name: cloud-order-service
eureka:
  client:
    #表示是否将自己注册进eurekaserver默认为true。
    register-with-eureka: true
    #是否从eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchregistry: true
    service-url:
      defaultzone: http://localhost:7001/eureka
- 7.主启动
@springbootapplication
@enableeurekaclient
public class mainapp80 {
    public static void main(string[] args) {
        springapplication.run(mainapp80.class, args);
    }
}
- 8.注意事项:

集群eureka构建步骤:

- 127.0.0.1 eureka7001.com
- 127.0.0.1 eureka7002.com
server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com  #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己。
    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultzone: http://eureka7002.com:7002/eureka/
7002中也将7001注册进去
4.主启动
package com.atguigu.springcloud;
@springbootapplication
@enableeurekaserver
public class eurekamain7002 {
    public static void main(string[] args) {
        springapplication.run(eurekamain7002.class, args);
    }
}
5.服务如集群
# 集群版
defaultzone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  
@value("${server.port}")
private string serverport;
///不要把地址写死
//public static final string paymentsrv_url = "http://localhost:8001";
// 通过在eureka上注册过的微服务名称调用
public static final string payment_srv = "http://cloud-payment-service";
package com.atguigu.springcloud.config;
@configuration
public class applicationcontextconfig {
    @bean
    @loadbalanced //使用@loadbalanced注解赋予resttemplate负载均衡的能力
    public resttemplate resttemplate() {
        return new resttemplate();
    }
}
instance:
  instance-id: payment8001
访问信息有ip信息提示:
prefer-ip-address: true     #访问路径可以显示ip地址
@resource
private discoveryclient discoveryclient;
@getmapping(value = "/discovery")
public object discovery() {
    list<string> services = discoveryclient.getservices();
    for (string service : services) {
        log.info("service:{}", service);
    }
    list<serviceinstance> instances = discoveryclient.getinstances("cloud-payment-service");
    for (serviceinstance instance : instances) {
        log.info(instance.getserviceid() + "\t" + instance.gethost() + "\t" + instance.getport() + "\t"
                 + instance.geturi());
    }
    return this.discoveryclient;
}
@springbootapplication
@enableeurekaclient
@enablediscoveryclient //服务发现
public class paymentmain8001 {
    public static void main(string[] args) {
        springapplication.run(paymentmain8001.class,args);
    }
}
 
             我要评论
我要评论 
                                             
                                            
发表评论