Spring Cloud之整合ZK作为注册中心

Spring Cloud之整合ZK作为注册中心

Eureka已经闭源了,用zk可以替代之

Eureka 作为注册中心

Dubbo也是zk作为注册中心的

 Zookeeper简介

Zookeeper是一个分布式协调工具,可以实现服务注册与发现、注册中心、消息中间件、分布式配置中心等。

 Spring Cloud之整合ZK作为注册中心_第1张图片

公共pom:


		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
	
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.M7
				pom
				import
			
		
	
	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.cloud
			spring-cloud-starter-zookeeper-discovery
		

	
	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/libs-milestone
			
				false
			
		
	
  

  

package com.toov5.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务
public class zkMemberApiController {
    @Value("${server.port}")
    private String serverPort;
    
    
    @RequestMapping("/getMember")
  public String getMember() {
        return "会员服务prot:"+serverPort;
  }
    
    public static void main(String[] args) {
        SpringApplication.run(zkMemberApiController.class, args);
    }
}

###订单服务的端口号
server:
port: 8003
###服务别名----服务注册到注册中心名称
spring:
application:
name: zk-member
cloud:
zookeeper:
connect-string: 192.168.91.7:2181

 

启动后: yml配置文件的别名

Spring Cloud之整合ZK作为注册中心_第2张图片

通过json解析工具:

{
	"name": "zk-member",
	"id": "c01a3167-71c4-4d8a-8584-332c659e2d57",
	"address": "localhost",
	"port": 8002,
	"sslPort": null,
	"payload": {
		"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
		"id": "application-1",
		"name": "zk-member",
		"metadata": {}
	},
	"registrationTimeUTC": 1542086637317,
	"serviceType": "DYNAMIC",
	"uriSpec": {
		"parts": [{
			"value": "scheme",
			"variable": true
		}, {
			"value": "://",
			"variable": false
		}, {
			"value": "address",
			"variable": true
		}, {
			"value": ":",
			"variable": false
		}, {
			"value": "port",
			"variable": true
		}]
	}
}

  

 yml

###订单服务的端口号
server:
  port: 8002
###服务别名----服务注册到注册中心名称 
spring:
  application:
    name: zk-order
  cloud:
    zookeeper:
      connect-string: 192.168.91.7:2181

  controller

package com.toov5.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@SpringBootApplication
@EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务
public class zkOrderApiController {
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private RestTemplate restTemplate;
    
    @RequestMapping("/orderToMember")
  public String orderToMember() {
        String url ="http://zk-member/getMember"; 
        return restTemplate.getForObject(url, String.class);
  }
    
    public static void main(String[] args) {
        SpringApplication.run(zkOrderApiController.class, args);
    }
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

可以实现轮询

Spring Cloud之整合ZK作为注册中心_第3张图片

Spring Cloud之整合ZK作为注册中心_第4张图片

 

你可能感兴趣的:(Zookeeper,Spring,Cloud)