服务提供者
作用:会自动的在zookeeper中/servers下创建结点,当服务器掉线后会马上结束删除结点
1.编写pom文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.编写yaml文件
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.12.1:2181
3.编写主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class paymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(paymentMain8004.class, args);
}
}
4.启动,查看
将当前服务注册到了zookeeper中 
服务消费者
作用:会自动的在zookeeper中/servers下创建结点,当服务器掉线后会马上结束删除结点。 同时可以通过提供的服务器名来代替ip地址和端口号
1.编写pom文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.编写yaml文件
server:
port: 8004
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.12.1:2181
3. 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class paymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(paymentMain8004.class, args);
}
}
4.配置类
@Configuration
public class ZookeeperConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5.业务类
@Controller
@ResponseBody
public class demo {
@Autowired
RestTemplate restTemplate;
public static final String INVOKE_URL="http://cloud-provider-payment";
@GetMapping("/demo")
public String demo(){
String forObject = restTemplate.getForObject(INVOKE_URL + "/take", String.class);
return forObject;
}
}
6.启动,查看

|