Consul快速入門(三):Spring Cloud Consul實現服務註冊與發現

1、前言

本文通過創建provider-service、consumer-service兩個微服務,並通過feign接口調用來演示Spring Cloud整合Consul。閱讀本文需要前置知識:

  • Spring Boot
  • Spring Cloud
  • Spring Cloud Feign

2、搭建provider-service服務

2.1、創建maven模塊

創建provider項目,添加provider-service、provider-service-remote-api兩個maven模塊:

provider-module.png

2.2、添加spring boot、spring cloud依賴

在父模塊provider中添加spring boot、spring cloud依賴
,spring cloud使用截止當前最新版本Hoxton.SR1,關聯spring boot版本2.2.2.RELEASE

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注意:spring boot和spring cloud之間有版本依賴關係,如果想用其他版本,請自行查詢對應的依賴版本,否則可能由於不兼容服務起不來

2.3、provider-service-remote-api模塊編寫feign接口

maven模塊provider-service-remote-api引入spring cloud feign

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

代碼編寫,新建IProviderRemoteService接口

@FeignClient(name = "provider-service")
public interface IProviderRemoteService {

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    String hello();
}

2.4、provider-service模塊編寫接口實現

maven依賴

<dependency>
    <groupId>com.example</groupId>
    <artifactId>provider-service-remote-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

接口實現類ProviderRemoteService

@RestController
public class ProviderRemoteService implements IProviderRemoteService {

    @Override
    public String hello() {
        return "hello world!";
    }
}

2.5、集成spring cloud consul

引入spring cloud consul模塊

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

application.yml添加consul配置

server:
  port: 8080
spring:
  application:
    name: provider-service
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        heartbeat:
          enabled: true

啓動類添加@EnableDiscoveryClient註解,啓用服務發現功能

@SpringBootApplication
@EnableDiscoveryClient
public class Application {

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

}

到此,provider-service服務已經全部搭建完畢,完整項目結構:

provider-service.png

3、搭建consumer-service服務

3.1、創建maven模塊

創建consumer項目,添加consumer-service,因爲該服務不需要對外提供接口,所以無需consumer-service-remote-api模塊:
consumer-module.png

3.2、consumer-service編寫接口調用provider-service服務

引用provider-service-remote-api

<dependency>
    <groupId>com.example</groupId>
    <artifactId>provider-service-remote-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

編寫測試接口,通過feign調用provider-service服務

@RestController
public class ConsumerController {

    @Autowired
    private IProviderRemoteService providerRemoteService;

    @RequestMapping(value = "/test")
    public String test() {
        return providerRemoteService.hello();
    }
}

3.3、集成spring cloud consul

同provider-service服務一樣,引入spring cloud consul模塊

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

application.yml添加consul配置

server:
  port: 9090
spring:
  application:
    name: consumer-service
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        heartbeat:
          enabled: true

啓動類添加@EnableDiscoveryClient註解,啓用服務發現功能,並添加@EnableFeignClients註解,啓用feign功能

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {

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

}

到此,consumer-service服務也搭建完成,完整項目結構:

consumer-service.png

4、測試

4.1、啓動consul服務

爲了便於測試,筆者在本機以開發者模式起了一個consul服務

D:\consul>consul agent -dev
==> Starting Consul agent...
           Version: 'v1.7.0'
           Node ID: 'c43eedeb-f211-dbf2-e3fa-3caa64f86ddb'
         Node name: 'DESKTOP-77V3R9P'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false

