首先需要我们事先在电脑上或者服务器上成功安装Zookeeper
参考:Docker安装Zookeeper
一、创建子module项目 – 服务提供者
cloud-provider-zookeeper-payment8004
- pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.cloud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-zookeeper-payment8004</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<!-- 引用自己定义的api通用包 -->
<groupId>com.cloud.demo</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-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>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>
</project>
- appliction.yml文件
server:
port: 8004
spring:
application:
name: payment-provider-service
cloud:
zookeeper:
# zookeeper 注册地址
connect-string: 192.168.155.13:2181
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动包
url: jdbc:mysql://localhost:3306/db2020?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding-utr-8&useSSL=false
username: root
password: south755
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.demo.common.entity #所有Entity别名类所在包
- 业务代码
Controller、Service、Mapper 参考之前的
cloud-provider-payment8001
二、创建子module项目 – 服务消费者
cloud-consumer-zookeeper-order80
- pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.cloud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-zookeeper-order80</artifactId>
<dependencies>
<dependency>
<!-- 引用自己定义的api通用包 -->
<groupId>com.cloud.demo</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<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>
</dependencies>
</project>
- appliction.yml文件
server:
port: 80
spring:
application:
name: order-consumer-service
cloud:
zookeeper:
# zookeeper 注册地址
connect-string: 192.168.155.13:2181
- 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZookeeperMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZookeeperMain80.class, args);
System.out.println("OrderZookeeperMain80 启动成功");
}
}
- RestTemplate bean
@Configuration
public class ApplicationContextConfig {
@Bean
//RestTemplate并且开启了客户端负载均衡功能
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- controller
@Slf4j
@RestController
@RequestMapping("/Order")
public class OrderController {
@Resource
private RestTemplate restTemplate;
//用微服务服务名去调用服务
private static final String paymentUrl = "http://payment-provider-service/Payment/";
@GetMapping("/{id}")
public ResultModel getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(paymentUrl+id, ResultModel.class );
}
}
三、启动、测试
成功运行以上两个模块后,前往docker里zookeeper容器
可以看到我们两个服务均成功在zookeeper中注册
功能测试也正常运行
Zookeeper作为注册中心就简单到这里。