Eureka与Zookeepr你了解哪一个

1、服务的注册和发现(Eureka,Zookeepr)

因为传统的rpc远程调用框架中,管理每个服务和服务之间的关系比较复杂,所以需要进行服务治理管理这些依赖,能够实现服务调用,负载均衡,容错等机制实现服务的发现与注册

Eureka
1.1 Eureka Server提供服务的注册服务
<!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false #表示不向注册中心注册自己
    fetch-registry: false #表示自己就是就是注册中心,用来维护服务的实例,并不需要去检索服务
    service-url:
      #设置与EurekaServer交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

主启动类

@SpringBootApplication
@EnableEurekaServer  //需要开启Eureka服务
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

测试:http://localhost:7001/

1.2 Eureka Client 通过注册中心进行访问

将提供者与消费者注册进去

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
@EnableEurekaClient
eureka:
  client:
    #表示自己需要到注册中心里面进行注册
    register-with-eureka: true
    #是否在Eureka Server里面抓取以有的注册信息
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
 spring:
  application:
    name: cloud-payment-service这个是注册到eureka里面的名称一定要进行设置
1.3 集群Eureka的搭建步骤

实现负载均衡与故障容错(互相注册,相互守望,也就是说自己能够关联到出自己之外所有的注册信息)

创建一个EurekaServer 与另一个一样

1.更改windows下面的host的映射文件

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

2.配置yml文件

在7001的yml里面 连接7002同理7002也是

server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称  这个名称在host文件里面进行了映射
  client:
    register-with-eureka: false #表示不向注册中心注册自己
    fetch-registry: false #表示自己就是就是注册中心,用来维护服务的实例,并不需要去检索服务
    service-url:
      #设置与EurekaServer交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7002.com:7002/eureka/

7002

server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名称
  client:
    register-with-eureka: false #表示不向注册中心注册自己
    fetch-registry: false #表示自己就是就是注册中心,用来维护服务的实例,并不需要去检索服务
    service-url:
      #设置与EurekaServer交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka/

测试:http://eureka7002.com:7002/或者localhost:7002都会显示指向对应的服务,形成一个集群的服务

3、客户端注册到集群里面

defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4、最好配置提供者的集群服务

  • 端口不能一致
  • 修改Controller 查看是否搭建成功
import org.springframework.beans.factory.annotation.Value;
@Value("${server.port}")
private String serverPort;

return new CommonResult(200,"插入数据成功"+serverPort,i);
        }else{
            return new CommonResult(666,"对不起插入数据是被"+serverPort,null);

5、修改消费者的Controller 根据名字

public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
因为这这个名字下面有两个提供者  随机进行选取 消费者只需要关注提供者的名称

需要开启LoadBalanced注解赋予RestTemplate负载均衡的功能

@Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }80里面使用@LoadBalanced进行负载均衡

补充:actuator微服务信息的完善

前提是:引入了web和actuator的配置文件

  • 主机名称:服务名称的修改 尽可能不把真实的主机名进行暴露
eureka:
  client:
    #表示自己需要到注册中心里面进行注册
    register-with-eureka: true
    #是否在Eureka Server里面抓取以有的注册信息
    fetch-registry: true
    service-url:
#      defaultZone: http://localhost:7001/eureka/
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
  instance:
    instance-id: payment8002  自定义主机名
  • 范文信息有IP信息的提示
instance:
    instance-id: payment8001
    prefer-ip-address: true

​ 服务发现Discovery

在8001的Controller中

@Resource
    private DiscoveryClient discoveryClient;
在自身进行测试就可以了
@GetMapping("/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for(String element:services){
            log.info("***element"+element);
        }
        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;
    }
@EnableDiscoveryClient

测试

@GetMapping("/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for(String element:services){
            //获取当前eureka上面的服务
            log.info("***element"+element);
        }
        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;
    }
    /**
     * ***elementcloud-payment-service
     * : ***elementcloud-order-service
     *  CLOUD-PAYMENT-SERVICE	172.17.226.28	8001	http://172.17.226.28:8001
     *  CLOUD-PAYMENT-SERVICE	172.17.226.28	8002	http://172.17.226.28:8002
     */
1.4、Eureka的自我保护

某一个时刻微服务不可用了,Eureka不会进行立即清理,依旧会对微服务的信息进行保存

也可以禁用保护模式 (网上查阅资料)

Zookeeper
1.5 Zookeeper的注册于发现

将Eureka换位Zookeeper

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
	controller   这个是服务的提供者
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    @GetMapping("/zk")
    public String paymentzk(){
        return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

因为使用了zooker的引入可能会出现zookeeper与jar包冲突的问题

解决冲突问题

在这里插入图片描述
测试:http://localhost:8004/payment/zk

特性:只要提供者取消,那么zookeeper就会将其进行删除,下一次启动就会产生一个新的

服务的消费者

server:
  port: 88
spring:
  application:
    name: cloud-consumerzk-order
  cloud:
    zookeeper:
      connect-string: 192.168.120.129:2181
 @Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderZkController {

    public static final String INVOKE_URL="http://cloud-provider-payment";
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getzk")
    public String getzk(){
        String result=restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
        return result;
    }
}
consul

是一种分布式的服务发现和配置管理

1、安装Consul

下载安装包,在Window下使用consul agent -dev进行运行

localhost:8500进行检验运行

添加pom

<dependency>
   <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

设置yml

server:
  port: 8005
spring:
  application:
    name: cloud-providerConsul-payment8005
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

消费者的配置与zk一样只不过是引入的jar包不同而已

Eureka zookeeper consul比较

在这里插入图片描述


补充:

spring cloud中discovery service有许多种实现(eureka、consul、zookeeper)等等@EnableDiscoveryClient基于spring-cloud-commons, @EnableEurekaClient基于spring-cloud-netflix其实用更简单的话来说,就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。(作用于提供者和消费者)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值