spring-cloud03-consul

  官网的安装说明https://learn.hashicorp.com/tutorials/consul/get-started-install

1.下载安装

  环境:阿里云服务器,consul1.9.5,

  consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。

  consul用于微服务下的服务治理,主要特点有:服务发现、服务配置、健康检查、键值存储、安全服务通信、多数据中心等

1.1下载

  https://www.consul.io/downloads

  

     选择linux版本下载

  1.2.上传到服务器

  

  1.3解压

  命令:unzip consulzip文件名字

  

  1.4把解压出来的consul文件移动到可用目录下

  命令:echo $PATH 查看可用目录,下面列出了很多目录,冒号隔开的。选择一个目录,把consul文件移动到该目录下。我选择的是/usr/local/bin

   1.5验证安装

  如下表示成功

  命令:consul

  

   1.6启动consul代理

  命令:./consul agent -dev -ui -node=consul-dev -client=192.168.128.149

  后面的ip是阿里云的私网ip

  1.7阿里云配置规则,打开8500端口

  1.8访问

2.搭建服务提供者

  2.1、新建一个maven项目(cloud-providerconsul-payment8006)

    结构如下:

    

  2.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>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-providerconsul-payment8006</artifactId>


    <dependencies>
    <dependency>
        <groupId>com.atguigu.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.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>


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

    </dependencies>

</project>

  2.3、编辑配置文件application.yml

server:
  port: 8006  #当前服务端口
spring:
  application:
    name: consul-provider-payment  #服务名称 注意这个名称不要太长,我前面名字是consul-provider-payment-con,导致访问consul页面的额时候这个服务有红叉
  cloud:
    consul:
      host: 59.120.138.4  #consul所在服务器ip
      port: 8500 #consul端口
      discovery:
        service-name: ${spring.application.name}
        heartbeat:
          enabled: true  #维持心跳

  2.4、编写主启动类

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Classname Payment8006
 * @Description TODO
 * @Date 2021/4/22 0022 下午 2:23
 * @Created by jcc
 */
@SpringBootApplication
@EnableDiscoveryClient
public class Payment8006 {
    public static void main(String[] args) {
        SpringApplication.run(Payment8006.class,args);
    }
}

 

  2.5、编写Controller

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

@RestController
@Slf4j
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/consul")
    public String paymentConsul(){
        return "springcloud with consul: "+serverPort+"	"+ UUID.randomUUID().toString();
    }
}

 

  2.6、启动项目,测试

    1)查看consul页面

  

    2)使用地址:http://localhost:8006/payment/consul

     

3搭建服务消费者

  1、新建一个maven项目(cloud-consumerconsul-order80)

    项目结构如下:

    

  2、引入pom依赖,同上(与服务提供者依赖相同)

  3、编辑application.yml文件

server:
  port: 80
spring:
  application:
    name: consul-consumer-order
  cloud:
    consul:
      host: 59.120.138.4
      port: 8500
      discovery:
        service-name: ${spring.application.name}
        heartbeat:
          enabled: true

 

  4、编写主启动类

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Classname ConsulOrder80
 * @Description TODO
 * @Date 2021/4/22 0022 下午 3:14
 * @Created by jcc
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulOrder80 {
    public static void main(String[] args) {
        SpringApplication.run(ConsulOrder80.class,args);
    }
}

 

  5、编辑配置类,注入RestTemplate对象

package com.atguigu.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @Classname ApplicationContextConfig
 * @Description TODO
 * @Date 2021/4/22 0022 下午 3:02
 * @Created by jcc
 */
@Configuration
public class ApplicationContextConfig {
    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

 

  6、编辑Controller

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.UUID;

@RestController
@Slf4j
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

     public static final String INVOME_URL = "http://consul-provider-payment";

     @Resource
     private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/consul")
    public String payment (){
    String result = restTemplate.getForObject(INVOME_URL+"/payment/consul",String.class);
    return result;
    }
}

 

  7、启动项目测试

    1)查看consul页面

    

    2)访问http://localhost/consumer/payment/consul

    

原文地址:https://www.cnblogs.com/jthr/p/14689395.html