网站首页 > 教程文章 正文
1.服务发现组件 Eureka
Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现 SpringCloud的服务发现功能。Eureka包含两个组件:Eureka Server和Eureka Client。
- Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中 的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
- Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就别一个内置的、使用轮 询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒, 如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个 服务节点移除(默认90秒)。
1.1Eureka服务端开发
(1)创建ihrm_eureka模块
(2)引入依赖 父工程pom.xml定义SpringCloud版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
ihrm_eureka模块pom.xml引入eureka-server
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
(3)ihrm_eureka模块添加application.yml
server:
port: 6868 #服务端口
eureka:
client:
registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所有无需注册
fetchRegistry: false #是否从Eureka中获取注册信息
serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
defaultZone: http://127.0.0.1:${server.port}/eureka/
(4) 配置启动类
/**
* Eure服务端
*/
@SpringBootApplication
@EnableEurekaServer
public class EureApplication {
public static void main(String[] args) {
SpringApplication.run(EureApplication.class);
}
}
1.2 微服务注册
(1)将其他微服务模块添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
(2)修改每个微服务的application.yml,添加注册eureka服务的配置
#微服务注册到eureka配置
eureka:
client:
service-url:
defaultZone: http://localhost:6868/eureka/
(3)修改每个服务类的启动类,添加注解
@EnableEurekaClient
最终,eureka服务运行之后界面如下:
http://localhost:6868/
2.微服务调用组件Feign
Feign是简化Java HTTP客户端开发的工具(java-to-httpclient-binder),它的灵感来自于Retrofit、JAXRS-2.0和 WebSocket。Feign的初衷是降低统一绑定Denominator到HTTP API的复杂度,不区分是否为restful
2.1搭建Feigh调用
(1)在ihrm_system模块添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
(2)修改ihrm_system模块的启动类,添加注解
@EnableDiscoveryClient
@EnableFeignClients
(3)在Ihrm_system模块创建com.ihrm.system.client包,包下创建接口
其中@FeignClient("ihrm-company")指定需要调用的某个其他微服务名称,该名称在yml文件中已配置如下
#spring配置
spring:
#1.应用配置
application:
name: ihrm-company #指定服务名
package com.ihrm.system.client;
import com.ihrm.common.entity.Result;
import com.ihrm.domain.company.Department;
import org.springframework.cloud.openfeign.FeignClient;
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;
/**
* 声明接口,通过feign调用其他微服务
*/
//声明调用的微服务名称
@FeignClient("ihrm-company")
public interface DepartmentFeignClient {
/**
* 调用微服务的接口
*/
@RequestMapping(value="/company/department/{id}",method = RequestMethod.GET)
public Result findById(@PathVariable(value="id") String id);
@RequestMapping(value="/company/department/search",method = RequestMethod.POST)
public Department findByCode(@RequestParam(value="code") String code, @RequestParam(value="companyId") String companyId);
}
(4)修改Ihrm_system模块的 UserController
@Autowired
private DepartmentFeignClient departmentFeignClient;
/**
* 测试Feign组件
* 调用系统微服务的/test接口传递部门id,通过feign调用部门微服务获取部门信息
*/
@RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
public Result testFeign(@PathVariable(value = "id") String id) {
Result result = departmentFeignClient.findById(id);
return result;
}
(5)配置Feign拦截器添加请求头
在ihrm_common服务添加,方便其他模块都可以调用。
package com.ihrm.common.feign;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
@Configuration
public class FeignConfiguration {
//配置feign拦截器,解决请求头问题
@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
//获取所有浏览器发送的请求属性,请求头赋值到feign
public void apply(RequestTemplate requestTemplate) {
//请求属性
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if(attributes != null) {
HttpServletRequest request = attributes.getRequest();
//获取浏览器发起的请求头
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement(); //请求头名称 Authorization
String value = request.getHeader(name);//请求头数据 "Bearer b1dbb4cf-7de6-41e5-99e2-0e8b7e8fe6ee"
requestTemplate.header(name,value);
}
}
}
}
};
}
}
猜你喜欢
- 2025-09-11 Spring Boot3 中 RESTful 接口调用全解析:从阻塞到响应式的实战指南
- 2025-09-11 springcloud实战:服务间通信OpenFeign熔断
- 2025-09-11 项目终于用上了动态Feign,真香!_feign动态服务名
- 2025-09-11 RestTemplate和Feign的区别_feign和httpclient的区别
- 2025-09-11 OpenFeign:让你的Java代码像本地调用一样简单
- 2025-09-11 【完结14章】SpringCloud+Netty集群实战千万级 IM系统
- 2025-09-11 Spring Cloud OpenFeign - 远程调用
- 2025-09-11 「SpringCloud」(十二)OpenFeign+Ribbon实现负载均衡
- 2025-09-11 微服务 - 服务接口调用 OpenFeign
- 2025-09-11 Sentinel 限流详解-Sentinel与OpenFeign服务熔断那些事
- 最近发表
-
- K8s 部署频繁出错?一套流程教你快速定位故障,工作效率翻倍
- 防火墙服务无法启用,显示灰色的解决办法
- 网络问题-电脑无法上网处理思路以及办法 (总集)
- Win10学院:Windows Denfender无法启动怎么办?
- Windows账户登录问题解决方案_登录windows账户什么意思
- IIS无法启动提示计算机上"."的服务W3SVC,一分钟搞定,抓紧收藏
- 已申请到免费顶级域名如何管理_顶级域名免费注册
- 网站被劫持了老是跳转怎么办_网站被劫持到其它网站如何解决
- 这些“常用药”被注销!涉及维生素、去痛片、眼药水等!快看看你家有吗?
- 《皕宋楼藏书志》清 藏书家陆心源与其门人李宗莲合编的藏书目录
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)