==> Log data will now stream in as it occurs:

    2020-02-22T15:34:07.878+0800 [DEBUG] agent: Using random ID as node ID: id=c43eedeb-f211-dbf2-e3fa-3caa64f86ddb
    2020-02-22T15:34:07.900+0800 [DEBUG] agent.tlsutil: Update: version=1
    2020-02-22T15:34:07.914+0800 [DEBUG] agent.tlsutil: OutgoingRPCWrapper: version=1
    2020-02-22T15:34:07.916+0800 [INFO]  agent.server.raft: initial configuration: index=1 servers="[{Suffrage:Voter ID:c43eedeb-f211-dbf2-e3fa-3caa64f86ddb Address:127.0.0.1:8300}]"
    2020-02-22T15:34:07.916+0800 [INFO]  agent.server.raft: entering follower state: follower="Node at 127.0.0.1:8300 [Follower]" leader=
    2020-02-22T15:34:07.919+0800 [INFO]  agent.server.serf.wan: serf: EventMemberJoin: DESKTOP-77V3R9P.dc1 127.0.0.1
    2020-02-22T15:34:07.920+0800 [INFO]  agent.server.serf.lan: serf: EventMemberJoin: DESKTOP-77V3R9P 127.0.0.1
    2020-02-22T15:34:07.920+0800 [INFO]  agent.server: Adding LAN server: server="DESKTOP-77V3R9P (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
    2020-02-22T15:34:07.921+0800 [INFO]  agent.server: Handled event for server in area: event=member-join server=DESKTOP-77V3R9P.dc1 area=wan
    2020-02-22T15:34:07.921+0800 [INFO]  agent: Started DNS server: address=127.0.0.1:8600 network=udp
    2020-02-22T15:34:07.923+0800 [INFO]  agent: Started DNS server: address=127.0.0.1:8600 network=tcp
    2020-02-22T15:34:07.926+0800 [INFO]  agent: Started HTTP server: address=127.0.0.1:8500 network=tcp
    2020-02-22T15:34:07.928+0800 [INFO]  agent: started state syncer
==> Consul agent running!
...

4.2、啓動服務註冊到consul

分別啓動provider-service服務和consumer-service服務,訪問consul的Web UI界面可以看到兩個服務都註冊上來了:

consul-services.png

consul-nodes.png

4.3、調用測試接口

調用consumer-service測試接口http://127.0.0.1:9090/test

invoke.png

現在嘗試把provider服務停止,consul註冊中心可以看到健康檢查狀態爲Critical,服務已經下線

