新建cloud-provider-zk-payment-8004
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--commons-->
<dependency>
<groupId>cn.zko0.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--替换掉eureka依赖,其他直接复制8001-->
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!--不需要下面的依赖-->
<!-- <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!–mysql-connector-java–>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>-->
<!--jdbc-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid-spring-boot-starter</artifactId>-->
<!-- <version>1.1.20</version>-->
<!-- <!–子工程写了版本号,就使用子工程的版本号,如果没写版本,找父工程中规定的版本号–>-->
<!-- </dependency>-->
</dependencies>
yml:
#端口号
server:
port: 8004
spring:
application:
#服务别名——注册到zookeeper注册中心的名称
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 101.43.244.40:2181 #linux的ip加暴露的端口号
入口类:
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
controller:
@Slf4j
@RestController
public class PaymentController {
@Value("${server.port}") //获取端口号
private String serverPort;
@RequestMapping("/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID().toString();
}
}
启动项目,进入zookeeper容器,查看服务注册:
查看zookeeper注册信息:
新建cloud-consumerzk-order80
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--commons-->
<dependency>
<groupId>cn.zko0.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--替换掉eureka依赖,其他直接复制8001-->
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
yml:
#端口号
server:
port: 80
spring:
application:
#服务别名——注册到zookeeper注册中心的名称
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 101.43.244.40:2181 #linux的ip加暴露的端口号
RestTemplate的Config:
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
Controller:
INVOKE_URL为远程调用的服务地址,有因为上面provider的服务名称为cloud-provider-payment,所以这里的URL为下面缩写的内容
@RestController
@Slf4j
public class OrderZKController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/payment/zk")
public String paymentInfo(){
String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
return result;
}
}
启动,测试查看服务注册信息,可以看到Provider和Consumer都被注册到了Zookeeper中:
测试远程调用:成功!!
至此Zookeeper整合Springcloud都完成了
当停掉服务,zookeeper会删除节点。
故:服务在zookeeper中是临时节点
CAP:C(一致性),A(可用性),P(分区容错)
Eureka开启保护模式,即为AP模式
当网络分区出现后,为了保证可用性,系统B可以返回旧值,保证系统的可用性。
结论:违背了一致性C的要求,只满足可用性和分区容错,即AP
Zookeeper继承Springcloud,服务即为CP模式
当网络分区出现后,为了保证一致性,就必须拒绝请求,否则无法保证一致性。
结论:违背了可用性A的要求,只满足一致性和分区容错,即CP
手机扫一扫
移动阅读更方便
你可能感兴趣的文章