- 关闭防火墙
- 修改tocat端口解决8080端口占用
- 解决zookeeper maven冲突
相关命令 zookeeperorg.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper 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
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
配置文件org.springframework.boot spring-boot-starter-web com.jia.learn cloud-api-commons 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.6.3 org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server:
port: 8004
spring:
application:
name: cloud-payment-service
cloud:
zookeeper:
# zookeeper ip+port
connect-string: 114.55.173.209:2181
启动类注解
Controllerconsul/zookeeper作为服务中心注入服务
@EnableDiscoveryClient
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;
@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
配置文件com.jia.learn cloud-api-commons ${project.version} org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.6.3 slf4j-log4j12 org.slf4j org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.mybatis.spring.boot mybatis-spring-boot-starter org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server:
port: 8000
spring:
application:
name: cloud-payment-consumerzk
cloud:
zookeeper:
# zookeeper ip+port
connect-string: 114.55.173.209:2181
启动类注解
@EnableDiscoveryClient
配置类配置RestTemplate负载均衡@LoadBalanced
Controllerpackage 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;
@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]



