Springcloud統合Consulチュートリアルと構築(詳細バージョン+ケース)

領事について:

  1. それは何ですか
  2. あなたは何ができますか
  3. どこへ行く
  4. 公式ウェブサイトのインストール手順を再生、
    ここに画像の説明を挿入します
    インストール、実行する
    方法:https:
    //learn.hashicorp.com/consul/getting-started/install.htmlダウンロードが完了したら、consul.exeファイルは1つだけです。ダブルクリックして、ハードディスクパスで実行し、バージョン情報の領事とスプリングクラウドの統合を確認してください
    ここに画像の説明を挿入します
    ここに画像の説明を挿入しますここに画像の説明を挿入します

領事に1.サービスプロバイダの登録
新しい決済サービスモジュールクラウドproviderconsul-payment8006作成するために、1.1
ここに画像の説明を挿入します
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メインブート

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ビジネス

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 8006テストを開始する
ここに画像の説明を挿入します
このようにして、consul-provider-payemntが登録され、関数がテストされます。これも正常に実行されます。
ここに画像の説明を挿入します
2.サービスの消費者は、領事に登録
新しいMODULクラウドconsumerconsul-order80作成2.1
ここに画像の説明を挿入します
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メインスタート

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.5Beanの構成

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コントローラー

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 80テスト
ここに画像の説明を挿入します
サービスと登録を開始し、正常に実行される機能をテストします。
ここに画像の説明を挿入します
作業終了、基本的に一連の手順を実行していただき、ありがとうございます。

おすすめ

転載: blog.csdn.net/songyinyi/article/details/106135698