服务提供者
新建cloud-provider-payment8004
POM
<dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
</dependencies>
YML
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.161.128:2181
主启动类 使用@EnableDiscoveryClient(不用加@EnableEurekaxxx了)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author YangJunyi
*/
@SpringBootApplication
@EnableDiscoveryClient//该注解用于向使用consul或zookeeper作为注册中心时注册服务
public class PaymentMain8004 {
/**
* Method main ...
*
* @param args of type String[]
*/
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
Controller
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* Class PaymentController ...
*
* @author YangJunyi
* Created on 2020/7/29
*/
@RestController
@Slf4j
public class PaymentController {
/** Field serverPort */
@Value("${server.port}")
private String serverPort;
/**
* Method paymentzk ...
* @return String
*/
@GetMapping("/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
启动8004注册进zookeeper
如果zookeeper版本低于3.5.3-beta版 会报出jar包冲突错误
高版本的zookeeper无此错误
解决zookeeper的jar包冲突问题
排除zk冲突后的新pom
验证测试
连接进docker中的zookeeper再连接客户端, 查看根节点后
访问http://localhost:8004/payment/zk
验证测试2
在zookeeper节点中/services/服务名下回有一串流水号, get一下得到一个json串, 这个json串存放的是服务信息
思考 服务节点是临时节点还是持久节点(临时节点)
停止服务后, 在心跳结束后, zookeeper把节点删除
重新启动后, zookeeper会产生一个该服务的新流水号
使用cp思想, 保持数据一致性.
服务消费者
建module cloud-consumerzk-order80
POM
<dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
</dependencies>
YML
server:
port: 80
spring:
application:
name: cloud-consumerzk-order
cloud:
zookeeper:
connect-string: 192.168.161.128:2181
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Class OrderZKMain80 ...
*
* @author YangJunyi
* Created on 2020/7/29
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
/**
* Method main ...
*
* @param args of type String[]
*/
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
业务类
config包
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* Class ApplicationContextConfig ...
* @author YangJunyi
* @date 2020/7/23
*/
@Configuration
public class ApplicationContextConfig {
/**
* Method getRestTemplate returns the restTemplate of this ApplicationContextConfig object.
*
*
*
* @return the restTemplate (type RestTemplate) of this ApplicationContextConfig object.
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller包
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* Class OrderController ...
* @author YangJunyi
* @date 2020/7/23
*/
@RestController
@Slf4j
public class OrderController {
/** Field INVOKE_URL zookeeper中的注册地址*/
public static final String INVOKE_URL = "http://cloud-provider-payment";
/** Field restTemplate */
@Resource
private RestTemplate restTemplate;
/**
* Method getPayment returns the payment of this OrderController object.
*
*
*
* @return the payment (type String) of this OrderController object.
*/
@GetMapping("/consumer/payment/zk")
public String getPayment(){
return restTemplate.getForObject(INVOKE_URL + "/payment/zk" , String.class);
}
}
验证
访问地址测试
http://localhost/consumer/payment/zk