SpringCloud整合Eureka

1, EurekaNetflix开发的基于Rest服务的服务注册与服务发现的组件,包括EurekaServer用于监控注册的服务和接收请求发送给服务的提供者,EurekaClient为注册者
复制代码
SpringCloud整合Eureka

2, Dubbo 是一个微服务整体架构的框架,提供的功能包括服务注册发现,远程调用,监控等待 对标于SpringCloud,而Dubbo的服务发现模块是基于zookeeper

二,单机部署

1, 部署服务器
复制代码
server:
  port: 7002
eureka:
  server:
    #开启(默认)自我保护机制
    enable-self-preservation: true
    #心跳包发送间隔,服务端收到心跳包后2秒后没有收到就会剔除服务
    #eviction-interval-timer-in-ms: 2000
  instance:
    hostname: eureka7002
  client:
    #不向注册机注册自己
    register-with-eureka: false
    #在注册中心获取其他服务
    fetch-registry: false
    service-url:
      #服务监控地址
      defaultZone: http://eureka7001:7001/eureka/
复制代码
启动器选择服务
@EnableEurekaServer
@SpringBootApplication
@EnableDiscoveryClient
public class Eureka7001Application {
    public static void main(String[] args) {
        SpringApplication.run(Eureka7001Application.class,args);
    }
}
复制代码
2, 注册服务者
复制代码
eureka:
  instance:
    instance-id: payment8002
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      #注册到注册中心
      defaultZone: http://eureka7001:7001/eureka/
复制代码
对外发布服务
@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
public class Payment8002Application {
    public static void main(String[] args) {
        SpringApplication.run(Payment8002Application.class, args);
    }
}
获取服务信息
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/discovery")
public Object discovery() {
    Map<Object,Object> map = new HashMap<>();
    List<String> services = discoveryClient.getServices();
    for (String s : services) {
        List<ServiceInstance> instances = discoveryClient.getInstances(s);
        map.put(s,instances);
    }
    map.put("discoveryClient",discoveryClient);
    return map;
}
复制代码
3, 注册消费者
复制代码
server:
  port: 80
eureka:
  instance:
    instance-id: consumer80
    #暴露IP地址
    prefer-ip-address: true
  client:
    #将自己注册到注册中心
    register-with-eureka: true
    #去注册中心获取其他服务地址
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
复制代码
/**
 * RestTemplate:
 * 提供了多种方式访问远程HTTP服务,是一种简单便捷访问restful服务模板类,
 * 是Spring提供用于访问Rest服务的客户端模板工具集
 */
@Autowired
private RestTemplate template;

@GetMapping(value = "/consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
    return template.postForObject(PAYMENT_URL + "payment/create", payment, CommonResult.class);
}
复制代码

三,集群部署

1, 部署服务器
复制代码
server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001
  client:
    #不向注册机注册自己
    register-with-eureka: false
    #去注册中心获取其他服务的地址
    fetch-registry: false
    service-url:
      #两个服务器相互注册
      defaultZone: http://eureka7002:7002/eureka/

--------------------------------------------------------
server:
  port: 7002
eureka:
  server:
    #开启(默认)自我保护机制
    enable-self-preservation: true
    #心跳包发送
#    eviction-interval-timer-in-ms: 2000
  instance:
    hostname: eureka7002
  client:
    #不向注册机注册自己
    register-with-eureka: false
    fetch-registry: false
    service-url:
      #两个服务器相互注册
      defaultZone: http://eureka7001:7001/eureka/
复制代码
2, 注册提供者
复制代码
eureka:
  instance:
    instance-id: payment8001
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      #同时注册两个服务器
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
-----------------------------------------------------------
eureka:
  instance:
    instance-id: payment8002
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
复制代码
3, 注册消费者
复制代码
server:
  port: 80
eureka:
  instance:
    instance-id: consumer80
    prefer-ip-address: true
  client:
    #将自己注册到注册中心
    register-with-eureka: true
    #去注册中心获取其他服务地址
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/
复制代码

原文 

https://juejin.im/post/5e8825c3f265da47cd3562b4

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » SpringCloud整合Eureka

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址