亲宝软件园·资讯

展开

springcloud consul服务治理

求知若渴的蜗牛 人气:0

Consul是一款由HashiCorp公司开源的,用于服务治理的软件,Spring Cloud Consul对其进行了封装。Consul具有如下特点:

Consul agent有两种运行模式:Server和Client。这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上 的应用服务无关。 以Server模式运行的Consul agent节点用于维护Consul集群的状态,官方建议每个Consul Cluster至少有3个或以上的运行在Server mode的Agent,Client节点不限。

安装Consul

Consul下载地址:https://www.consul.io/downloads.html,本文选择windows 64bit 版本进行演示。

下载后解压,然后在解压目录下运行./consul命令:

可以看到Consul所包含的命令,使用consul [命令] --help可以查看某个命令的具体用法。

执行下面这条命令来启动一个Consul agent

-dev表示创建一个开发环境下的server节点,在该模式下不会有任何持久化操作,即不会有任何数据写入到磁盘,所以这个模式适合用于开发过程,而不适用于生产环境。

启动后,默认的端口号为8500,访问http://localhost:8500

当前就一个consul服务。接下来我们开始创建服务提供者和服务消费者。

cloud-provider-consul-payment8005 服务提供者

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</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>cloud-provider-consul-payment8005</artifactId>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
 
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
 
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-conmmon</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--swagger 整合swagger需要的依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <!--swagger ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
        <!--SpringBoot热部署配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
 
        <!--consul依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
 
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

编写application.yml配置文件

server:
  port: 8005
spring:
  application:
    name: server-provider-consul
  cloud:
    consul:
      host: localhost  #consul的ip地址
      port: 8500    #consul的段端口号
      discovery:
        register-health-check: false  #不进行检查机制
        service-name: ${spring.application.name}

然后在配置文件里添加如下配置:

spring.cloud.consul.hostspring.cloud.consul.port配置了consul的ip和端口;spring.cloud.consul.discovery.service-name配置了该服务在consul里注册的服务名称;spring.cloud.consul.discovery.register-health-check用于开启健康检查,spring.cloud.consul.discovery.health-check-interval配置了健康检查的周期为10秒,spring.cloud.consul.discovery.health-check-path配置了健康检查路径。

接着新建ConsulController

package com.shiwen.controller;
 
import com.shiwen.entity.CommonResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.UUID;
 
/**
 * @author wangjie
 * @Title: ConsulController
 * @Description: consul测试类
 * @company: 西安石文软件有限公司
 * @date 2020/10/910:44
 */
@RestController
@RequestMapping("/consul")
public class ConsulController {
 
    @Value("${server.port}")
    private String serverPort;
 
    @RequestMapping("/payment")
    public CommonResult getPayment(){
        return  new CommonResult(200,"consul"+serverPort, UUID.randomUUID().toString());
    }
 
}

最后,要开启服务注册与发行,需要在Spring Boot入口类上添加@EnableDiscoveryClient注解:

package com.shiwen;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 
/**
 * @author wangjie
 * @Title: ProviderConsulApplication8005
 * @Description: consul服务治理
 * @company: 西安石文软件有限公司
 * @date 2020/10/910:28
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderConsulApplication8005 {
    public static void main(String[] args) {
        SpringApplication.run(ProviderConsulApplication8005.class,args);
    }
}

准备完毕后,打包项目,然后启动两个实例,端口号分别为8005和8006,启动后,再次访问consul管理界面:

服务提供者注册成功,接下来开始搭建服务消费者。

cloud-provider-consul-order80

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</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>cloud-provider-consul-order80</artifactId>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <!--swagger 整合swagger需要的依赖-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <!--swagger ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
 
        <!--SpringBoot热部署配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
 
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cloud-api-conmmon</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
 
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <fork>true</fork> <!-- fork: 如果没有配置该项配置,devtools不会起作用的,即应用不会restear -->
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

配置application.yml:

server:
  port: 80
spring:
  application:
    name: server-consumer
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        register-health-check: false
        service-name: ${spring.application.name}

同样的,需要开启服务注册与发现需要在入口类上添加@EnableDiscoveryClient注解。

接着创建ConsulController来消费cloud-provider-consul-payment8005提供的payment服务:

package com.shiwen.controller;
 
import com.shiwen.entity.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
/**
 * @author wangjie
 * @Title: ConsulController
 * @Description: 调用
 * @company: 西安石文软件有限公司
 * @date 2020/10/911:57
 */
@RestController
@RequestMapping("/order")
public class ConsulController {
 
    private String sonsulUrl="http://server-provider-consul";
 
    @Autowired
    private RestTemplate restTemplate;
 
    @RequestMapping("/info")
    public CommonResult getCommonInfor(){
       return restTemplate.getForObject(sonsulUrl+"/consul/payment",CommonResult.class);
    }
}

sonsulUrl的值为服务提供者在consul注册中心的实例名称,即server-provider-consul。通过DiscoveryClient我们可以获取到所有名称为server-provider-consul的服务实例信息。consul内置了Ribbon,所以我们还可以通过@LoadBalanced标注的RestTemplate来实现负载均衡服务调用打包部署项目,

访问:http://localhost/order/info

再次访问http://localhost/order/info

加载全部内容

相关教程
猜你喜欢
用户评论