代码拉取完成,页面将自动刷新
1、 3.0.0 开启spring boot 3.0 + 版本、jdk 17 支持
spring:
invoker:
connectionRequestTimeout: 15000 #请求超时
connectTimeout: 20000 #连接超时
readTimeout: 15000 #读取超时
serializable: json、jdk #默认使用json方式序列化
request:
client: httpClient || okHttp
#最大链接数
maxConnections: 100
#最大空闲链接,单位毫秒
timeToLive: 1000 * 30
server:
#全局服务端地址配置
address: 127.0.0.1:9092
webSocket:
#webSocket模式下,握手地址
endpoint: httpInvoker
hystrix:
enabled: true #启动熔断降级处理,默认true
command:
#hystrix配置
thread:
coreSize = 10;
maximumSize = 10;
keepAliveTimeMinutes = 1;
maxQueueSize = -1;
queueSizeRejectionThreshold = 5;
allowMaximumSizeToDivergeFromCoreSize = false;
rollingStatisticalWindowInMilliseconds = 10000;
rollingStatisticalWindowBuckets = 10;
依赖 | 版本 | 描述 |
---|---|---|
spring-boot-invoker-core | 3.0.0 | invoker核心包 |
spring-boot-invoker-hystrix | 3.0.0 | invoker熔断 |
spring-boot-invoker-security | 3.0.0 | invoker security |
spring-boot-starter-invoker | 3.0.0 | spring boot使用 |
spring-boot-starter-redis-invoker | 3.0.0 | spring redis使用 |
spring-boot-starter-amqp-invoker | 3.0.0 | spring rabbitmq使用 |
spring-boot-starter-websocket-invoker | 3.0.0 | spring websocket使用 |
spring-cloud-starter-invoker | 3.0.0 | spring微服务使用 |
spring-invoker-dependencies | 3.0.0 | 版本依赖 |
<!-- https://mvnrepository.com/artifact/com.i360day/spring-cloud-starter-invoker -->
<dependency>
<groupId>com.i360day</groupId>
<artifactId>spring-boot-starter-invoker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.i360day</groupId>
<artifactId>spring-boot-starter-redis-invoker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.i360day</groupId>
<artifactId>spring-boot-starter-amqp-invoker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.i360day</groupId>
<artifactId>spring-boot-starter-websocket-invoker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.i360day</groupId>
<artifactId>spring-cloud-starter-invoker</artifactId>
<version>${project.version}</version>
</dependency>
1、 启动类添加invoker注解
@EnableRemoteDiscoveryClient//spring boot 模式 自动发现@RemoteCLient
@EnableAmqpDiscoveryClient//rabbitmq 模式 自动发现@RemoteCLient
@EnableRedisDiscoveryClient//redis 模式 自动发现@RemoteCLient
@EnableWebSocketeDiscoveryClient//webSocket 模式 自动发现@RemoteCLient
public class TestBootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(TestBootstrap.class);
}
}
2、 暴露service
@Service
public class TestService implements TestServiceFacade {
public String test(){
return "ok";
}
}
注:TestServiceFacade类必须有@RemoteClient标记,否则无效
3、 添加接口与熔断,并指向模块调用注册中心地址进行远程访问 3.1) 有注册中心下使用,如果没有项目名(contextPath)则为空或不填写
方式一:
@RemoteClient(name = "cx-test",fallback = TestServiceFacadeHystrix.class, contextPath = "/projectName")
public interface TestServiceFacade {
public String test();
}
public class TestServiceFacadeHystrix implements TestServiceFacade{
public String test(){
return "ok";
}
}
方式二:
#客服端调用
@RemoteClient(fallback = TestServiceFacadeHystrix.class, contextPath = "/projectName")
public interface TestServiceFacade extends TestServiceModule {
public String test();
}
#降级回调
public class TestServiceFacadeHystrix implements TestServiceFacade{
public String test(){
return "ok";
}
}
#模块独立,供子类继承
@RemoteModule(name = "cx-test", address = "127.0.0.1:9092")
public interface TestServiceModule {
}
方式三:
@RemoteClient(name = "cx-test", FallbackFactory = TestServiceFacadeHystrix.class, contextPath = "/projectName")
public interface TestServiceFacade {
public String test();
}
@Component
public class SpringInvokerSampleServiceHystrix implements FallbackFactory {
@Override
public Object create(Throwable var1) {
return new SpringInvokerSampleServiceFacade() {
@Override
public String test() {
return null;
}
};
}
}
方式四:
@RemoteClient(FallbackFactory = TestServiceFacadeHystrix.class, contextPath = "/projectName")
public interface TestServiceFacade extends TestServiceModule{
public String test();
}
@Component
public class SpringInvokerSampleServiceHystrix implements FallbackFactory {
@Override
public Object create(Throwable var1) {
return new SpringInvokerSampleServiceFacade() {
@Override
public String test() {
return null;
}
};
}
}
#模块独立,供子类继承
@RemoteModule(name = "cx-test", address = "127.0.0.1:9092")
public interface TestServiceModule {
}
新增remoteModule注解后也可以这样写,获取当前请求的地址配置
@RemoteModule(name = "cx-test", contextPath = "/projectName")
public interface TestModuleFacade {
public String test();
}
@RemoteClient(fallback = TestServiceFacadeHystrix.class)
public interface TestServiceFacade extends TestModuleFacade{
public String test();
}
3.2) 非注册中心下使用,如果没有项目名(contextPath)则为空或不填写
@RemoteClient(address = "127.0.0.1:8080",fallback = TestServiceFacadeHystrix.class, contextPath = "/projectName")
public interface TestServiceFacade {
public String test();
}
public class TestServiceFacadeHystrix implements TestServiceFacade{
public String test(){
return "ok";
}
}
新增remoteModule注解后也可以这样写,获取当前请求的地址配置
@RemoteModule(name = "cx-test", address = "127.0.0.1:8080", contextPath = "/projectName")
public interface TestModuleFacade {
public String test();
}
@RemoteClient(fallback = TestServiceFacadeHystrix.class)
public interface TestServiceFacade extends TestModuleFacade{
public String test();
}
4、 客服端使用接口
@RestController
@RequestMapping("/test")
public class ChatController {
@Autowired
private TestServiceFacade testServiceFacade;
@RequestMapping("/test")
public String test(){
return testServiceFacade.test();
}
}
5、security oauth2 服务调用验证
忽略远程service
@IgnoreRemoteService
public class TestService{
...
}
加入请求头
@Component
public class RequestInterceptor implements HttpInvokerRequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if ((authentication instanceof AbstractAuthenticationToken)) {
AbstractAuthenticationToken aat = (AbstractAuthenticationToken)authentication;
if ((aat.getDetails() instanceof OAuth2AuthenticationDetails)) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)aat.getDetails();
requestTemplate.addHeaders("Authorization", new String(String.format("%s %s", new Object[] { details.getTokenType(), details.getTokenValue() })));
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型