上一讲我们已经讲将支付服务进zookeeper完成,结合下图,可以发现我们还需要将订单服务注册进zookeeper。
(1)建工程
新建消费者模块cloud-consumerzk-order80。
(2)写pom/yml
pom和yml直接复制8004。(yml中端口号改为80,应用名改为cloud-consumer-order,其他都相同)
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>cloud2020</artifactId> <groupId>com.wangzhou.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-order80</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- 这两个依赖一般绑定在一起使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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> <!--引入自定义的api通用包,可以使用Payment支付bean--> <dependency> <groupId>com.wangzhou.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <!--替换掉eureka依赖--> <!--SpringBoot整合Zookeeper客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <exclusions> <!-- 先排除自带的zookeeper3.5.3--> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加zookeeper3.4.10版本(引入对应版本的依赖)--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
application.yml
#端口号 server: port: 80 spring: application: #服务别名——注册到zookeeper注册中心的名称 name: cloud-consumer-order cloud: zookeeper: connect-string: 192.168.106.43:2182,192.168.106.44:2182,192.168.106.45:2182 #linux的ip加暴露的端口号 autoconfigure: exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
(3)主启动类
主启动类。(与8004相同)
@SpringBootApplication @EnableDiscoveryClient public class OrderZKMain80 { public static void main(String[] args) { SpringApplication.run(OrderZKMain80.class,args); } }
(4)业务类
先写配置类。返回restTemplate。
@Configuration public class Applicationcontextconfig { @Bean @LoadBalanced //负载均衡 public RestTemplate getRestTemplate() { return new RestTemplate(); } }
再写controller.
@RestController @Slf4j public class ZKController { public static final String INVOKE_URL="http://cloud-provider-payment"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/zk") public String paymentInfo(){ // url拼接上/payment/zk,对应8004服务controller中的rest api String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class); return result; } }
启动zookeeper集群(如果没有搭建可以参考:zookeeper入门到精通03——zookeeper集群搭建
),启动8004支付微服务,80订单微服务。
在zookeeper服务器上执行命令,可以看到订单服务注册成功。
访问rest api,进行调用功能测试如下。