目录
1.启动zookeeper??????
2.创建8004模块???????
?3.临时节点还是永久节点??????
4.创建zk80消费模块??????
./zkServer.sh start? ?和 ./zkCli.sh -server 主机名
在父工程下创建模块
注意:jdk版本和,maven版本
1.springboot依赖
2.mybatis依赖
3.druid依赖
4.mysql依赖
5.zookeeper依赖
<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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.16</version>
</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>
<!--引入自己的api通用包-->
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--springboot整合zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
1.服务端口
2.服务名称
3.数据库连接池
4.zookeeper连接地址
5.mybatis配置
server:
port: 8004
spring:
application:
#服务名称
name: cloud-provider-payment
datasource:
#druid连接池
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud
username: root
password: 123456
#zookeeper服务注册中心
cloud:
zookeeper:
connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xz.springcloud.entity
@EnableDiscoveryClient:将服务注册到服务注册中心(zookeeper)
@SpringBootApplication
@EnableDiscoveryClient//作为服务中心
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class);
}
}
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/getById/{id}")
public CommonResult getById(@PathVariable("id") Integer id){
Payment payment = paymentService.getPaymentById(id);
if (payment!=null){
return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
}else {
return new CommonResult(404,"查询无果,serverPort:"+serverPort,null);
}
}
@RequestMapping("/payment/zk")
public String paymentZk(){
return "springCloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
?说明zookeeper配置成功
当关闭8004服务时,在查询zookeeper,发现节点剔除
所以:zookeeper存储的是临时节点,当服务消失时,会马上剔除?
在父工程下创建模块
注意jdk版本和maven版本
1.springboot依赖
2.通用依赖
3.zookeeper依赖
<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.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>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--spring boot整合zookeeper-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
1.服务端口
2.服务名
3.zookeeper连接ip(我这里使用集群)
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.20.129:2181,192.168.20.130:2181,192.168.20.131:2181
@EnableDiscoveryClient添加上
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZkMain80.class);
}
}
注意:因为访问的是服务名,而不是具体的ip,所以加上@LoadBalance注解
@Configuration
public class ApplicationConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
1.声明常量,表示zookeeper中8004的服务名
2.注入RestTemplate
3.调用restTemplate访问
@RestController
public class OrderController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/payment/getById/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Integer id){
return restTemplate.getForObject(INVOKE_URL+"/payment/getById/"+id,CommonResult.class);
}
@GetMapping("/payment/zk")
public String getDiscovery(){
return restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
}
}
?