在Spring Cloud中一般都使用Eureka做服务治理,本文先快速体验一下基于Eureka完成一次远程调用
Eureka Server搭建
类似于Spring Cloud Config Server
,Euraka Server
的搭建也是非常简单
添加依赖
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
Spring Boot Application
使用注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置application.properties
eureka.client.registerWithEureka = false
eureka.client.fetchRegistry = false
server.port = 8761
浏览器查看
服务提供方接入
添加依赖pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
Spring Boot Application
- 使用注解
@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
服务接口HomeController.java
@RestController
@RequestMapping
public class HomeController {
@GetMapping("ping")
public String ping() {
return "success";
}
}
配置文件application.properties
spring.application.name=tenmao-eureka-provider
# eureka配置
eureka.client.enabled=true
# 优先使用IP地址(防止hosts配置导致的不可访问问题)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=5
# 发呆时间,即服务续约到期时间 (缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=15
eureka.client.registry-fetch-interval-seconds=10
# 拉取服务注册信息间隔(缺省为30s)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
启动
启动后在Eureka Server上可以看到TENMAO-EUREKA-PROVIDER
服务使用方
添加依赖pom.xml
跟服务提供方的依赖和配置几乎一致
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
Spring Boot Application
- 使用注解
@EnableEurekaClient
- 使用
EurekaClient
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
@Resource
private EurekaClient eurekaClient;
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("tenmao-eureka-provider", false);
System.out.println(instanceInfo);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(instanceInfo.getHomePageUrl() + "ping", String.class);
System.out.println(result);
};
}
}
配置文件application.properties
spring.application.name=tenmao-eureka-client
server.port=8081
# eureka配置
eureka.client.enabled=true
# 优先使用IP地址(防止hosts配置导致的不可访问问题)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=5
# 发呆时间,即服务续约到期时间 (缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=15
eureka.client.registry-fetch-interval-seconds=10
# 拉取服务注册信息间隔(缺省为30s)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
启动
启动后就能从标准输出中看到远程调用结果了