[Spring-cloud-eureka]使用 gradle, spring boot,Spring cloud Eureka 搭建服务消费者

本次内容为搭建一个服务消费者,用于消费上一篇博客注册在注册中心里的服务。

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。然后就可以在注册中心查看到现在的服务注册情况:

 

 

 

 

 

转载于:https://my.oschina.net/simonton/blog/1532467

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值