spring cloud consul入门,服务注册和发现,配置中心使用入门示例

我们知道,在微服务中比较关键的几个点在于:

  • 服务注册和发现
  • 服务的负载均衡
  • 容错
  • 网关
  • 统一配置管理
  • 链路追踪和日志

在服务注册和发现这块,spring提供了eureka和consul两种方式来实现服务的注册和发现,本例讲解consul作为服务注册和服务配置中心的使用方式。consul版本为consul_1.7.3,可以在 https://www.consul.io/downloads进行下载。
下载解压完之后,如在windows下,在consul.exe所在目录,同时 shift+鼠标右键在命令行输入如下指令:consul.exe agent -dev,这样启动consul服务:
在这里插入图片描述

接下来编写服务提供这,consul-provider:
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>spring-cloud-test-001</artifactId>
        <groupId>com.leo.test</groupId>
        <version>1.0.0-snapshot</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>consule-provider-001</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--消息总线,提供配置实时刷新,不再依赖中间件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-bus</artifactId>
        </dependency>
        <!--consul的配置中心功能-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <!--服务注册和发现功能-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

建立配置文件:
bootstrap.xml:

spring:
  application:
    name: consul-provider-001
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: consul-provider-001
      config:
        enabled: true
        format: YAML
        prefix: config
        profile-separator: ','
        data-key: data
        defaultContext: consul-provider-001
        watch:
          enabled: true
          delay: 1000
        instance-id: ${spring.application.name}
server:
  port: 9040

··application.ym配置如下:

spring:
  profiles:
    active: dev

management:
  endpoints:
    web:
      exposure:
        include: '*'

建立启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsulProviderApplication.class);
    }
}

建立controller:
查看配置controller


import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //刷新配置
@RequestMapping("/consul/config")
public class ConfigController {
    @Value("${test.hello}")
    private String consulTest;

    @RequestMapping("/test")
    public String testConfig(){
        return String.format("test.hello is %s ",
                consulTest);
    }
}

服务对外提供controller:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/provider/consul")
public class ConsulProviderController {
    @Value("${server.port}")
    public String port;
    @RequestMapping("/sayHello")
    public String sayHello(){
        return "hello from provider of port "+port;
    }
}

访问 http://localhost:8500/ui/dc1/kv 建立配置
在这里插入图片描述
启动consul-provider,访问consul: http://localhost:8500/ui/dc1/services
发现consul-provider已经注册,
在这里插入图片描述

访问consul-provider, http://localhost:9040/consul/config/test:
在这里插入图片描述
已经获取到consul里的配置,

更改consul中的配置:
在这里插入图片描述

重新访问 consul-provider:http://localhost:9040/consul/config/test
在这里插入图片描述
配置已经刷新。

下面建立consul-consumer进行服务消费,
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>spring-cloud-test-001</artifactId>
        <groupId>com.leo.test</groupId>
        <version>1.0.0-snapshot</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consul-consumer-001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

建立模块配置文件
application.yml

server:
  port: 9050
spring:
  application:
    name: consul-consumer-001
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        # service-name: consul-consumer-001
        register: false

建立启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ConsulConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsulConsumerApplication.class);
    }
}

建立feign访问客户端:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "consul-provider-001",path ="/provider/consul" )
public interface ConsulFeignClient {
    @RequestMapping("/sayHello")
    String sayHello();
}

建立controller访问:

import com.leo.test.spring.cloud.test.consul.consumer.client.ConsulFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer/consul")
public class ConsulConsumerController {
    @Autowired
    private ConsulFeignClient feignClient;

    @RequestMapping("/sayHello")
    public String sayHello(){

        return feignClient.sayHello();
    }

}

启动consul-consumer:,访问 http://localhost:9050/consumer/consul/sayHello
在这里插入图片描述

这样consul的服务注册发现和配置中心搭建完成

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读