`critical.png

此時,再次調用consumer-service測試接口,會報找不到服務

service not found.ong

5、Spring Cloud Consul應用配置

Name Default Description
spring.cloud.consul.config.acl-token
spring.cloud.consul.config.data-key data If format is Format.PROPERTIES or Format.YAML then the following field is used as key to look up consul for configuration.
spring.cloud.consul.config.default-context application
spring.cloud.consul.config.enabled TRUE
spring.cloud.consul.config.fail-fast TRUE Throw exceptions during config lookup if true, otherwise, log warnings.
spring.cloud.consul.config.format
spring.cloud.consul.config.name Alternative to spring.application.name to use in looking up values in consul KV.
spring.cloud.consul.config.prefix config
spring.cloud.consul.config.profile-separator ,
spring.cloud.consul.config.watch.delay 1000 The value of the fixed delay for the watch in millis. Defaults to 1000.
spring.cloud.consul.config.watch.enabled TRUE If the watch is enabled. Defaults to true.
spring.cloud.consul.config.watch.wait-time 55 The number of seconds to wait (or block) for watch query, defaults to 55. Needs to be less than default ConsulClient (defaults to 60). To increase ConsulClient timeout create a ConsulClient bean with a custom ConsulRawClient with a custom HttpClient.
spring.cloud.consul.discovery.acl-token
spring.cloud.consul.discovery.catalog-services-watch-delay 1000 The delay between calls to watch consul catalog in millis, default is 1000.
spring.cloud.consul.discovery.catalog-services-watch-timeout 2 The number of seconds to block while watching consul catalog, default is 2.
spring.cloud.consul.discovery.consistency-mode Consistency mode for health service request.
spring.cloud.consul.discovery.datacenters Map of serviceId’s → datacenter to query for in server list. This allows looking up services in another datacenters.
spring.cloud.consul.discovery.default-query-tag Tag to query for in service list if one is not listed in serverListQueryTags.
spring.cloud.consul.discovery.default-zone-metadata-name zone Service instance zone comes from metadata. This allows changing the metadata tag name.
spring.cloud.consul.discovery.deregister TRUE Disable automatic de-registration of service in consul.
spring.cloud.consul.discovery.enabled TRUE Is service discovery enabled?
spring.cloud.consul.discovery.fail-fast TRUE Throw exceptions during service registration if true, otherwise, log warnings (defaults to true).
spring.cloud.consul.discovery.health-check-critical-timeout Timeout to deregister services critical for longer than timeout (e.g. 30m). Requires consul version 7.x or higher.
spring.cloud.consul.discovery.health-check-headers Headers to be applied to the Health Check calls.
spring.cloud.consul.discovery.health-check-interval 10s How often to perform the health check (e.g. 10s), defaults to 10s.
spring.cloud.consul.discovery.health-check-path /actuator/health Alternate server path to invoke for health checking.
spring.cloud.consul.discovery.health-check-timeout Timeout for health check (e.g. 10s).
spring.cloud.consul.discovery.health-check-tls-skip-verify Skips certificate verification during service checks if true, otherwise runs certificate verification.
spring.cloud.consul.discovery.health-check-url Custom health check url to override default.
spring.cloud.consul.discovery.heartbeat.enabled FALSE
spring.cloud.consul.discovery.heartbeat.interval-ratio
spring.cloud.consul.discovery.heartbeat.ttl 30s
spring.cloud.consul.discovery.hostname Hostname to use when accessing server.
spring.cloud.consul.discovery.include-hostname-in-instance-id FALSE Whether hostname is included into the default instance id when registering service.
spring.cloud.consul.discovery.instance-group Service instance group.
spring.cloud.consul.discovery.instance-id Unique service instance id.
spring.cloud.consul.discovery.instance-zone Service instance zone.
spring.cloud.consul.discovery.ip-address IP address to use when accessing service (must also set preferIpAddress to use).
spring.cloud.consul.discovery.lifecycle.enabled TRUE
spring.cloud.consul.discovery.management-port Port to register the management service under (defaults to management port).
spring.cloud.consul.discovery.management-suffix management Suffix to use when registering management service.
spring.cloud.consul.discovery.management-tags Tags to use when registering management service.
spring.cloud.consul.discovery.order 0 Order of the discovery client used by?CompositeDiscoveryClient?for sorting available clients.
spring.cloud.consul.discovery.port Port to register the service under (defaults to listening port).
spring.cloud.consul.discovery.prefer-agent-address FALSE Source of how we will determine the address to use.
spring.cloud.consul.discovery.prefer-ip-address FALSE Use ip address rather than hostname during registration.
spring.cloud.consul.discovery.query-passing FALSE Add the 'passing` parameter to /v1/health/service/serviceName. This pushes health check passing to the server.
spring.cloud.consul.discovery.register TRUE Register as a service in consul.
spring.cloud.consul.discovery.register-health-check TRUE Register health check in consul. Useful during development of a service.
spring.cloud.consul.discovery.scheme http Whether to register an http or https service.
spring.cloud.consul.discovery.server-list-query-tags Map of serviceId’s → tag to query for in server list. This allows filtering services by a single tag.
spring.cloud.consul.discovery.service-name Service name.
spring.cloud.consul.discovery.tags Tags to use when registering service.
spring.cloud.consul.enabled TRUE Is spring cloud consul enabled.
spring.cloud.consul.host localhost Consul agent hostname. Defaults to ‘localhost’.
spring.cloud.consul.port 8500 Consul agent port. Defaults to ‘8500’.
spring.cloud.consul.retry.initial-interval 1000 Initial retry interval in milliseconds.
spring.cloud.consul.retry.max-attempts 6 Maximum number of attempts.
spring.cloud.consul.retry.max-interval 2000 Maximum interval for backoff.
spring.cloud.consul.retry.multiplier 1.1 Multiplier for next interval.
spring.cloud.consul.scheme Consul agent scheme (HTTP/HTTPS). If there is no scheme in address - client will use HTTP.
spring.cloud.consul.tls.certificate-password Password to open the certificate.
spring.cloud.consul.tls.certificate-path File path to the certificate.
spring.cloud.consul.tls.key-store-instance-type Type of key framework to use.
spring.cloud.consul.tls.key-store-password Password to an external keystore.
spring.cloud.consul.tls.key-store-path Path to an external keystore.

來源官網:https://cloud.spring.io/spring-cloud-consul/reference/html/appendix.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章