说明
平台使用Feign实现微服务之间的RPC调用。
相关配置与开发流程如下
开发
下面是开发rpc客户端的相关步骤
开发RPC调用客户端类
在提供服务的工程中创建.fegin.的子包路径,如下图所示:
在包路径中创建调用服务的客户端类,并基于fegin相关注解开发接口调用代码:
package com.houyi.servicea.feign;
import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.houyi.common.model.PageResult;
import com.houyi.common.model.Result;
import com.houyi.servicea.feign.fallback.DemoDomainServiceFallbackFactory;
import com.houyi.servicea.model.DemoDomain;
@FeignClient(name = ServiceNameConstants.SERVICE_SAMPLE_A, fallbackFactory = DemoDomainServiceFallbackFactory.class, decode404 = true)
public interface DemoDomainService {
@GetMapping(value = "/demodomain", params = "params")
PageResult<DemoDomain> list(@RequestParam("params") Map<String, Object> params);
@GetMapping(value = "/demodomain/{id}")
public Result<DemoDomain> findUserById(@PathVariable("id") Long id);
@PostMapping(value = "/demodomain", params = "demoDomain")
public Result save(@RequestParam("demoDomain") DemoDomain demoDomain);
@DeleteMapping(value = "/demodomain/{id}")
public Result delete(@PathVariable("id") Long id);
}
编写降级服务实现
当调用的服务下线或者服务触发了熔断,限流的阀值。将会对服务做降级处理。
为了提高服务质量,需要编写服务降级的工厂类,并配置在@FeignClient注解的fallbackFactory属性中。
package com.houyi.servicea.feign.fallback;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.houyi.common.model.PageResult;
import com.houyi.common.model.Result;
import com.houyi.servicea.feign.DemoDomainService;
import com.houyi.servicea.model.DemoDomain;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class DemoDomainServiceFallbackFactory implements FallbackFactory<DemoDomainService> {
@Override
public DemoDomainService create(Throwable throwable) {
return new DemoDomainService() {
@Override
public PageResult<DemoDomain> list(Map<String, Object> params) {
log.error("通过翻页信息查询DemoDomain异常:{}", params, throwable);
return new PageResult<DemoDomain>();
}
@Override
public Result<DemoDomain> findUserById(Long id) {
log.error("通过用户ID信息查询DemoDomain异常:{}", id, throwable);
return new Result<DemoDomain>();
}
@Override
public Result save(DemoDomain demoDomain) {
log.error("创建DemoDomain对象异常:{}", demoDomain, throwable);
return new Result();
}
@Override
public Result delete(Long id) {
log.error("通过用户ID删除DemoDomain异常:{}", id, throwable);
return new Result();
}
};
}
}
使用RPC客户端组件
导入RPC客户端组件
服务工程会由maven的插件,为工程生成一个针对客户端调用的jar包。
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>client</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<classifier>client</classifier>
<includes>
<include>**/model/**</include>
<include>**/feign/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
所以在引入服务调用客户端的组件包时,需要配置classifier参数:
<dependency>
<groupId>com.houyi.saas.projectsample</groupId>
<artifactId>service-sample-a</artifactId>
<classifier>client</classifier>
</dependency>
确认引入平台的RPC组件包
<dependency>
<groupId>com.houyi.saas</groupId>
<artifactId>ribbon-spring-boot-starter</artifactId>
</dependency>
确认配置工程下的RPC相关配置
##### feign配置
feign.sentinel.enabled=true
feign.hystrix.enabled=false
feign.okhttp.enabled=true
feign.httpclient.enabled=false
feign.client.config.feignName.connectTimeout=30000
feign.client.config.feignName.readTimeout=30000
## 开启Feign请求响应压缩
feign.compression.request.enabled=true
feign.compression.response.enabled=true
## 配置压缩文档类型及最小压缩的文档大小
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048
开启工程RPC调用功能
在SpringBoot的启动类上配置@FeignClients注解,开启RPC调用功能。
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注入RPC调用组件
@Resource
private DemoDomainService demoDomainService;
/**
* 列表
*/
@ApiOperation(value = "查询列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
@ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
})
@GetMapping("/demodomain")
public PageResult<DemoDomain> list(@RequestParam Map<String, Object> params) {
return demoDomainService.list(params);
}
网友评论