eureka已经不再更新,代替方案有zookeeper或consoul
zookeeper是一个分布式的协调工具,可以实现服务注册中心
linux 安装zookeeper(https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/)
https://blog.51cto.com/u_4837471/2466606
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
1. cloud-provider-payment8004(服务提供者)com.eiletxie.springcloud cloud-api-commons ${project.version} org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.4.14 org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
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 Payment8004 {
public static void main(String[] args) {
SpringApplication.run(Payment8004.class,args);
}
}
@RestController
@Slf4j
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();
}
}
如果不排除自带的3.5.3-zookeeper-beat会报jar包冲突
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
org.apache.zookeeper
zookeeper
验证 http://localhost:8004/payment/zk2.cloud-consumerzk-order80(服务的消费者)
org.example cloud-api-commons 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.4.14 org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server:
port: 80
#服务别名 --- 注册zookeeper到注册中心名称
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZK80 {
public static void main(String[] args) {
SpringApplication.run(OrderZK80.class,args);
}
}
@RestController
public class OrderZkController {
private static final String INVOKE_URL = "http://cloud-provider-payment";
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk")
public String consumer() {
String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
return result;
}
}
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
验证 http://localhost/consumer/payment/zk



