Spring Cloud使用zookeeper作为服务注册中心与配置中心

查看zk ./zkCli.sh -server localhost:2181

ls /test

服务注册:service-app

 
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        
    
启动后,http://localhost:9090/list   

["http://192.168.1.124:9090"]
进入zkClient.sh:

get /services/service-app/7fc53fc1-ad65-4ec1-b9e9-0c541e3cc8e1
{"name":"service-app","id":"7fc53fc1-ad65-4ec1-b9e9-0c541e3cc8e1","address":"192.168.1.124","port":9090,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"service-app:9090","name":"service-app","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1515120268159,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
cZxid = 0x264
ctime = Fri Jan 05 10:44:28 CST 2018
mZxid = 0x264
mtime = Fri Jan 05 10:44:28 CST 2018
pZxid = 0x264
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1004c4c55320005
dataLength = 537
numChildren = 0
服务调用: service-app-client

@Component
@FeignClient(value="service-app",fallback=ZkServiceHystrix.class)
public interface ZkServiceProviderClient {
    //等同于service-app/hi
    @RequestMapping(method = RequestMethod.GET, value = "/hi")
    public String what();

}


配置中心:

获取配置的规则

  • 假设:

spring.cloud.zookeeper.config.root=xxxx ;

spring.application.name=abc

  • zk 路径:

/xxxx/abc/com/gabo/username

  • 取值:

//不能动态更新值,需要重启项目

@Value("${com.xxx.username}")Spring Cloud使用zookeeper作为服务注册中心与配置中心_第1张图片

@Data
//可以动态修改值,不需要重启
@ConfigurationProperties(prefix = "datasource")
public class Myconf2 {
    private String url;
    private String pass;
}
@SpringBootApplication
@EnableDiscoveryClient
//@EnableConfigServer
@EnableAutoConfiguration
@EnableConfigurationProperties({Myconf.class,Myconf2.class})
//支持多个配置类 @EnableConfigurationProperties({xxx.class,abc.class})
public class ZookeeperApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZookeeperApplication.class, args);
    }
}
项目地址: https://github.com/nick8sky/spring-clouds/blob/master/cloud-zookeeper-provider/
Spring Cloud使用zookeeper作为服务注册中心与配置中心_第2张图片

你可能感兴趣的:(Spring-cloud系列)