避坑
- 关闭防火墙
- 修改tocat端口解决8080端口占用
- 解决zookeeper maven冲突
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>安装版本号</version>
</dependency>
相关命令
zookeeper
- 启动: sh bin/zkServer.sh start
- 查看服务状态: sh bin/zkServer.sh status
- 停止: sh bin/zkServer.sh stop
- 重启: sh bin/zkServer.sh restart
- get /services/application/2bf4ff71-16ad-4965-b7ba-1e095c29dd7f
tomcat占用8080端口 换点端口号
sudo find / -name tomcat
找到tomcat/conf/server.xml 修改端口号
防火墙
- 状态
systemctl status firewalld - 开启
systemctl start firewalld - 停止
pkill -f firewall //杀死进程
systemctl stop firewalld - 开机启动
systemctl enable firewalld - 禁用开机启动
systemctl disable firewalld
简单实现
开启zookeeper
生产者服务注册
maven
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.jia.learn</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
</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>
配置文件
server:
port: 8004
spring:
application:
name: cloud-payment-service
cloud:
zookeeper:
# zookeeper ip+port
connect-string: 114.55.173.209:2181
启动类注解
consul/zookeeper作为服务中心注入服务
@EnableDiscoveryClient
Controller
package com.jia.learn.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @Description:
* @Author: LIANG
* @Data: 2021/12/10 09:36:24
*/
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Value("${server.port}")
private String port;
@GetMapping("/zk")
public String paymentzk(){
return "zookeeper port:"+port+ UUID.randomUUID().toString();
}
}
消费者服务注册
maven
<dependencies>
<dependency>
<groupId>com.jia.learn</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>
<!--先排除自带的zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</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>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>
配置文件
server:
port: 8000
spring:
application:
name: cloud-payment-consumerzk
cloud:
zookeeper:
# zookeeper ip+port
connect-string: 114.55.173.209:2181
启动类注解
@EnableDiscoveryClient
配置类配置RestTemplate负载均衡
@LoadBalanced
Controller
package com.jia.learn.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @program: SpringCloudLearn
* @description
* @author: LIANG
* @create: 2021-12-08 22:27
**/
@RestController
@RequestMapping("/consumer")
public class OrderZKController {
public static final String INVOKE_URL="http://CLOUD-PAYMENT-SERVER";
@Resource
private RestTemplate restTemplate;
@GetMapping(value="/payment/zk")
public String paymentInfo(){
String result = restTemplate.getForObject(INVOKE_URL + "/paymennt/zk", String.class);
return result;
}
}
查看服务
[root@iZbp1im64zbi66krifox9fZ bin]# ./zkCli.sh
Connecting to localhost:2181
*********************
[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /services
[cloud-payment-consumerzk, cloud-payment-service]