目录
我的上一篇博客文章:
初学SpringCloud:使用ZooKeeper作为服务注册中心,搭建服务提供者的模块
1、背景介绍
目前我本人使用的是ZooKeeper作为服务注册中心。已经在Linux的CentOS7版本的操作系统下搭建成功了。并且CentOS7操作系统我本人在安装的时候,进行了必要选项的勾选,能够进行图形化界面的操作,并且本身已经默认可以联网,跟我的本机已经连通了。采用的是默认的NAT模式。我本人在我的上一篇博客中也是搭建成功了一个服务的通过者模块,以8004作为端口。
2、本篇博客的目的
本篇博客我要搭建一个以90为端口的微服务模块,然后在使用ZooKeeper作为服务注册中心的条件下,使得这个微服务模块能够调用以8004为端口的微服务模块提供的服务。(其实就是一个RestTemplate)
3、前提条件
在搭建这个模块之前,已经完成的工作包括:安装了VMware,安装了CentOS7操作系统,在CentOS7操作系统上安装成功了JDK和ZooKeeper并且配置了环境变量,已经启动了CentOS7上面的ZooKeeper,已经搭建好了8004为端口的微服务模块并且正常启动,虚拟机和本机可以ping通。
以上这些前提准备条件在我上面的几篇博客中都有,欢迎前往查看。
4、我的文件结构
5、本模块的代码部分
1、我的POM文件
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>cloud2023</artifactId>
<groupId>com.lanse.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerzk-order80</artifactId>
<dependencies>
<dependency>
<groupId>com.lanse.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这个POM文件中都是非常常见的依赖,这里我就不多讲了。
第一个依赖,是我把每个微服务模块都会使用到的公共的一些实体类,抽出来组成了一个模块,然后这个模块使用install和package命令,打成了一个jar包,最后在依赖中引入进来了。
第四个依赖,是使用ZooKeeper作为服务注册中心必须的依赖。
2、我的application文件
server:
port: 90
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.52.131:2181 #一个zookeeper机器的ip以及端口号
这个模块的application文件是更加的简单的
首先就是设置一下端口90,然后设置了注册进服务注册中心的名字
最后是设置了服务注册中心的地址。这个地址就是在CentOS7上面安装的ZooKeeper,然后查看了这台虚拟机的IP,最后的2181是默认的端口号。
3、我的controller层的代码
由于我的业务逻辑设计的是比较的简单的,因此呢在这里也就不再设计Service层甚至是Dao层了。
package com.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class OrderZKController {
public static final String INVOKE_URL="http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/payment/zk")
public String paymentInfo(){
String result= restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
return result;
}
}
上面就是我的Controller层的代码,确实是比较的简单。
首先就是老生常谈的@RestController注解=@Controller+@ResponseBody
然后是LomBok插件的@Slf4j注解,这个注解可以使用log.info()方法,也就是打印日志什么的,其实在我的这个代码中目前来说并没有使用到这个注解带来的功能。
再往下就进入了类的内部,先是一个静态的字符串,这个字符串就是为了调用8004为端口的服务所使用的。因为我在8004为端口的模块的application文件中设置了spring.application.name属性为cloud-provider-payment,这个名字就是要注册进服务注册中心的,所以是可以使用的。其实最原始的应该是:localhost:8004。 还要注意一点:这样使用的话还要做一些配置,也就是赋予RestTemplate类的负载均衡的能力。
再朝下的话,就是将RestTemplate类注入进来一个实体。这个是需要有对应的配置的,后面会讲到的。
再朝下的话,就是一个方法了。这个方法就调用了RestTemplate类的我们注入的对象的getForObject()方法,用来发送我们的HTTP请求并且得到返回值。关于这个方法的用法和参数,我在我前面的博客中简单的介绍过。这里就不再介绍了。但是由于我本人也是才疏学浅,所以介绍的也不是非常的深入。
4、我的config文件夹
里面只有一个类,代码如下所示:
package com.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
首先既然是配置,它就是相当于原先的Spring框架下的xml配置文件,因此需要有 @Configuration注解。注意这个注解是需要加在类上面的。
然后再往下就进入到类体当中了。首先就是@Bean注解,这个注解的作用就是相当于我们原先使用Spring框架的时候,xml文件中的Bean标签。
再往下的话,就是@LoadBalanced注解,这个注解就是赋予了RestTemplate类的负载均衡的能力。
最后就是方法体了,直接返回了一个RestTemplate对象。
对于这样写的具体的原因和细节,其实我本人也不是非常的明白。我只能简单的讲解到这里了。后面我本人会尽力的去提升自己的知识的,等后面我本人有了新的认识以后,会再具体额说明的。
5、我的主启动类
代码如下:
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class,args);
}
}
主启动类已经是比较的简单。
这里值得说明的就是 @EnableDiscoveryClient注解了。
由于我目前使用的是ZooKeeper作为服务注册中心的,因此无论是服务提供者还是服务消费者,都是需要使用这个注解的。本质上它们两个都是要注册进服务注册中心的Client端。
作为对比,当我们使用Eureka作为服务注册中心的时候,使用的就是@EnableEurakaClient注解。
6、结语
到此为止,消费者模块的搭建工作也完成了。在满足前提条件的情况下,依次启动服务提供者模块和服务消费者模块,然后按照设定的@RequestMapping地址,访问地址栏,就可以得到返回的结果。
这里我就不再展示运行成功的截图了。(也确实都启动的话也比较的麻烦,哈哈哈哈哈,我比较懒)
有任何的问题,欢迎在评论区留言!!