zookeeper学习
学习步骤:
1.zookeeper服务启动
2.建module
3.改pom, 加入zookeeper相关依赖
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
3.1.1
4.写yml配置文件
#该服务的端口号
server:
port: 8004
spring:
application:
name: cloud-payment-server # 该服务的名称
cloud:
zookeeper:
discovery:
enabled: true # 开启服务
connect-string: 192.123.32.19:2181 #服务注册中心(zookeeper)地址
connection-timeout: 1000000 # 连接超时时间
5.主启动
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderPaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(ProviderPaymentMain8004.class, args);
}
}
6.写业务
@RestController
@Slf4j
public class PaymentController8004 {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/pro")
public String payment(){
return "payment serverPort ==>" + serverPort + UUID.randomUUID().toString();
}
}
// 配置类
//使用RestTemplate 远程调用(底层使用的还是HttpClient)
@Configuration
public class BeanConf {
@Bean
// 开启负载均衡
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
// 控制器
@RestController
@Slf4j
public class ConsumerZkOrder80Controller {
@Autowired
private RestTemplate restTemplate;
// 远程服务地址(服务提供方)
private static final String INVOKE_URL = "http://cloud-payment-server";
@GetMapping("/remote/server")
public String remoteService(){
return restTemplate.getForObject(INVOKE_URL + "/payment/pro",String.class);
}
}