Spring cloud 之 Feign Client

本文接选自 《Netkiller Java 手札》

12.2.2. Eureka Client

12.2.2.1. Maven

			<project 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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.netkiller.spring.cloud</groupId>
	<artifactId>eureka.client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka.client</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath />
	</parent>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>			

12.2.2.2. Application

			package cn.netkiller.spring.cloud.eureka.client;

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

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

12.2.2.3. RestController

			package cn.netkiller.spring.cloud.eureka.client;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestRestController {
	private static final Logger logger = LoggerFactory.getLogger(TestRestController.class);

	@RequestMapping("/")
	public String home() {
		logger.info("Hello!!!");
		return "Hello World";
	}

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/service-instances/{applicationName}")
	public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
		return this.discoveryClient.getInstances(applicationName);
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
		@SuppressWarnings("deprecation")
		ServiceInstance instance = discoveryClient.getLocalServiceInstance();
		Integer r = a + b;
		logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
		return r;
	}
	
	@RequestMapping("/greeting")
	public String greeting() {
		return "GREETING";
	}
}			

12.2.2.4. application.properties

			spring.application.name=test-service
server.port=8080
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/			

12.2.2.5. 测试

首先确认客户端已经注册到 http://localhost:8761/

			$ curl http://localhost:8080/service-instances/test-service

[
	{
		"host": "Neo-Desktop",
		"port": 8080,
		"secure": false,
		"uri": "http://Neo-Desktop:8080",
		"serviceId": "TEST-SERVICE",
		"metadata": {},
		"instanceInfo": {
			"instanceId": "Neo-Desktop:test-service:8080",
			"app": "TEST-SERVICE",
			"appGroupName": null,
			"ipAddr": "172.25.10.150",
			"sid": "na",
			"homePageUrl": "http://Neo-Desktop:8080/",
			"statusPageUrl": "http://Neo-Desktop:8080/info",
			"healthCheckUrl": "http://Neo-Desktop:8080/health",
			"secureHealthCheckUrl": null,
			"vipAddress": "test-service",
			"secureVipAddress": "test-service",
			"countryId": 1,
			"dataCenterInfo": {
			"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
			"name": "MyOwn"
		},
		"hostName": "Neo-Desktop",
		"status": "UP",
		"leaseInfo": {
			"renewalIntervalInSecs": 30,
			"durationInSecs": 90,
			"registrationTimestamp": 1497922681680,
			"lastRenewalTimestamp": 1497922681680,
			"evictionTimestamp": 0,
			"serviceUpTimestamp": 1497922003783
		},
		"isCoordinatingDiscoveryServer": false,
		"metadata": {},
		"lastUpdatedTimestamp": 1497922681680,
		"lastDirtyTimestamp": 1497922681025,
		"actionType": "ADDED",
		"asgName": null,
		"overriddenStatus": "UNKNOWN"
		}
	}
]			

add 接口测试

			curl http://localhost:8080/add.json?a=5&b=3

8			

12.2.3. Feign client

12.2.3.1. Maven

			<project 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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.netkiller.spring.cloud.netflix</groupId>
	<artifactId>feign.client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>feign.client</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath />
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
			<version>1.3.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>1.3.1.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>			

12.2.3.2. Application

			package cn.netkiller.spring.cloud.netflix.feign.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
public class Application {
	@Autowired
	private GreetingClient greetingClient;

	@RequestMapping("/get-greeting")
	public String greeting() {
		return greetingClient.greeting();
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		System.out.println("Hello World!");
	}
}			

12.2.3.3. interface

			package cn.netkiller.spring.cloud.netflix.feign.client;

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

@FeignClient("test-service")
public interface GreetingClient {
	@RequestMapping("/greeting")
	String greeting();
}			

@FeignClient("test-service") 是 Eureka Client application.properties 中的 spring.application.name 配置项

@RequestMapping("/greeting") 是 Eureka Client RestController 中的 @RequestMapping

12.2.3.4. application.properties

			spring.application.name=spring-cloud-eureka-feign-client
server.port=8088
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
feign.compression.response.enabled=true
feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048			

12.2.3.5. 测试

			$ curl -s http://localhost:8088/get-greeting.json

GREETING

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2017-06-21

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏PPV课数据科学社区

【观点】你会被大数据歧视吗?

泰勒·罗德里格斯(TaylorRodriguez)准备乘飞机出外,她把行程都输进了云端,接下来一切都实现了自动化:   门外的行李由智能路灯负...

2553
来自专栏机器人网

顺德机器人打入本田汽车厂 朝智能精密升级

日本是老牌机器人制造强国,六月底日本共同社属下媒体THE DAILY NNA却关注起成立才十余年的广东嘉腾机器人自动化有限公司。报道不仅关注南海上市公司东方精工...

3008
来自专栏华章科技

中国新型智慧城市榜单:2019哪里最宜居?哪里最有发展潜力?

导读:近日,国衡智慧、国新智库评估组通过对全国数字经济和智慧城市跨年度持续的跟踪、研究、监测、评估与分析,通报公布2017-2018中国新型智慧城市建设与发展综...

4496
来自专栏罗超频道

乐视接盘侠、中国足球王、怼腾讯先锋,PPTV的好戏才刚开始?

《人民的名义》成为中国娱乐业的一只黑马,并且这部剧也很有机会成为2017年的年度现象级大剧,反腐题材的“正剧”却能取得如此之高的收视率出人意料。许多互联网公司也...

3224
来自专栏新智元

这届I/O大会不行?Pichai专访:AI的万里长征只有谷歌能赢

【新智元导读】谷歌 I/O大会第一天落幕,舆论不乏负面之声,CEO Pichai 却直言,谷歌历来后发而先至,做邮件、做搜索、做地图,谷歌都不是第一,但最终都做...

3155
来自专栏DevOps时代的专栏

走出国门!中国 DevOps 力量欧洲首秀

? 春节前夕,应国外社区邀请,我和雪峰同学代表DevOps时代与高效运维社区游了一趟欧洲!为大家带了很多干货. 今天先跟大家分享一些美图,有大牛、有美食、有美...

3468
来自专栏镁客网

凭什么?这家叫“喔去”的智能共享出行企业这么自信!

1487
来自专栏云加头条

湖南200万党员在云端的组织生活是这样的

“一网一云”支撑着湖南省220万党员学习的需求

2790
来自专栏大数据文摘

不想做下一个中兴!格力成立新公司布局芯片制造

根据国家企业信用信息公示系统显示,2018年8月14日,珠海零边界集成电路有限公司成立,注册资本为10亿元,法定代表人是董明珠,公司股东为珠海格力电器股份有限公...

571
来自专栏科技向令说

激荡四十年,中国企业家群画像

小米在香港上市已经过去20多天了,在接受央视采访时,雷军表示,当初在小米开业那天应该理个发。开业那天没想到小米会成为一家这么伟大的公司。

932

扫码关注云+社区