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

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

ls /test

服务注册:service-app

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
    </dependencies>
启动后,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}")

@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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值