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

dubbo生产实践

上级 6e90d82f
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.api;
package com.whatsmars.dubbo.demo.api;
public interface DemoService {
......
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.api;
package com.whatsmars.dubbo.demo.api;
public interface UserService {
......
package com.whatsmars.dubbo.demo.consumer.rpc;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itlong.whatsmars.dubbo.demo.api.DemoService;
import com.whatsmars.dubbo.demo.api.DemoService;
import org.springframework.stereotype.Component;
/**
......
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.consumer;
package com.whatsmars.dubbo.demo.consumer;
import com.itlong.whatsmars.dubbo.demo.api.DemoService;
import com.itlong.whatsmars.dubbo.demo.api.UserService;
import com.whatsmars.dubbo.demo.api.DemoService;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -13,23 +12,15 @@ public class DemoAction {
private DemoService demoService;
private UserService userService;
public void setDemoService(DemoService demoService) {
this.demoService = demoService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void start() throws Exception {
for (int i = 0; i < Integer.MAX_VALUE; i ++) {
try {
String hello = demoService.sayHello("world" + i);
String hi = userService.sayHi("java" + i);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hello);
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] " + hi);
} catch (Exception e) {
e.printStackTrace();
}
......
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.consumer;
package com.whatsmars.dubbo.demo.consumer;
public class DemoConsumer {
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean class="com.itlong.whatsmars.dubbo.demo.consumer.DemoAction" init-method="start">
<bean class="com.whatsmars.dubbo.demo.consumer.DemoAction" init-method="start">
<property name="demoService" ref="demoService" />
<property name="userService" ref="userService" />
</bean>
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--&lt;!&ndash; 提供方应用信息,用于计算依赖关系 &ndash;&gt;-->
......@@ -25,7 +11,6 @@
<!--&lt;!&ndash; 使用zookeeper注册中心暴露服务地址 &ndash;&gt;-->
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />-->
<dubbo:reference id="demoService" interface="com.itlong.whatsmars.dubbo.demo.api.DemoService" />
<dubbo:reference id="userService" interface="com.itlong.whatsmars.dubbo.demo.api.UserService" />
<dubbo:reference id="demoService" interface="com.whatsmars.dubbo.demo.api.DemoService" />
</beans>
\ No newline at end of file
......@@ -5,7 +5,7 @@ package com.whatsmars.dubbo.demo.provider.resource;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import com.itlong.whatsmars.dubbo.demo.api.DemoService;
import com.whatsmars.dubbo.demo.api.DemoService;
import com.whatsmars.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
......
......@@ -43,6 +43,11 @@
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>com.itlong</groupId>
<artifactId>whatsmars-dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
......
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.provider;
import com.alibaba.dubbo.rpc.RpcContext;
import com.itlong.whatsmars.dubbo.demo.api.UserService;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UserServiceImpl implements UserService {
public String sayHi(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hi " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hi " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
\ No newline at end of file
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.provider;
package com.whatsmars.dubbo.demo.provider;
import java.io.IOException;
......
/**
* Created by shenhongxi on 2017/6/21.
*/
package com.itlong.whatsmars.dubbo.demo.provider;
package com.whatsmars.dubbo.demo.provider;
import com.alibaba.dubbo.rpc.RpcContext;
import com.itlong.whatsmars.dubbo.demo.api.DemoService;
import com.whatsmars.dubbo.demo.api.DemoService;
import com.whatsmars.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
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 + ", response form provider: " + RpcContext.getContext().getLocalAddress();
return "Hello " + name + ", registerSuccess:" + registerSuccess + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 1999-2011 Alibaba Group.
-
- 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-autowire="byName">
<!--&lt;!&ndash; 提供方应用信息,用于计算依赖关系 &ndash;&gt;-->
<!--<dubbo:application name="whatsmars_provider" />-->
......@@ -26,11 +15,9 @@
<!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />-->
<!--&lt;!&ndash; 用dubbo协议在20880端口暴露服务 &ndash;&gt;-->
<!--<dubbo:protocol name="dubbo" port="20880" />-->
<bean id="demoService" class="com.itlong.whatsmars.dubbo.demo.provider.DemoServiceImpl" />
<bean id="userService" class="com.itlong.whatsmars.dubbo.demo.provider.UserServiceImpl" />
<dubbo:service interface="com.itlong.whatsmars.dubbo.demo.api.DemoService" ref="demoService" />
<dubbo:service interface="com.itlong.whatsmars.dubbo.demo.api.UserService" ref="userService" />
<context:component-scan base-package="com.whatsmars.dubbo"/>
<dubbo:service interface="com.whatsmars.dubbo.demo.api.DemoService" ref="demoService" />
</beans>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册