先看一看要作事情,須要在Consul上面實現註冊中心的功能,並以2個Spring boot項目分別做爲生產者,消費者。html
假設已經完成文章《Consul的開發者模式之Docker版》中的全部的配置,並啓動了Consul,訪問:http://localhost:8500/ui/dc1/services 即以下圖: java
這裏就是一個簡單的spring boot工程。git
plugins { id 'org.asciidoctor.convert' version '1.5.3' id 'org.springframework.boot' version '2.1.6.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.zyl' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' configurations { developmentOnly runtimeClasspath { extendsFrom developmentOnly } compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } ext { set('snippetsDir', file("build/generated-snippets")) set('springCloudVersion', "Greenwich.SR1") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery' implementation "org.springframework.boot:spring-boot-starter-actuator" compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } test { outputs.dir snippetsDir } asciidoctor { inputs.dir snippetsDir dependsOn test }
spring.application.name=consul-producer
server.port=0 spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.prefer-ip-address=true spring.cloud.consul.discovery.health-check-critical-timeout=90m management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always spring.jackson.serialization.indent_output=true
這個文件什麼都沒有,主要是解決一個警告。github
@EnableDiscoveryClient
表示啓用服務發現客戶端。web
消費者包含以上配置,只是在調用生產者接口都部分有所不一樣。spring
package com.zyl.consulconsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; import java.time.Duration; @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @LoadBalanced @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.setConnectTimeout(Duration.ofMillis(100)) .setReadTimeout(Duration.ofMillis(500)) .build(); } }
這裏主要就是啓用服務發現客戶端,和啓用resttemplate的負載均衡。bootstrap
package com.zyl.consulconsumer.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class DemoController { private final RestTemplate restTemplate; public DemoController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @RequestMapping("/") public String home() { return restTemplate.getForObject("http://consul-producer/", String.class); } }
注意這裏調用的時候只要使用consul註冊中心發現的服務名,進行調用便可。springboot
這是consul註冊中心的頁面效果,生產者和消費者都已經註冊到consul了。bash
https://github.com/fxtxz2/consul-springboot架構