Spring Cloud使用zookeeper作为服务注册中心与配置中心 - Go语言中文社区

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/

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/llianlianpay/article/details/78978131
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 22:18:33
  • 阅读 ( 1523 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