Spring cloud 学习(二)Feign
来源:互联网 发布:网络职业男士头像大全 编辑:程序博客网 时间:2024/06/06 01:27
Feign:一个可以把远程服务提供方的 rest 接口变成本地方法调用
对上一个项目进行改造:
1.改造service0
Service0.java:
package service0;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication(scanBasePackages = "service0")
@EnableEurekaClient
@EnableWebMvc
public class Service0 {
public static void main(String[] args) {
SpringApplication.run(Service0.class, args);
}
}
-----------------------------------------------------------------------------------------------------------
Service0Controller.java:
package service0.controller;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.web.bind.annotation.*;
@RestController
public class Service0Controller {
@GetMapping("/service0/{value}")
public String service0(@PathVariable("value") String value){
return value + ":" + FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.sss").format(new Date());
}
}
------------------------------------------------------------------------------
启动discovery,启动service0后,测试service0的调用:
2.改造service1
Service1.java:
package service1;
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;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication(scanBasePackages = "service1")
@EnableFeignClients
@EnableDiscoveryClient
@EnableWebMvc
public class Service1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Service1.class, args);
}
}
--------------------------------------------------------------------
Server0Client.java:
package service1.client;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("service0") //service0的application中的name属性一致
public interface Server0Client {
@RequestMapping(method=RequestMethod.GET,path="/service0/{value}")
public String service0(@PathVariable("value") String value);//与service0controller的方法一致
}
--------------------------------------------------------------------------
Service1Controller.java:
package service1.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import service1.client.Server0Client;
@RestController
public class Service1Controller {
@Autowired
private Server0Client server0Client;
@GetMapping("/service1/{value2}")
public String service1(@PathVariable("value2") String value2){
return server0Client.service0(value2);
}
}
-----------------------------------------------------------------------
启动discovery,启动service0,service1后,测试service1的调用:
访问service1时,又通过本地调用调用service0的接口,最后返回service0的处理结果。
- Spring cloud 学习(二)Feign
- Spring Cloud学习--声明式调用(Feign)
- Spring Cloud feign
- Spring Cloud Feign诠释
- spring cloud-feign
- Spring Cloud Feign常见问题
- spring cloud Feign
- spring cloud feign
- Spring Cloud feign使用
- Spring Cloud Feign
- Spring Cloud Feign
- spring-cloud【Feign】
- spring cloud day03 【feign】
- 疯狂Spring Cloud连载(14)Spring Cloud整合Feign
- 学习Spring Cloud第十一课(Feign的简介及基础使用)
- 学习Spring Cloud第十二课(覆写Feign的默认配置)
- Spring Cloud学习系列第二章:使用Feign调用服务
- Spring Cloud之Feign入门
- BFC
- HTML&CSS初级
- Springboot搭建web项目访问自定义位置的html页面
- FTP部署
- JavaScript 对象_0
- Spring cloud 学习(二)Feign
- python实现阿拉伯数字和罗马数字的互相转换
- Java微信支付功能整理
- 设计模式之禅笔记-享元模式
- 电子通讯录(1)
- 使用Valgrind Memcheck检查内存
- 原创html小游戏,分享给大家!!
- 用java语言设计app签到功能
- FAFU OJ 寻路问题