请简述网站建设的一般流程,左侧菜单设置设置 wordpress,资源网站平台建设方案,网站建设百度一、前言
本篇主要是围绕着两个点#xff0c;1、集成 Feign#xff0c;2、分离feign接口层#xff0c;独立服务#xff1b;
还有一点就是上篇文章的服务 iot-channel、system-server 服务名称调整成为了 chain-iot-channel、chain-system二、搭建 chain-common 服务
pom.…一、前言
本篇主要是围绕着两个点1、集成 Feign2、分离feign接口层独立服务
还有一点就是上篇文章的服务 iot-channel、system-server 服务名称调整成为了 chain-iot-channel、chain-system二、搭建 chain-common 服务
pom.xml properties!-- lombok --lombok.version1.18.26/lombok.version/properties!-- Dependencies --dependencies!-- Lombok Dependency --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion${lombok.version}/version/dependency/dependencieschain-common 项目暂时只是空项目二、搭建 chain-starter/chain-feign-starter 服务
chain-starter
chain-starter 服务只是一个 pom 项目主要作用是来包含一些启动服务例如 chain-feign-starter 之类chain-feign-starter
搭建这个服务的主要是目的是后续会有很多服务会引用到 Feign 框架如果在每个服务独立引用 Feign在后续的升级版本或需要增加 Feign 的配置就会很麻烦所以现在统一管理起来dependencies!-- feign 客户端 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency/dependencies三、chain-system、chain-iot-channel 集成 Feign
pom.xml 增加 Feign 引用dependencygroupIdcom.chain/groupIdartifactIdchain-feign-starter/artifactIdversion${chain.version}/version/dependency四、服务配置 Feign
1、启动服务增加注解
在 chain-system、chain-iot-channel 启动服务都增加 EnableFeignClients 注解开发Feign 客户端SpringBootApplication
EnableDiscoveryClient
EnableFeignClients
public class IotChannelServeApp {public static void main(String[] args) {SpringApplication.run(IotChannelServeApp.class, args);}
}SpringBootApplication
EnableDiscoveryClient
EnableFeignClients
public class SystemServerApp {public static void main(String[] args) {SpringApplication.run(SystemServerApp.class);}
}2、chain-iot-channel 服务增加被调用接口
IotChannelInterface.java
RestController
RequestMapping(path /iot/channel/open/api)
public class IotChannelInterface {OverrideGetMapping(path /testIotChannelFeign)public String testIotChannelFeign() {return test iot channel feign open api;}
}
3、chain-system 服务增加调用接口
SystemForIotChannelInterfaceClient.java
FeignClient(name chain-iot-channel, url http://localhost:10020, path /iot/channel/open/api)
public interface SystemForIotChannelInterfaceClient {GetMapping(path /testIotChannelFeign)String testIotChannelFeign();
}
在这里需要注意一点的是如果在 IotChannelInterface.java 中配置了RequestMapping(path /iot/channel/open/api)那么在 SystemForIotChannelInterfaceClient.java 中就需要增加 path /iot/channel/open/api 配置
还有另一点就是如果单独使用 Feign没有集成 Ribbon那么就需要在 FeignClient 注解中增加 url 配置项因为没有 Ribbon 框架是无法实现负载均衡那么 name 参数的配置不会直接调用到服务的只能增加 url 配置五、独立 Feign 调用接口
1、增加 chain-open-api/chain-iot-channel-api 服务
chain-open-api
chain-open-api 和 chain-starter 服务一样只是一个 pom 项目主要作用是来包含项目中每个服务对应的 open api 项目chain-iot-channel-api
pom.xml dependencies!-- 自定义 Feign --dependencygroupIdcom.chain/groupIdartifactIdchain-feign-starter/artifactIdversion${chain.version}/version/dependency/dependenciesIotChannelInterfaceApi.java
public interface IotChannelInterfaceApi {/*** 测试 iot channel 服务是否可用** return String*/GetMapping(path /testIotChannelFeign)String testIotChannelFeign();
}
2、增加对 chain-iot-channel-api 的引用
chain-iot-channel\chain-system
pom.xml dependencygroupIdcom.chain/groupIdartifactIdchain-iot-channel-api/artifactIdversion${chain.version}/version/dependency3、改造IotChannelInterface.java、SystemForIotChannelInterfaceClient.java
IotChannelInterface.java、
RestController
RequestMapping(path /iot/channel/open/api)
public class IotChannelInterface implements IotChannelInterfaceApi {Overridepublic String testIotChannelFeign() {return test iot channel feign open api;}
}
SystemForIotChannelInterfaceClient.java
FeignClient(name chain-iot-channel, url http://localhost:10020, path /iot/channel/open/api)
public interface SystemForIotChannelInterfaceClient extends IotChannelInterfaceApi {
}
最后附上项目结构图