第一个Feign程序

feign一个REST客户端,目的是为了简化webservice客户端的开发.
Feign工作机制:
Feign通过注解注入一个模板化请求进行工作,只需在发送之前关闭它,参数就可以被直接的运用到模板中。然而这样也限制了Feign,只支持文本形式的API,他在相应请求等方面极大的简化了系统。同时,他也是十分容易进行单元测试的。
服务端(由两个服务一个http://localhost:8083/person/1,另一个http://localhost:8083/hello):

<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>com</groupId>
  <artifactId>first-boot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  		<version>1.5.7.RELEASE</version>
  	</dependency>
  </dependencies>
</project>
@Controller
public class MyController {
	@GetMapping("/hello")
	@ResponseBody
	public String hello(){
		return "hello world";
	}
}
@RestController
public class MyRestController {
	@RequestMapping(value="/person/{id}",method=RequestMethod.GET,
			produces=MediaType.APPLICATION_JSON_VALUE)
	public Person getPerson(@PathVariable Integer id){
		Person p=new Person();
		p.setId(id);
		p.setName("angus");
		p.setAge(30);
		return p;
	}
}
@SpringBootApplication
public class FirstApp {

	public static void main(String[] args) {
		//SpringApplication.run(FirstApp.class, args);
		new SpringApplicationBuilder(FirstApp.class).properties("server.port="+8083).run(args);
	}

}

先用CXF框架来调用服务端程序:

<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>com</groupId>
  <artifactId>cxf-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>org.apache.cxf</groupId>
  		<artifactId>cxf-core</artifactId>
  		<version>3.1.10</version>
  	</dependency>
  	<dependency>
  		<groupId>org.apache.cxf</groupId>
  		<artifactId>cxf-rt-rs-client</artifactId>
  		<version>3.1.10</version>
  	</dependency>
  </dependencies>
</project>
public class CxfClient {

	public static void main(String[] args) throws Exception {
		// 创建WebClient
		WebClient client = WebClient.create("http://localhost:8083/person/1");
		// 获取响应
		Response response = client.get();
		// 获取响应内容
		InputStream ent = (InputStream) response.getEntity();
		String content = IOUtils.readStringFromStream(ent);
		// 输出字符串
		System.out.println(content);
	}

}

下面用Feign调用服务端程序:

<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>com</groupId>
  <artifactId>FeignClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
  		<groupId>io.github.openfeign</groupId>
  		<artifactId>feign-core</artifactId>
  		<version>9.5.0</version>
  	</dependency>
  	<dependency>
  		<groupId>io.github.openfeign</groupId>
  		<artifactId>feign-gson</artifactId>
  		<version>9.5.0</version>
  	</dependency>
  </dependencies>
</project>
public interface HelloClient {
	@RequestLine("GET /hello")
	public String hello();
	@RequestLine("GET /person/{id}")
	public Person getPerson(@Param("id") Integer id);
}
public class HelloMain {

	public static void main(String[] args) {
		HelloClient client=Feign.builder().target(HelloClient.class,
				"http://localhost:8083");
		String result=client.hello();
		System.out.println(result);
	}

}
public class PersonMain {

	public static void main(String[] args) {
		HelloClient client=Feign.builder()
				.decoder(new GsonDecoder())
				.target(HelloClient.class,
				
				"http://localhost:8083");
		Person p=client.getPerson(1);
		System.out.println(p.getId());
		System.out.println(p.getName());
		System.out.println(p.getAge());
		System.out.println(p.getClass());
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值