spring cloud spirng整合feign

Posted zfzf1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud spirng整合feign相关的知识,希望对你有一定的参考价值。

restserver

@RestController
public class PoliceController {

	@RequestMapping(value = "/call/{id}", method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Police call(@PathVariable Integer id, HttpServletRequest request) {
		Police p = new Police();
		p.setId(id);
		p.setName("angus");
		p.setMessage(request.getRequestURL().toString());
		return p;
	}
	
	@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
	public String hello(@PathVariable String name) {
		return "Hello, " + name;
	}
	
	@RequestMapping(value = "/hellowd", method = RequestMethod.GET)
	public String helloWithOutArg() {
		return "Hello World";
	}
}

  feign client interface

@FeignClient("spring-feign-provider")
public interface HelloClient {

	@RequestMapping(method = RequestMethod.GET, value="/hello/{name}")
	String hello(@PathVariable("name") String name);
	
	
	@RequestMapping(method = RequestMethod.GET, value="/call/{id}")
	Police getPolice(@PathVariable("id") Integer id);
	
	@MyUrl(url = "/hellowd", method = "GET")
	String myHello();
}

  feign client

@RestController
public class TestController {
	
	@Autowired
	private HelloClient helloClient;

	@RequestMapping(method = RequestMethod.GET, value="/router")
	public String router() {
		String result = helloClient.hello("angus");
		return result;
	}

	@RequestMapping(method = RequestMethod.GET, value="/police", 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Police getPolice() {
		Police p = helloClient.getPolice(1);
		return p;
	}
	
	@RequestMapping(method = RequestMethod.GET, value="/myhello")
	public String myHello() {
		return helloClient.myHello();
	}
}

  

以上是关于spring cloud spirng整合feign的主要内容,如果未能解决你的问题,请参考以下文章

spirng整合rmi

SpringCloud分布式事务实战在微服务1中创建整合函数,调用微服务2

如何使用 Spring Cloud Feign 发布表单 URL 编码的数据

Spirng+SpringMVC+Mybatis+Shiro 整合---实现用户认证

springCloud:Spirng Cloud简介

最新版Spring Cloud Alibaba微服务架构-Openfeign服务调用篇