parent
0f22307dc1
commit
2015e88eec
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>dubbo-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>dubbo过滤器</name>
|
||||||
|
|
||||||
|
<artifactId>dubbo-spi-filter</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.xjs.filter;
|
||||||
|
|
||||||
|
import org.apache.dubbo.common.constants.CommonConstants;
|
||||||
|
import org.apache.dubbo.common.extension.Activate;
|
||||||
|
import org.apache.dubbo.rpc.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dubbo过滤器
|
||||||
|
*
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
@Activate(group = {CommonConstants.CONSUMER})
|
||||||
|
public class DubboInvokeFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||||
|
long startTime = 0L;
|
||||||
|
long endTime = 0L;
|
||||||
|
try {
|
||||||
|
startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
Result invoke = invoker.invoke(invocation);
|
||||||
|
|
||||||
|
endTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
return invoke;
|
||||||
|
} finally {
|
||||||
|
String serviceName = invocation.getServiceName();
|
||||||
|
System.out.println("serviceName---invoke time : " + (endTime - startTime) + "ms");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
timeFilter=com.xjs.filter.DubboInvokeFilter
|
@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>dubbo-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>dubbo负载均衡器</name>
|
||||||
|
|
||||||
|
<artifactId>dubbo-spi-loadbalance</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
import org.apache.dubbo.rpc.Invocation;
|
||||||
|
import org.apache.dubbo.rpc.Invoker;
|
||||||
|
import org.apache.dubbo.rpc.RpcException;
|
||||||
|
import org.apache.dubbo.rpc.cluster.LoadBalance;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dubbo自定义负载均衡器
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-24
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class LoadBalancer implements LoadBalance {
|
||||||
|
@Override
|
||||||
|
public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
|
||||||
|
|
||||||
|
//按照 IP + 端口排序
|
||||||
|
|
||||||
|
Invoker<T> tInvoker = invokers.stream().sorted((i1, i2) -> {
|
||||||
|
final int ipCompare = i1.getUrl().getIp().compareTo(i2.getUrl().getIp());
|
||||||
|
if (ipCompare == 0) {
|
||||||
|
return Integer.compare(i1.getUrl().getPort(), i2.getUrl().getPort());
|
||||||
|
}
|
||||||
|
return ipCompare;
|
||||||
|
}).findFirst().get();
|
||||||
|
|
||||||
|
return tInvoker;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
onlyFirst=com.xjs.LoadBalancer
|
@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>dubbo-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>dubbo线程池</name>
|
||||||
|
|
||||||
|
<artifactId>dubbo-spi-shreadpool</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1 @@
|
|||||||
|
watching=com.xjs.thread.WatchingThreadPool
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.rpc.RpcContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-24
|
||||||
|
*/
|
||||||
|
public class XmlConsumerApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
|
||||||
|
|
||||||
|
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
|
||||||
|
|
||||||
|
HelloService helloService = context.getBean(HelloService.class);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
System.in.read();
|
||||||
|
|
||||||
|
String word = helloService.sayHello("word", 500
|
||||||
|
);
|
||||||
|
|
||||||
|
//利用Future 模式来获取
|
||||||
|
Future<Object> future = RpcContext.getContext().getFuture();
|
||||||
|
System.out.println("future result:"+future.get());
|
||||||
|
|
||||||
|
System.out.println(word);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
|
||||||
|
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||||
|
<dubbo:application name="service-consumer"/>
|
||||||
|
<dubbo:registry address="nacos://127.0.0.1:8848"/>
|
||||||
|
<dubbo:reference id="helloService" interface="com.xjs.service.HelloService">
|
||||||
|
<dubbo:method name="sayHello" async="true">
|
||||||
|
<dubbo:argument type="String"/>
|
||||||
|
<dubbo:argument type="int"/>
|
||||||
|
</dubbo:method>
|
||||||
|
</dubbo:reference>
|
||||||
|
<context:component-scan base-package="com.xjs.bean"/>
|
||||||
|
</beans>
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import org.apache.dubbo.config.RegistryConfig;
|
||||||
|
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class DubboApplication2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
|
||||||
|
context.start();
|
||||||
|
System.in.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableDubbo(scanBasePackages = "com.xjs.service")
|
||||||
|
@PropertySource("classpath:/dubbo-provider2.properties")
|
||||||
|
static class ProviderConfiguration{
|
||||||
|
@Bean
|
||||||
|
public RegistryConfig registryConfig() {
|
||||||
|
RegistryConfig registryConfig = new RegistryConfig();
|
||||||
|
registryConfig.setAddress("nacos://127.0.0.1:8848");
|
||||||
|
return registryConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import org.apache.dubbo.config.RegistryConfig;
|
||||||
|
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class DubboApplication3 {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
|
||||||
|
context.start();
|
||||||
|
System.in.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableDubbo(scanBasePackages = "com.xjs.service")
|
||||||
|
@PropertySource("classpath:/dubbo-provider3.properties")
|
||||||
|
static class ProviderConfiguration{
|
||||||
|
@Bean
|
||||||
|
public RegistryConfig registryConfig() {
|
||||||
|
RegistryConfig registryConfig = new RegistryConfig();
|
||||||
|
registryConfig.setAddress("nacos://127.0.0.1:8848");
|
||||||
|
return registryConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
dubbo.application.name=service-provider
|
||||||
|
|
||||||
|
dubbo.protocol.name=dubbo
|
||||||
|
|
||||||
|
dubbo.protocol.port=7712
|
||||||
|
|
||||||
|
dubbo.protocol.host=localhost
|
||||||
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
|||||||
|
dubbo.application.name=service-provider
|
||||||
|
|
||||||
|
dubbo.protocol.name=dubbo
|
||||||
|
|
||||||
|
dubbo.protocol.port=7713
|
||||||
|
|
||||||
|
|
||||||
|
dubbo.protocol.host=localhost
|
||||||
|
|
Loading…
Reference in new issue