SpringCloud学习之旅07--weather项目集成feign(微服务消费者)

目录结构:


build.gradle文件:

// buildscript 代码块中脚本优先执行
buildscript {


// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M3'
}


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}


// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'


// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


ext {
springCloudVersion = 'Finchley.M2'
}


// 依赖关系
dependencies {


// Eureka Client
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')


// Feign
//compile('org.springframework.cloud:spring-cloud-starter-openfeign')

// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '1.4.4.RELEASE'


// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-feign', version: '1.0.3.RELEASE'



// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}


dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}

}



application.properties文件:

spring.application.name: micro-weather-eureka-client-feign

//这里的地址是 SpringCloud学习之旅03--eureka服务端构建 的地址

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/


feign.client.config.feignName.connectTimeout: 5000
feign.client.config.feignName.readTimeout: 5000


server.port=9091




package com.waylau.spring.cloud.weather.service;


import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;


/**
 * City Client.
 * 
 * @since 1.0.0 2017年11月27日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
//指明服务的地址
@FeignClient("msa-weather-city-eureka")
public interface CityClient {


@GetMapping("/cities")
String listCity();

}



package com.waylau.spring.cloud.weather.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


import com.waylau.spring.cloud.weather.service.CityClient;
/**
 * City Controller.
 * 
 * @since 1.0.0 2017年11月27日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@RestController
public class CityController {
@Autowired
private CityClient cityClient;

@GetMapping("/cities")
public String listCity() {
// 通过Feign客户端来查找
String body = cityClient.listCity();
return body;
}

}




package com.waylau.spring.cloud.weather;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {


public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}



这里需要启动服务端,下篇博文来构建。

启动08和03 和本项目:

访问链接:

http://localhost:9091/cities


这样就完成了通过feign和eureka来“组合”两个项目了。


猜你喜欢

转载自blog.csdn.net/qq_33371766/article/details/80655552
今日推荐