consul

在使用的consul进行服务发现,项目整体的配置如下:

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">
    <groupId>com.curve</groupId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consul-server1</artifactId>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR2</spring-cloud.version>
    </properties>

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

    <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>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-consul-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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


</project>

application.yml

server:
  port: 0
spring:
  cloud:
    consul:
      port: 8500
      host: 127.0.0.1
      discovery:
        healthCheckInterval: 2s
        prefer-ip-address: true
  application:
    name: server

main方法

扫描二维码关注公众号,回复: 236862 查看本文章
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulServer1 {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ConsulServer1.class);
        application.run(args);
    }
}

调用服务main方法

@SpringBootApplication
@EnableDiscoveryClient
public class WebApiApp {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(WebApiApp.class);
        application.run(args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate build() {
        return new RestTemplateBuilder().build();
    }
}

这里需要注意的地方:

1、调用方和被调用方都必须在consul中进行注册

2、Consul默认调用headler进行检测心跳。需要引入 spring-boot-actuator 进行被检测心跳;

3、同一个IP地址启动多份服务时,端口不可以设置成0(spring默认随机端口),不然会只能发现一个服务;

4、连接Consul时,进行调用服务的时候,resttemplate必须要进行 @LoadBalanced 注解

service

[Unit]
Description=Consul Server
After=network.target

[Service]
User=consul
Group=consul
Environment="GOMAXPROCS=2"
ExecStart=/usr/local/consul/bin/consul agent -config-dir=/etc/consul.d
ExecReload=/bin/kill -9 $MAINPID
KillSignal=SIGINT
Restart=on-failure
RestartSec=1

[Install]
WantedBy=default.target

 

JSON:

[root@dw-alpha-1-core-nj consul.d]# ll
total 8
-rw-r--r--. 1 consul consul  48 Jun 11 15:13 basic_config.json
-rw-r--r--. 1 consul consul 164 Jul 11 17:48 extra_config.json
[root@dw-alpha-1-core-nj consul.d]# cat basic_config.json 
{
  "data_dir": "/local/consul",
  "ui": true
}
[root@dw-alpha-1-core-nj consul.d]# cat extra_config.json 
{
  "datacenter": "dw-nj",
  "node_name": "dw-alpha-1-core-nj",
  "server": true,
  "bootstrap": true,
  "client_addr": "0.0.0.0",
  "bind_addr": "10.41.214.199"
}
[root@dw-alpha-1-core-nj consul.d]# 

 

猜你喜欢

转载自xiangdong-li.iteye.com/blog/2389271