文章目录


一、consul的下载安装

consul的下载地址:​https://www.consul.io/downloads​

零基础带你快速入门consul-难道consul还能这样用?_xml


解压之后

零基础带你快速入门consul-难道consul还能这样用?_后端_02


在该路径下以cmd的方式打开

然后启动

consul agent -dev

零基础带你快速入门consul-难道consul还能这样用?_后端_03




二、框架搭建

1、创建父工程

零基础带你快速入门consul-难道consul还能这样用?_spring cloud_04


零基础带你快速入门consul-难道consul还能这样用?_后端_05


删除src目录

2、创建服务提供子模块(provider-consul-module)

零基础带你快速入门consul-难道consul还能这样用?_maven_06


零基础带你快速入门consul-难道consul还能这样用?_maven_07

3、创建服务消费子模块(consumer-consul-module)

零基础带你快速入门consul-难道consul还能这样用?_maven_06


零基础带你快速入门consul-难道consul还能这样用?_xml_09


零基础带你快速入门consul-难道consul还能这样用?_maven_10




二、代码准备

1、父工程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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.cloud</groupId>
<artifactId>springcloud_all</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>provider-consul-module</module>
<module>consumer-consul-module</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.13</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.19</mysql.version>
<druid.version>1.2.4</druid.version>
<mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>
<druid.spring.boot.version>1.2.4</druid.spring.boot.version>
<springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version>
<springfox-swagger2.version>2.9.2</springfox-swagger2.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud 2020.0.2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger-ui.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>

2、服务提供子模块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>springcloud_all</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.provider</groupId>
<artifactId>provider-consul-module</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

3、消费服务子模块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>springcloud_all</artifactId>
<groupId>com.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.consumer</groupId>
<artifactId>consumer-consul-module</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

4、提供服务模块和消费模块服务模块启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulProviderApplication.class, args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(ConsulConsumerApplication.class, args);
}
}

@EnableDiscoveryClient注解的作用:能够让注册中心能够发现,扫描到微服务。

5、提供服务模块和消费模块服务模块application.yml文件

server:
port: 8006
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
service-name: ${spring.application.name}
application:
name: provider-consul-module
server:
port: 80
spring:
application:
name: consumer-consul-module
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
service-name: ${spring.application.name}

6、提供服务模块和消费模块服务模块controller代码

@RestController
@Slf4j
public class ProviderConsulController {
@Value("${server.port}")
private String serverPort;

@GetMapping(value = "/provider/test")
public String test() {
return "提供服务模块服务端口号为:" + serverPort + "《--------------》" + UUID.randomUUID().toString();
}
}
@RestController
public class ConsumerConsulController {
private static final String INVOKE_URL = "http://provider-consul-module";

@Resource
private RestTemplate restTemplate;

@GetMapping(value = "/consumer/test")
public String getData() {

return restTemplate.getForObject(INVOKE_URL + "/provider/test", String.class);
}
}

7、RestTemplate详解及其配置

RestTemplate是spring封装的一个服务端远程HTTP请求工具,支持常见的Rest请求(get、post等)
简单了说就是用来远程调用的,因为你订单模块是一个服务,消费模块又是一个单独的服务,两个服务端的端口号要进行交互,所以就有了RestTemplate

当然要注入RestTemplate(注意是在消费模块配置,因为是消费服务调用提供服务模块)

@Configuration
public class ApplicationContextConfig {

@Bean
@LoadBalanced
public RestTemplate AutoRestTemplate() {
return new RestTemplate();
}
}




三、测试

启动提供服务模块和消费服务模块(注意一定要开启zookeeper服务,顺便进入zookeeper客户端)

零基础带你快速入门consul-难道consul还能这样用?_后端_11


访问http://localhost:8500,你可以看到consul的客户端界面(然后我们启动的两个服务已经成功注册进consul)

零基础带你快速入门consul-难道consul还能这样用?_后端_12


接口测试

零基础带你快速入门consul-难道consul还能这样用?_spring_13


成功!



四、灵魂拷问?为什么需要注册中心

我们在微服务中首先需要面对的问题是服务之间如何进行通信呢?在单体架构中,我们只要将服务的接口暴露,然后通过http请求即可,微服务当然也可以这样。
问题是我们请求的url是固定的,单体服务为我们还可以很快的修改;一旦有大量的服务那就需要我们手动维护服务接口,配置负载均衡;服务宕机后,如何及时下线呢?
总而言之注册中心必不可少(至少当前时代是这样)。