编程知识 cdmana.com

Spring cloud learning (3) consult service registration and discovery

Consul Is an open source distributed service discovery and configuration management system , from HashiCorp Company use Go Language development .

1、 summary

Consul It provides service governance in microservice system 、 Configuration center 、 Control bus and other functions . Each of these functions can be used separately as needed , It can also be used together to build a comprehensive service network , All in all Consul Provides a complete service network solution . It has many advantages , Include : be based on raft agreement , Simple comparison ; Support health checks , Support at the same time HTTP and DNS The protocol supports cross data center WAN colony Provide graphic interface , Cross platform , Support linux,mac,windows.

2、 Install and run consul

https://www.consul.io/downloa..., When the download is complete , Decompression will appear consul.exe file , stay cmd Down entry consul Catalog

image-20201112154409160

visit http://localhost:8500 You can see consul The interface of

image-20201112154533281

3、 Usage method

newly build cloud-providerconsul-payment8006 modular

pom.xml

<dependencies>
        <dependency><!--  Quote your own definition of api General package , have access to Payment payment Entity -->
            <groupId>com.yu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- monitor -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!-- Thermal deployment -->
        <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>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml

server:
  port: 8006

spring:
  application:
    name: consul-provider-payment
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

Main start

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

controller

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

    @RequestMapping(value = "/payment/consul")
    public String paymentConsul(){
        return "springCloud--consul:"+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

image-20201101183833413

newly build cloud-consumerconsul-order80 modular

pom.xml

 <dependencies>
        <dependency><!--  Quote your own definition of api General package , have access to Payment payment Entity -->
            <groupId>com.yu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- monitor -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!-- Thermal deployment -->
        <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>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml

server:
  port: 8090
spring:
  application:
    name: cloud-consumer-order
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

Main start

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

To configure Bean

@Configuration
public class ApplicationContextConfig {

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

controller

@RestController
@Slf4j
public class ConsumerController {
    public static final String INVOKE_URL = "http://consul-provider-payment";

    @Resource
    private RestTemplate restTemplate;

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

test

image-20201101184044782

image-20201101184101760

版权声明
本文为[thirtyyy]所创,转载请带上原文链接,感谢

Scroll to Top