Tutoriel et construction de l'intégration Springcloud Consul (version détaillée + cas)

À propos de Consul:

  1. Qu'est-ce que c'est
  2. Que pouvez-vous faire
  3. Où aller
  4. Comment jouer,
    Insérez la description de l'image ici
    installer et exécuter
    les instructions d'installation du site Web officiel: https://learn.hashicorp.com/consul/getting-started/install.html Une
    fois le téléchargement terminé, il n'y a qu'un seul fichier consul.exe, double-cliquez pour exécutez-le dans le chemin du disque dur et consultez les informations de version du
    Insérez la description de l'image ici
    Insérez la description de l'image iciInsérez la description de l'image ici
    consul et de l'intégration de springcloud:

1. Le fournisseur de services s'inscrit dans consul
1.1 pour créer un nouveau module de service de paiement cloud-providerconsul-payment8006
Insérez la description de l'image ici
1.2 pom.xml

    <?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>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <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>
    </dependencies>

</project>

1.3 application.yml

###consull服务端口号
server:
  port: 8006

spring:
  application:
    name: consul-provider-peyment
  ###consul注册中心地址
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        #hostname: 127.0.0.1
        service-name: ${
    
    spring.application.name}

1.4 Démarrage principal

package com.atguigu.springcloud;

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

/**
 * @author : 宋银义
 * @date : 2020-05-15 09:09
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
    
    

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

1.5 Entreprise

package com.atguigu.springcloud.controller;

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

import java.util.UUID;

/**
 * @author : 宋银义
 * @date : 2020-05-15 09:32
 **/
@RestController
@Slf4j
public class PaymentController {
    
    
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/consul")

    public String paymentConsul(){
    
    
        return "springcloud with consul:"+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

1.6 Démarrer le test 8006 De
Insérez la description de l'image ici
cette façon, le consul-provider-payemnt est enregistré, puis la fonction est testée, qui est également exécutée normalement.
Insérez la description de l'image ici
2. Les consommateurs de services s'inscrivent dans consul
2.1 Créer un nouveau module cloud-consumerconsul-order80
Insérez la description de l'image ici
2.2 pom.xml

<?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-consumerconsul-order80</artifactId>
    <dependencies>
        <!--springcloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!--springboot整合web组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
        </dependency>
    </dependencies>
</project>

2.3 application.yml

### consul服务端口号
server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
###consul服务注册中心
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        #hostname: 127.0.0.1
        service-name: ${
    
    spring.application.name}
      

2.4 Démarrage principal

package com.atguigu.springclud;

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

/**
 * @author : 宋银义
 * @date : 2020-05-15 09:47
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderConsulMain80.class,args);
    }

}

2.5 Configurer Bean

package com.atguigu.springclud.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;
//没讲ribbon 先使用restTemplate实现
/**
 * @author : 宋银义
 * @date : 2020-05-15 09:48
 **/
@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
    
    
        return new RestTemplate();
    }
}

2.6 Contrôleur

package com.atguigu.springclud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author : 宋银义
 * @date : 2020-05-15 09:50
 **/
@RestController
@Slf4j
public class OrderConsulController {
    
    
    public static final String INVOKE_URL = "http://consul-provider-payment";
    @Resource
    private RestTemplate restTemplate;

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

2.7 Démarrez le
Insérez la description de l'image ici
service de test 80 et l'enregistrement, puis testez la fonction, qui est également exécutée normalement. Après
Insérez la description de l'image ici
avoir terminé le travail, essentiellement un ensemble de procédures, merci

Je suppose que tu aimes

Origine blog.csdn.net/songyinyi/article/details/106135698
conseillé
Classement