spring.application.name=a-bootiful-client
你将配置一个Eureka service注册中心和一个client,client能自动注册到注册中心来解决端口问题。一个服务注册中心非常有用,它使得client-side负载均衡,并从 consumers分离服务的providers而不需要DNS。
15分钟
一个自己喜欢的编辑器或IDE
JDK1.8以上
Gradle 2.3+ 或者 Maven 3.0+
创建gradle工程,设置build.gradle
eureka-service/build.gradle
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'eureka-service'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
eureka-client/build.gradle
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'eureka-client'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
}
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
Spring Boot Gradle Plugin提供了许多便利的特性:
创建Maven工程
eureka-server/pom.xml
xml version="1.0" encoding="UTF-8"?>
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">
4.0.0
com.example
eureka-service
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Camden.SR5
pom
import
org.springframework.boot
spring-boot-maven-plugin
xml version="1.0" encoding="UTF-8"?>
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">
4.0.0
com.example
eureka-client
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka-server
test
org.springframework.cloud
spring-cloud-dependencies
Camden.SR5
pom
import
org.springframework.boot
spring-boot-maven-plugin
Spring Boot Maven plugin提高了许多方便的特性: 同上 gradle plugin
你首先需要一个Eureka Service注册中心,你可以使用Spring Cloud的@EnableEurekaServer来建立注册中心,以便其他应用访问。这是一个普通的Spring Boot应用,加上注解来提供服务注册功能。
service中创建一个application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
当注册中心启动时,会发生错误,带有堆栈信息:“没有注册中心可连接的副本节点”。在生产环境下,你想要注册中心不止一个实例,然而,出于简单的目的,它足以使相关日志失效。
默认情况下,注册中心也将试图将自己也注册上,所以你也需要禁止它注册。
一般在本地使用时,协定将注册中心放在一个单独的端口上。
添加一些属性来处理这些要求:
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
现在我们已经搭建好注册中心了,现在搭建一个客户端,让它在注册中心自己注册,并且使用Spring Cloud DiscoveryClient抽象去注册中心询问主机和端口。@EnableDiscoveryClient
激活Netflix Eureka DiscoveryClient实现。还有其他实现用于其他服务注册中心,如 Hashicorp’s Consul or Apache Zookeeper.
client中创建application.java
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
@RestController
class ServiceInstanceRestController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}
eureka-client/src/main/resources/bootstrap.properties
spring.application.name=a-bootiful-client
eureka-client定义一个Spring MVC REST端点,ServiceInstanceRestController,它将返回一个枚举,其中包含了注册中心在http://localhost:8080/service-instances/a-bootiful-client注册的所有ServiceInstance实例。查阅 Building a RESTful Web Service指导来学习更多关于用Spring MVC 和 Spring Boot建立REST服务。
ServiceInstance
映射在响应中。