本次内容为搭建一个服务消费者,用于消费上一篇博客注册在注册中心里的服务。
1)用 eclipse 新建一个 gradle 项目:EurekaBService. 该服务消费上一篇博客里注册的EurekaAService,同时也把自己注册入注册中心,供他人消费。
2)配置 build.gradle 文件,并 build 一次,确保所依赖的 jar 都已到位。
下面是 build.gradle 配置内容:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
repositories {
jcenter()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server:1.2.7.RELEASE'
compile 'org.springframework.cloud:spring-cloud-starter-eureka-ribbon:1.2.7.RELEASE'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.32'
compile 'org.springframework.boot:spring-boot-starter-test'
}
3) 实现 EurekaBService.
a) 项目目录如下:
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── simonton
│ │ └── eureka
│ │ ├── EurekaBApplication.java
│ │ ├── config
│ │ │ └── EurekaConfig.java
│ │ └── controller
│ │ └── EurekaBController.java
│ └── resources
│ └── application.yml
└── test
└── java
b) EurekaBApplication 代码如下:
package com.simonton.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaBApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaBApplication.class, args);
}
}
c) EurekaConfig 代码如下:
/**
*
*/
package com.simonton.eureka.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author simonton
*
*/
@Configuration
public class EurekaConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplage() {
return new RestTemplate();
}
}
@LoadBalanced 是 spring-ribbon工具的核心,负责客户端请求的 load balance。
d) EurekaBController 代码如下:
package com.simonton.eureka.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class EurekaBController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value="/serviceB",method=RequestMethod.GET)
public String service() {
String serviceA = restTemplate.getForEntity("http://a-service/serviceA", String.class).getBody();
return "service B " + serviceA;
}
@RequestMapping(value="/beforeService",method=RequestMethod.GET)
public String beforeService() {
String ret = null;
restTemplate.getForEntity("http://a-service/print", String.class);
ret = this.restTemplate.getForEntity("http://a-service/beforeService", String.class).getBody();
return ret;
}
}
PS:调用方式是http:// + 注册中心里的application name + controller里定义的具体的 requestmapping.
e) application.yml配置如下:
server:
port: 7777
spring:
application:
name: serviceB
eureka:
client:
service-url:
defaultZone: http://localhost:9999/eureka
最后,Run EurekaBApplication。然后就可以在注册中心查看到现在的服务注册情况: