zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后启动zookeeper服务器
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
version已经交给父工程管理
-
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <junit.version>4.12junit.version>
- <log4j.version>1.2.17log4j.version>
- <lombok.version>1.16.18lombok.version>
- <mysql.version>8.0.16mysql.version>
- <druid.version>1.1.16druid.version>
- <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
- properties>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>mscloud03artifactId>
- <groupId>com.atguigu.springcloudgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>cloud-provider-payment8004artifactId>
-
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>com.atguigu.springcloudgroupId>
- <artifactId>cloud-api-commonsartifactId>
- <version>${project.version}version>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
-
- <exclusions>
- <exclusion>
- <groupId>org.apache.zookeepergroupId>
- <artifactId>zookeeperartifactId>
- exclusion>
- exclusions>
- dependency>
-
- <dependency>
- <groupId>org.apache.zookeepergroupId>
- <artifactId>zookeeperartifactId>
- <version>3.4.9version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- project>
- #8004表示注册到zookeeper服务器的支付服务提供者端口号
- server:
- port: 8004
- #服务别名----注册zookeeper到注册中心名称
- spring:
- application:
- name: cloud-provider-payment
- cloud:
- zookeeper:
- connect-string: 192.168.111.144:2181
- @SpringBootApplication
- @EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
- public class PaymentMain8004
- {
- public static void main(String[] args)
- {
- SpringApplication.run(PaymentMain8004.class,args);
- }
-
- }
- @RestController
- public class PaymentController
- {
- @Value("${server.port}")
- private String serverPort;
-
- @RequestMapping(value = "/payment/zk")
- public String paymentzk()
- {
- return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
- }
- }
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>mscloudartifactId>
- <groupId>com.atguigu.springcloudgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>cloud-consumerzk-order81artifactId>
-
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
-
- <exclusions>
- <exclusion>
- <groupId>org.apache.zookeepergroupId>
- <artifactId>zookeeperartifactId>
- exclusion>
- exclusions>
- dependency>
-
- <dependency>
- <groupId>org.apache.zookeepergroupId>
- <artifactId>zookeeperartifactId>
- <version>3.4.9version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- project>
-
- server:
- port: 80
-
- spring:
- application:
- name: cloud-consumer-order
- cloud:
- #注册到zookeeper地址
- zookeeper:
- connect-string: 192.168.231.136:2181
- @SpringBootApplication
- @EnableDiscoveryClient
- public class OrderZK80 {
- public static void main(String[] args) {
- SpringApplication.run(OrderZK80.class,args);
- }
- }
- @Configuration
- public class ApplicationContextBean
- {
- @Bean
- @LoadBalanced//负载均衡,虽然在这里没有用到
- public RestTemplate getRestTemplate()
- {
- return new RestTemplate();
- }
- }
- @RestController
- public class OrderZKController
- {
- public static final String INVOKE_URL = "http://cloud-provider-payment";
-
- @Autowired
- private RestTemplate restTemplate;
-
- @RequestMapping(value = "/consumer/payment/zk")
- public String paymentInfo()
- {
- String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class);
- System.out.println("消费者调用支付服务(zookeeper)--->result:" + result);
- return result;
- }
-
- }