When using consul for service discovery, the overall configuration of the project is as follows:
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 method
@SpringBootApplication @EnableDiscoveryClient public class ConsulServer1 { public static void main(String[] args) { SpringApplication application = new SpringApplication(ConsulServer1.class); application.run(args); } }
Call the service main method
@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(); } }
Points to note here:
1. Both the caller and the callee must be registered in consul
2. Consul calls the header to detect the heartbeat by default. Need to introduce spring-boot-actuator to be detected heartbeat;
3. When starting multiple services with the same IP address, the port cannot be set to 0 (spring default random port), otherwise only one service will be found;
4. When connecting to Consul, when calling the service, the resttemplate must be annotated with @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]#