提交 114a8c45 编写于 作者: 武汉红喜's avatar 武汉红喜

Upgrade to dubbo 2.7.0, remove whatsmars-dubbo-sentinel,whatsmars-dubbo-service

上级 a29ff9de
......@@ -41,7 +41,7 @@
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<mybatis.version>3.4.5</mybatis.version>
<dubbo.version>2.6.4</dubbo.version>
<dubbo.version>2.7.0</dubbo.version>
<rocketmq.version>4.3.2</rocketmq.version>
<guava.version>20.0</guava.version>
......@@ -54,8 +54,6 @@
<commons-dbcp.version>1.4</commons-dbcp.version>
<fastjson.version>1.2.44</fastjson.version>
<swagger2.version>2.5.0</swagger2.version>
<jetty_version>6.1.26</jetty_version>
<hessian.version>4.0.7</hessian.version>
<zkclient.version>0.9</zkclient.version>
<curator.version>2.12.0</curator.version>
<javassist.version>3.24.1-GA</javassist.version>
......@@ -73,7 +71,7 @@
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
......@@ -144,16 +142,6 @@
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty_version}</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>${hessian.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
......
......@@ -21,13 +21,10 @@
<module>whatsmars-dubbo-provider</module>
<module>whatsmars-dubbo-provider-boot</module>
<module>whatsmars-dubbo-consumer-boot</module>
<module>whatsmars-dubbo-service</module>
<module>whatsmars-dubbo-sentinel</module>
</modules>
<properties>
<dubbo-boot.version>0.2.0</dubbo-boot.version>
<sentinel.version>1.3.0-GA</sentinel.version>
</properties>
<dependencyManagement>
......@@ -37,17 +34,6 @@
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-dubbo-adapter</artifactId>
<version>${sentinel.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>${sentinel.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......
......@@ -20,10 +20,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
......
......@@ -15,10 +15,6 @@
<name>${project.artifactId}</name>
<description>The demo consumer module of dubbo project</description>
<properties>
<skip_maven_deploy>false</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>org.hongxi</groupId>
......@@ -26,7 +22,7 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
......@@ -35,7 +31,7 @@
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
......@@ -51,30 +47,11 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- for hessian protocol -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
</dependency>
<!-- netty4 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-dubbo-adapter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -3,7 +3,7 @@
*/
package org.hongxi.whatsmars.dubbo.demo.consumer;
import com.alibaba.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcContext;
import org.hongxi.whatsmars.dubbo.demo.api.BarService;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar;
......
......@@ -3,7 +3,7 @@
*/
package org.hongxi.whatsmars.dubbo.demo.consumer;
import com.alibaba.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcContext;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
......
......@@ -3,7 +3,7 @@
*/
package org.hongxi.whatsmars.dubbo.demo.consumer;
import com.alibaba.dubbo.rpc.service.EchoService;
import org.apache.dubbo.rpc.service.EchoService;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
......@@ -24,13 +24,13 @@ public class DemoConsumer {
// hessian protocol 直连
DemoService demoService2 = (DemoService) context.getBean("demoService2");
String hello2 = demoService2.sayHello("hessian直连");
String hello2 = demoService2.sayHello("dubbo直连");
System.out.println(hello2);
// hessian protocol
DemoService demoService3 = (DemoService) context.getBean("demoService3");
String hello3 = demoService3.sayHello("hessian");
System.out.println(hello3);
// DemoService demoService3 = (DemoService) context.getBean("demoService3");
// String hello3 = demoService3.sayHello("hessian");
// System.out.println(hello3);
// service group
DemoService demoService4 = (DemoService) context.getBean("demoService4");
......
......@@ -14,12 +14,11 @@
<dubbo:reference id="demoService" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" mock="return null" />
<!-- url="hessian://127.0.0.1:8080/org.hongxi.whatsmars.dubbo.demo.api.DemoService" -->
<dubbo:reference id="demoService2" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService"
url="hessian://127.0.0.1:8080" />
url="dubbo://127.0.0.1:20880" />
<!--protocol默认dubbo,可指定,如果provider没有以该protocol发布,consumer启动会报 No provider-->
<dubbo:reference id="demoService3" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" protocol="hessian" />
<!--<dubbo:reference id="demoService3" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" protocol="hessian" />-->
<dubbo:reference id="demoService4" group="new" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" />
......
package org.hongxi.whatsmars.dubbo.demo.consumer;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
public class Consumer {
......
......@@ -18,11 +18,7 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
......@@ -38,11 +34,6 @@
<artifactId>whatsmars-dubbo-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hongxi</groupId>
<artifactId>whatsmars-dubbo-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......
......@@ -2,13 +2,11 @@ package org.hongxi.whatsmars.dubbo.demo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by javahongxi on 2017/12/4.
*/
@SpringBootApplication
@ComponentScan(value = {"org.hongxi.whatsmars.dubbo"})
public class ProviderApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProviderApplication.class, args);
......
......@@ -6,8 +6,6 @@ package org.hongxi.whatsmars.dubbo.demo.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.hongxi.whatsmars.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -15,13 +13,9 @@ import java.util.Date;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private UserService userService;
public String sayHello(String name) {
boolean registerSuccess = userService.register(name);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
\ No newline at end of file
......@@ -6,8 +6,6 @@ package org.hongxi.whatsmars.dubbo.demo.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import org.hongxi.whatsmars.dubbo.demo.api.OtherService;
import org.hongxi.whatsmars.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -15,13 +13,9 @@ import java.util.Date;
@Service(registry = "otherRegistry")
public class OtherServiceImpl implements OtherService {
@Autowired
private UserService userService;
public String sayHello(String name) {
boolean registerSuccess = userService.register(name);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
return "Hi " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
\ No newline at end of file
......@@ -20,14 +20,6 @@ dubbo:
scan:
basePackages: org.hongxi.whatsmars.dubbo.demo.provider.service
endpoints:
dubbo:
enabled: true
management:
port: 9091
security:
enabled: false
registry:
other:
protocol: zookeeper
......
......@@ -15,10 +15,6 @@
<name>${project.artifactId}</name>
<description>The demo provider module of dubbo project</description>
<properties>
<skip_maven_deploy>false</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>org.hongxi</groupId>
......@@ -26,7 +22,7 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
......@@ -35,7 +31,7 @@
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
......@@ -51,27 +47,11 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- for hessian protocol -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
</dependency>
<!-- netty4 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.hongxi</groupId>
<artifactId>whatsmars-dubbo-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
......
package org.hongxi.whatsmars.dubbo.demo.provider;
import org.hongxi.whatsmars.dubbo.demo.api.BarService;
import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar;
public class BarServiceMock implements BarService {
@Override
public Bar findBar(String barId) {
// 你可以伪造容错数据,此方法只在出现RpcException时被执行
return new Bar("", "容错数据", "");
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ public class DemoProvider {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
com.alibaba.dubbo.container.Main.main(args);
org.apache.dubbo.container.Main.main(args);
}
......
......@@ -3,10 +3,8 @@
*/
package org.hongxi.whatsmars.dubbo.demo.provider;
import com.alibaba.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcContext;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.hongxi.whatsmars.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
......@@ -15,13 +13,9 @@ import java.util.Date;
@Service("demoService")
public class DemoServiceImpl implements DemoService {
@Autowired
private UserService userService;
public String sayHello(String name) {
boolean registerSuccess = userService.register(name);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
\ No newline at end of file
......@@ -3,14 +3,19 @@
*/
package org.hongxi.whatsmars.dubbo.demo.provider;
import org.apache.dubbo.rpc.RpcContext;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service("demoService2")
public class DemoServiceImpl2 implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hi " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
\ No newline at end of file
......@@ -19,7 +19,7 @@
(也是官方推荐,出于稳定性和性能的折中考虑)
序列化方式由provider决定,所以团队要决定改序列化方式时,只需修改provider配置 -->
<dubbo:protocol name="dubbo" port="20880" server="netty4" />
<dubbo:protocol name="hessian" port="8080" server="jetty" />
<!--<dubbo:protocol name="hessian" port="8080" server="jetty" />-->
<context:component-scan base-package="org.hongxi.whatsmars.dubbo"/>
......@@ -34,7 +34,6 @@
<dubbo:service group="new" interface="org.hongxi.whatsmars.dubbo.demo.api.DemoService" ref="demoService2" protocol="dubbo" />
<!-- 通过loadbalance="leastactive"也可实现并发控制,此 Loadbalance 会调用并发数最小的 Provider(Consumer端并发数) -->
<dubbo:service interface="org.hongxi.whatsmars.dubbo.demo.api.BarService" ref="barService"
mock="org.hongxi.whatsmars.dubbo.demo.provider.BarServiceMock" executes="10" protocol="dubbo" />
<dubbo:service interface="org.hongxi.whatsmars.dubbo.demo.api.BarService" ref="barService" executes="10" protocol="dubbo" />
</beans>
\ No newline at end of file
package org.hongxi.whatsmars.dubbo.demo.provider;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.hongxi.whatsmars.dubbo.demo.api.DemoService;
public class Provider {
......
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>whatsmars-dubbo</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S4</version>
</parent>
<artifactId>whatsmars-dubbo-sentinel</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>use sentinel in dubbo</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-dubbo-adapter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel;
/**
* @author Eric Zhao
*/
public interface FooService {
String sayHello(String name);
String doAnother();
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.consumer;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Eric Zhao
*/
@Configuration
@DubboComponentScan
public class ConsumerConfiguration {
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-consumer");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
// Uncomment below line if you don't want to enable Sentinel for Dubbo service consumers.
// consumerConfig.setFilter("-sentinel.dubbo.consumer.filter");
return consumerConfig;
}
@Bean
public FooServiceConsumer annotationDemoServiceConsumer() {
return new FooServiceConsumer();
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import org.hongxi.whatsmars.dubbo.sentinel.FooService;
/**
* @author Eric Zhao
*/
public class FooServiceConsumer {
@Reference
private FooService fooService;
public String sayHello(String name) {
return fooService.sayHello(name);
}
public String doAnother() {
return fooService.doAnother();
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo1;
import com.alibaba.csp.sentinel.slots.block.SentinelRpcException;
import org.hongxi.whatsmars.dubbo.sentinel.consumer.ConsumerConfiguration;
import org.hongxi.whatsmars.dubbo.sentinel.consumer.FooServiceConsumer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Please add the following VM arguments:
* <pre>
* -Djava.net.preferIPv4Stack=true
* -Dcsp.sentinel.api.port=8721
* -Dproject.name=dubbo-consumer-demo
* </pre>
*
* @author Eric Zhao
*/
public class FooConsumerBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
consumerContext.register(ConsumerConfiguration.class);
consumerContext.refresh();
FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class);
for (int i = 0; i < 15; i++) {
try {
String message = service.sayHello("Eric");
System.out.println("Success: " + message);
} catch (SentinelRpcException ex) {
System.out.println("Blocked");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo1;
import com.alibaba.csp.sentinel.init.InitExecutor;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Collections;
/**
* Please add the following VM arguments:
* <pre>
* -Djava.net.preferIPv4Stack=true
* -Dcsp.sentinel.api.port=8720
* -Dproject.name=dubbo-provider-demo
* </pre>
*
* @author Eric Zhao
*/
public class FooProviderBootstrap {
private static final String RES_KEY = "org.hongxi.whatsmars.dubbo.sentinel.FooService:sayHello(java.lang.String)";
private static final String INTERFACE_RES_KEY = "org.hongxi.whatsmars.dubbo.sentinel.FooService";
public static void main(String[] args) {
// Users don't need to manually call this method.
InitExecutor.doInit();
initFlowRule();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ProviderConfiguration.class);
context.refresh();
System.out.println("Service provider is ready");
}
private static void initFlowRule() {
FlowRule flowRule = new FlowRule();
flowRule.setResource(RES_KEY);
flowRule.setCount(10);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setLimitApp("default");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo1;
import com.alibaba.dubbo.config.annotation.Service;
import org.hongxi.whatsmars.dubbo.sentinel.FooService;
import java.time.LocalDateTime;
/**
* @author Eric Zhao
*/
@Service
public class FooServiceImpl implements FooService {
@Override
public String sayHello(String name) {
return String.format("Hello, %s at %s", name, LocalDateTime.now());
}
@Override
public String doAnother() {
return LocalDateTime.now().toString();
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo1;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Eric Zhao
*/
@Configuration
@DubboComponentScan("org.hongxi.whatsmars.dubbo.sentinel.demo1")
public class ProviderConfiguration {
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-provider");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(25758);
return protocolConfig;
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo2;
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallbackRegistry;
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.SentinelRpcException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.dubbo.rpc.RpcResult;
import org.hongxi.whatsmars.dubbo.sentinel.consumer.ConsumerConfiguration;
import org.hongxi.whatsmars.dubbo.sentinel.consumer.FooServiceConsumer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Please add the following VM arguments:
* <pre>
* -Djava.net.preferIPv4Stack=true
* -Dcsp.sentinel.api.port=8721
* -Dproject.name=dubbo-consumer-demo
* </pre>
*
* @author Eric Zhao
*/
public class FooConsumerBootstrap {
private static final String RES_KEY = "org.hongxi.whatsmars.dubbo.sentinel.FooService:sayHello(java.lang.String)";
private static final String INTERFACE_RES_KEY = "org.hongxi.whatsmars.dubbo.sentinel.FooService";
private static final ExecutorService pool = Executors.newFixedThreadPool(10,
new NamedThreadFactory("dubbo-consumer-pool"));
public static void main(String[] args) {
initFlowRule();
registerFallback();
AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
consumerContext.register(ConsumerConfiguration.class);
consumerContext.refresh();
FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class);
for (int i = 0; i < 10; i++) {
pool.submit(() -> {
try {
String message = service.sayHello("Eric");
System.out.println("Success: " + message);
} catch (Exception ex) {
if (BlockException.isBlockException(ex)) {
System.out.println("service.sayHello degrade");
} else {
ex.printStackTrace();
}
}
});
pool.submit(() -> System.out.println("Another: " + service.doAnother()));
}
}
private static void initFlowRule() {
DegradeRule rule = new DegradeRule();
rule.setResource(RES_KEY);
rule.setCount(1);
rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
rule.setTimeWindow(1);
rule.setLimitApp("default");
DegradeRuleManager.loadRules(Collections.singletonList(rule));
}
private static void registerFallback() {
// Register fallback handler for consumer.
// If you only want to handle degrading, you need to
// check the type of BlockException.
DubboFallbackRegistry.setConsumerFallback((a, b, ex) ->
new RpcResult("Error: " + ex.getClass().getTypeName()));
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo2;
import com.alibaba.csp.sentinel.init.InitExecutor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Please add the following VM arguments:
* <pre>
* -Djava.net.preferIPv4Stack=true
* -Dcsp.sentinel.api.port=8720
* -Dproject.name=dubbo-provider-demo
* </pre>
*
* @author Eric Zhao
*/
public class FooProviderBootstrap {
public static void main(String[] args) {
// Users don't need to manually call this method.
InitExecutor.doInit();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ProviderConfiguration.class);
context.refresh();
System.out.println("Service provider is ready");
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo2;
import com.alibaba.dubbo.config.annotation.Service;
import org.hongxi.whatsmars.dubbo.sentinel.FooService;
import java.time.LocalDateTime;
/**
* @author Eric Zhao
*/
@Service
public class FooServiceImpl implements FooService {
@Override
public String sayHello(String name) {
return String.format("Hello, %s at %s", name, LocalDateTime.now());
}
@Override
public String doAnother() {
return LocalDateTime.now().toString();
}
}
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hongxi.whatsmars.dubbo.sentinel.demo2;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Eric Zhao
*/
@Configuration
@DubboComponentScan("org.hongxi.whatsmars.dubbo.sentinel.demo2")
public class ProviderConfiguration {
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("demo-provider");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(25758);
return protocolConfig;
}
}
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>whatsmars-dubbo</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S4</version>
</parent>
<artifactId>whatsmars-dubbo-service</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>普通service</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.dubbo.service;
/**
* Created by shenhongxi on 2017/12/4.
*/
public interface UserService {
boolean register(String name);
}
package org.hongxi.whatsmars.dubbo.service.impl;
import org.hongxi.whatsmars.dubbo.service.UserService;
import org.springframework.stereotype.Service;
/**
* Created by shenhongxi on 2017/12/4.
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public boolean register(String name) {
// register ...
return true;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册