关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

springcloud整合zookeeper注册中心

发布时间:2023-07-01 00:28:31
首先本地下载一个zookeeper,https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 下载3.4的,3.5 的解压的时候回出现文件已存在的错误,解压完会多出来一个文件夹,然后启动的时候找不到jar包报错。 下载完解压,进入conf文件夹,把zoo_sample.cfg文件改名为zoo.cfg。然后改一下里面的dataDir=D:\zookeeper-3.4.14\data 临时文件存放位置,一个路径。进入bin文件夹,双击zkServer.cmd启动zookeeper服务。 下载zookeeper图形化客户端 ZooInspectorhttps://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 解压后双击build文件夹中的那个jar包。点击左上角启动按钮连接zookeeper 新建一个maven项目, pom文件 org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom import org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.4.10 org.slf4j slf4j-log4j12 spring-milestones Spring Milestones https://repo.spring.io/milestone false repository.springframework.maven.release Spring Framework Maven Release Repository http://maven.springframework.org/milestone/ org.springframework http://maven.springframework.org/snapshot spring-milestone Spring Maven MILESTONE Repository http://repo.spring.io/libs-milestone spring-release Spring Maven RELEASE Repository http://repo.spring.io/libs-release org.springframework.boot spring-boot-maven-plugin 启动类 package com.vhukze.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; @SpringBootApplication @RestController @EnableDiscoveryClient //如果服务使用consoul、zookeeper,便可以使用此注解开启服务 public class ZkMemberController { @Value("${server.port}") private String serverPort; @RequestMapping("getMember") public String getMember() { return "订单服务调用会员服务接口,端口号为:"+serverPort; } public static void main(String[] args) { SpringApplication.run(ZkMemberController.class, args); } } 配置文件 ###订单服务的端口号 server:  port: 8002 ###服务别名---服务注册到注册中心的名称 spring:  application:    name: zk-member  cloud:    zookeeper:    ###注册到的zookeeper地址      connect-string: 127.0.0.1:2181 再新建一个maven项目 pom文件同上 启动类 package com.vhukze.controller; import org.springframework.beans.factory.annotation.Autowired; 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; /** * Hello world! * */ @SpringBootApplication @EnableDiscoveryClient @RestController public class App { @Autowired private RestTemplate template; @RequestMapping("orderToMember") public String orderToMember() { String memberUrl = "http://zk-member/getMember"; return template.getForObject(memberUrl, String.class); } public static void main( String[] args ) { SpringApplication.run(App.class, args); } //默认rest方式开启负载均衡,如果以服务别名调用,必须注入此对象 @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } } 配置文件 ###订单服务的端口号 server: port: 8004 ###服务别名---服务注册到注册中心的名称 spring: application: name: zk-order cloud: zookeeper: ###注册到的zookeeper地址 connect-string: 127.0.0.1:2181 启动第一个项目,然后改一个端口号再启动一遍,相当于一个集群,然后启动第二个项目。这时候 查看ZooInspector,便有三个服务了。 访问第二个项目的接口方法,刷新会变端口号,负载均衡的轮询机制

/template/Home/leiyu/PC/Static