|
最新稳定版请使用Spring Cloud Consul 5.0.0! |
快速入门
这份快速入门教程介绍如何使用 Spring Cloud Consul 进行服务发现和分布式配置。
首先,在你的电脑上运行Consul Agent。然后你可以访问它,并用 Spring Cloud Consul 作为服务注册表和配置源使用。
发现客户端使用
要在应用中使用这些功能,你可以将其构建为依赖于春云执政核心.
添加依赖最方便的方法是使用 Spring Boot Starters:org.springframework.cloud:spring-cloud-starter-consul-discovery.
我们建议使用依赖管理和Spring靴启动父.
以下示例展示了典型的Maven配置:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-version}</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
以下示例展示了典型的Gradle设置:
plugins {
id 'org.springframework.boot' version ${spring-boot-version}
id 'io.spring.dependency-management' version ${spring-dependency-management-version}
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
现在你可以创建标准的 Spring Boot 应用程序,比如以下 HTTP 服务器:
@SpringBootApplication
@RestController
public class Application {
@GetMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
当该 HTTP 服务器运行时,它连接到运行在默认本地 8500 端口的 Consul 代理。
要修改启动行为,你可以通过以下方式更改 Consul 代理的位置。application.properties如下例所示:
spring:
cloud:
consul:
host: localhost
port: 8500
你现在可以使用发现客户端,@LoadBalanced 休息模板或@LoadBalanced WebClient.Builder从Consul获取服务和实例数据,如下示例所示:
@Autowired
private DiscoveryClient discoveryClient;
public String serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
if (list != null && list.size() > 0 ) {
return list.get(0).getUri().toString();
}
return null;
}
分布式配置使用
要在应用中使用这些功能,你可以将其构建为依赖于春云执政核心和Spring-cloud-consul-config.
添加依赖最方便的方法是使用 Spring Boot Starters:org.springframework.cloud:spring-cloud-starter-consul-config.
我们建议使用依赖管理和Spring靴启动父.
以下示例展示了典型的Maven配置:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-version}</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
以下示例展示了典型的Gradle设置:
plugins {
id 'org.springframework.boot' version ${spring-boot-version}
id 'io.spring.dependency-management' version ${spring-dependency-management-version}
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-consul-config'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
现在你可以创建标准的 Spring Boot 应用程序,比如以下 HTTP 服务器:
@SpringBootApplication
@RestController
public class Application {
@GetMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
应用程序从Consul获取配置数据。
如果你用的是 Spring Cloud Consul 配置,你需要设置spring.config.import财产以约束执政官。
你可以在 Spring Boot 配置数据导入部分了解更多。 |