mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
添加dubbo服务调用
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<dubbo.version>2.6.5</dubbo.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
@@ -44,6 +45,22 @@
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Boot Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.boot</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
<version>0.2.1.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>${dubbo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- swagger2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
|
@@ -42,7 +42,7 @@ public class AlipayBookController {
|
||||
return story;
|
||||
}
|
||||
|
||||
// 调用story服务
|
||||
// 通过Feign调用story服务
|
||||
@ApiMapping(value = "alipay.book.story.get")
|
||||
public Object getBook2() {
|
||||
Story story = new Story();
|
||||
|
@@ -0,0 +1,49 @@
|
||||
package com.gitee.sop.bookweb.controller;
|
||||
|
||||
import com.alibaba.dubbo.config.annotation.Reference;
|
||||
import com.gitee.sop.servercommon.annotation.ApiMapping;
|
||||
import com.gitee.sop.story.api.param.DemoParam;
|
||||
import com.gitee.sop.story.api.result.DemoResult;
|
||||
import com.gitee.sop.story.api.service.DemoService;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 调用dubbo服务,provider是story,见DefaultDemoService.java
|
||||
* dubbo配置方式参见:https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/README_CN.md
|
||||
*
|
||||
* 对比SpringCloud提供的Feign,dubbo会方便很多。
|
||||
*
|
||||
* Feign的使用方式参见:com.gitee.sop.bookweb.controller.AlipayBookController#getBook2()
|
||||
*/
|
||||
@RestController
|
||||
public class DemoConsumerController {
|
||||
|
||||
@Reference(version = "${demo.service.version}",
|
||||
application = "${dubbo.application.id}",
|
||||
url = "dubbo://localhost:12345")
|
||||
private DemoService demoService;
|
||||
|
||||
// http://localhost:3333/sayHello?name=jim
|
||||
@RequestMapping("/sayHello")
|
||||
public String sayHello(@RequestParam String name) {
|
||||
return demoService.sayHello(name);
|
||||
}
|
||||
|
||||
// http://localhost:3333/dubboStory
|
||||
@RequestMapping("/dubboStory")
|
||||
public DemoResult dubboStory() {
|
||||
DemoParam demoParam = new DemoParam();
|
||||
demoParam.setId(1);
|
||||
return demoService.getStory(demoParam);
|
||||
}
|
||||
|
||||
// 作为开放接口
|
||||
@ApiMapping(value = "dubbo.story.get")
|
||||
public DemoResult openApi(DemoParam demoParam) {
|
||||
// 通过dubbo调用story提供的服务
|
||||
return demoService.getStory(demoParam);
|
||||
}
|
||||
|
||||
}
|
@@ -15,4 +15,19 @@ eureka:
|
||||
host: localhost
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://${eureka.host}:${eureka.port}/eureka/
|
||||
defaultZone: http://${eureka.host}:${eureka.port}/eureka/
|
||||
|
||||
# dubbo consumer
|
||||
demo:
|
||||
service:
|
||||
version: 1.0.0
|
||||
dubbo:
|
||||
application:
|
||||
id: dubbo-consumer-demo
|
||||
name: dubbo-consumer-demo
|
||||
protocol:
|
||||
id: dubbo
|
||||
name: dubbo
|
||||
port: 12345
|
||||
management:
|
||||
port: 8081
|
@@ -0,0 +1,14 @@
|
||||
package com.gitee.sop.story.api.param;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* dubbo 参数
|
||||
* @author tanghc
|
||||
*/
|
||||
@Data
|
||||
public class DemoParam implements Serializable {
|
||||
private int id;
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.gitee.sop.story.api.result;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* dubbo返回结果
|
||||
* @author tanghc
|
||||
*/
|
||||
@Data
|
||||
public class DemoResult implements Serializable {
|
||||
private int id;
|
||||
private String name;
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package com.gitee.sop.story.api.service;
|
||||
|
||||
import com.gitee.sop.story.api.param.DemoParam;
|
||||
import com.gitee.sop.story.api.result.DemoResult;
|
||||
|
||||
public interface DemoService {
|
||||
|
||||
String sayHello(String name);
|
||||
|
||||
/**
|
||||
* 获取故事名称
|
||||
* @param param
|
||||
* @return
|
||||
*/
|
||||
DemoResult getStory(DemoParam param);
|
||||
|
||||
}
|
@@ -16,6 +16,7 @@
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<dubbo.version>2.6.5</dubbo.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
@@ -39,6 +40,23 @@
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo Spring Boot Starter -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.boot</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
<version>0.2.1.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>${dubbo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- swagger2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
@@ -84,6 +102,7 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@@ -0,0 +1,29 @@
|
||||
package com.gitee.sop.storyweb.service;
|
||||
|
||||
import com.alibaba.dubbo.config.annotation.Service;
|
||||
import com.gitee.sop.story.api.param.DemoParam;
|
||||
import com.gitee.sop.story.api.result.DemoResult;
|
||||
import com.gitee.sop.story.api.service.DemoService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
@Service(version = "1.0.0")
|
||||
public class DefaultDemoService implements DemoService {
|
||||
|
||||
/**
|
||||
* The default value of ${dubbo.application.name} is ${spring.application.name}
|
||||
*/
|
||||
@Value("${dubbo.application.name}")
|
||||
private String serviceName;
|
||||
|
||||
public String sayHello(String name) {
|
||||
return String.format("[%s] : Hello, %s", serviceName, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DemoResult getStory(DemoParam param) {
|
||||
DemoResult demoResult = new DemoResult();
|
||||
demoResult.setId(2);
|
||||
demoResult.setName("dubbo 白雪公主, param=" + param);
|
||||
return demoResult;
|
||||
}
|
||||
}
|
@@ -16,4 +16,14 @@ eureka:
|
||||
host: localhost
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://${eureka.host}:${eureka.port}/eureka/
|
||||
defaultZone: http://${eureka.host}:${eureka.port}/eureka/
|
||||
|
||||
# dubbo provider
|
||||
dubbo:
|
||||
protocol:
|
||||
name: dubbo
|
||||
port: 12345
|
||||
registry:
|
||||
address: N/A
|
||||
scan:
|
||||
base-packages: com.gitee.sop.storyweb.service
|
||||
|
58
sop-test/src/test/java/com/gitee/sop/DubboDemoTest.java
Normal file
58
sop-test/src/test/java/com/gitee/sop/DubboDemoTest.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package com.gitee.sop;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.alipay.AlipaySignature;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 测试dubbo服务调用,需要启动book服务
|
||||
* @author tanghc
|
||||
*/
|
||||
public class DubboDemoTest extends TestBase {
|
||||
|
||||
String url = "http://localhost:8081/api"; // zuul
|
||||
String appId = "2019032617262200001";
|
||||
// 支付宝私钥
|
||||
String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
|
||||
|
||||
// 测试dubbo服务,book会调用story提供的服务。参见:DemoConsumerController.java
|
||||
@Test
|
||||
public void testDemo() throws Exception {
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
params.put("method", "dubbo.story.get");
|
||||
params.put("format", "json");
|
||||
params.put("charset", "utf-8");
|
||||
params.put("sign_type", "RSA2");
|
||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
params.put("version", "1.0");
|
||||
|
||||
// 业务参数
|
||||
Map<String, String> bizContent = new HashMap<>();
|
||||
bizContent.put("id", "222");
|
||||
|
||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
||||
|
||||
System.out.println("----------- 请求信息 -----------");
|
||||
System.out.println("请求参数:" + buildParamQuery(params));
|
||||
System.out.println("商户秘钥:" + privateKey);
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
System.out.println("待签名内容:" + content);
|
||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
System.out.println("签名(sign):" + sign);
|
||||
|
||||
params.put("sign", sign);
|
||||
|
||||
System.out.println("----------- 返回结果 -----------");
|
||||
String responseData = post(url, params);// 发送请求
|
||||
System.out.println(responseData);
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user