服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - 详解使用Spring Cloud Consul实现服务的注册和发现

详解使用Spring Cloud Consul实现服务的注册和发现

2021-05-08 11:11西夏一品堂 Java教程

这篇文章主要介绍了详解使用Spring Cloud Consul实现服务的注册和发现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

首先安装consul环境,参照之前的文章:http://www.zzvips.com/article/142903.html

项目规划,2个服务端,1个客户端

首先来看服务端,

一:服务端1:

项目依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependencymanagement>
  <dependencies>
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-consul-dependencies</artifactid>
      <version>1.0.1.release</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencymanagement>
 
<dependencies>
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-actuator</artifactid>
    <version>1.3.5.release</version>
  </dependency>
  <dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-consul-discovery</artifactid>
  </dependency>
</dependencies>

注意,增加spring-boot-actuator是为了项目可以访问/health 路径来判断服务是否健康

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.pp.consul1;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@springbootapplication
@enablediscoveryclient
@restcontroller
public class consulapp {
   
  @requestmapping("/home")
  public object home() {
    system.out.println("1111111111111");
    return "ok11";
  }
   
  public static void main( string[] args ) {
    springapplication.run(consulapp.class, args);
  }
}

application.properties 配置内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
server.port=9955
spring.application.name=consul-server-1
spring.cloud.consul.host=192.168.1.100
spring.cloud.consul.port=8500
spring.cloud.consul.enabled=true
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.instanceid=tomcat1
spring.cloud.consul.discovery.servicename=tomcat
spring.cloud.consul.discovery.hostname=192.168.2.95
spring.cloud.consul.discovery.port=${server.port}
spring.cloud.consul.discovery.healthcheckurl=http://192.168.2.95:9955/health
spring.cloud.consul.discovery.healthcheckinterval=10s
spring.cloud.consul.discovery.tags=dev

看过我之前文章的,应该对这些配置很清楚了。这样,一个服务端就配置写好了。

由于我们增加了@enablediscoveryclient注解,所以,系统启动的时候,就会向consul注册一个服务,服务的名字为tomcat, id为tomcat1

访问consul的http api /v1/catalog/service/tomcat 输出如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
{
  "node":"192.168.1.100",
  "address":"192.168.1.100",
  "serviceid":"tomcat1",
  "servicename":"tomcat",
  "servicetags":["dev"],
  "serviceaddress":"192.168.2.95",
  "serviceport":9955,
  "serviceenabletagoverride":false,
  "createindex":993,
  "modifyindex":1057
}

二:服务端2

项目依赖和上面一样 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.pp.consul2; 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@springbootapplication
@enablediscoveryclient
@restcontroller
public class consulapp {
   
  @requestmapping("/home")
  public object home() {
    system.out.println("2222222222222222");
    return "ok22";
  }
   
  public static void main( string[] args ) {
    springapplication.run(consulapp.class, args);
  }
}

application.properties 配置内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server.port=9966
 
spring.application.name=consul-server-2
spring.cloud.consul.host=192.168.1.100
spring.cloud.consul.port=8500
spring.cloud.consul.enabled=true
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.instanceid=tomcat2
spring.cloud.consul.discovery.servicename=tomcat
spring.cloud.consul.discovery.hostname=192.168.2.95
spring.cloud.consul.discovery.port=${server.port}
spring.cloud.consul.discovery.healthcheckurl=http://192.168.2.95:9966/health
spring.cloud.consul.discovery.healthcheckinterval=10s
spring.cloud.consul.discovery.tags=test

三:客户端

项目依赖,只需要spring-cloud-starter-consul-discovery

application.properties 配置内容:

?
1
2
3
4
5
6
server.port=9977
 
spring.application.name=consul-client
spring.cloud.consul.host=192.168.1.100
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.register=false

注意,这里的spring.cloud.consul.discovery.register需要配置成false,否则系统启动的时候,会向consul注册一个服务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.pp.client;
 
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.discoveryclient;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import org.springframework.cloud.client.loadbalancer.loadbalancerclient;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@springbootapplication
@enablediscoveryclient
@restcontroller
public class consulclient {
   
  @autowired
  private loadbalancerclient loadbalancer;
   
  @autowired
  private discoveryclient discoveryclient;
   
  /**
   * 从所有服务中选择一个服务(轮询)
   */
  @requestmapping("/discover")
  public object discover() {
    return loadbalancer.choose("tomcat").geturi().tostring();
  }
   
  /**
   * 获取所有服务
   */
  @requestmapping("/services")
  public object services() {
    return discoveryclient.getinstances("tomcat");
  }
   
  public static void main( string[] args ) {
    springapplication.run(consulclient.class, args);
  }
}

启动之后,就可以访问/discover,/services 查看效果了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/mn960mn/article/details/51775212

延伸 · 阅读

精彩推荐