eureka和feign的使用
2024-10-09 10:39:02
1 eureka和feign的简介(copy来的)
eureka:Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务。Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务发现功能。
feign:Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。 它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔 编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon 和Eureka,在使用Feign时提供负载平衡的http客户端。
2 eureka的服务端
2.1 pom.xml中导入相应的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.2 在配置文件中编码相关配置
#指定端口
server:
port: 7001
#创建eureka实例
eureka:
instance:
hostname: localhost
client:
#不允许服务端向自己注册
register-with-eureka: false
fetch-registry: false
#客户端注册时需要使用的地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.3 在主类上添加相关注解
3 服务提供者(feign)
3.1 pom.xml中导入相关依赖
<!--eureka的客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2 在配置文件中编码相关配置
#配置端口
server:
port: 7003
#注册到eureka客户端的名字
spring:
application:
name: feign-client
#eureka相关实例
eureka:
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
#在eureka中显示ip地址
prefer-ip-address: true
ip-address: 127.0.0.1
client:
registerWithEureka: true
fetchRegistry: true
#eureka服务端的地址
serviceUrl:
defaultZone: http://localhost:7001/eureka/
3.3 在主类上添加相关依赖
3.4 需要被服务消费者调用的方法(例)
@RestController
public class UserController {
@GetMapping("/user")
public String user(@PathVariable("id") Integer id){
return "测试成功啦啦啦啦啦啦"+id;
}
}
4 服务消费者(feign)
4.1 在pom.xml 中添加相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
4.2 在配置文件中编码相关配置
server:
port: 7002
ribbon:
ReadTimeout: 60000
ConnectTimeout: 60000
eureka:
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
prefer-ip-address: true
ip-address: 127.0.0.1
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:7001/eureka/
spring:
application:
name: eureka_client
4.3 在主类上添加相关注解
4.4 服务消费者的feign端(即为一个接口,通过此接口与另一个服务建立连接)
@Component
@FeignClient(value = "FEIGN-CLIENT",name = "FEIGN-CLIENT")
public interface Feign { @RequestMapping(value = "/user",method = RequestMethod.GET)
public String user(@PathVariable("id") Integer id); }
4.5 服务消费端调用此接口的方法
@RestController
public class UserController {
@Autowired
private Feign feign; @GetMapping("/user/{id}")
public String user(@PathVariable("id") Integer id){
return feign.user(id);
}
}
最新文章
- lhgDialog窗口组件
- 【转载】Pyqt 编写的俄罗斯方块
- 并发编程 13—— 线程池的使用 之 配置ThreadPoolExecutor 和 饱和策略
- Hadoop 生态系统
- Linux 图形化操作
- Hibernate,JPA注解@SecondaryTable
- javascript oo实现(转)
- SQL Server 数据库文件管理
- windows server 2003 取消登录Ctrl+Alt+delete
- JavaScript 中的事件设计
- 使用dom4j讲xml字符串递归遍历成Map
- c++(选择排序)
- uva1395 枚举不同区间的最小生成树
- java第二周的学习知识2
- confirm
- Keras/tensorflow出现‘Loaded runtime CuDNN library: 7.0.5 but source was compiled with: 7.1.14’错误的解决办法
- Static Proxy (静态代理模式)
- python学习之winreg模块
- Storm-源码分析- metric
- struts2 :Unable to load configuration. ……struts-default.xml:46:178异常解决