com.meituan.mtrace
mtrace
1.1.14
3.1.2 StudentParam.java
=======================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 16:34
-
@description: 学生定义(作为输入参数)
**/
@ThriftStruct
public class StudentParam {
private Integer id;
private String name;
@ThriftConstructor
public StudentParam(Integer id, String name) {
this.id = id;
this.name = name;
}
@ThriftField
public Integer getId() {
return id;
}
@ThriftField(1)
public void setId(Integer id) {
this.id = id;
}
@ThriftField
public String getName() {
return name;
}
@ThriftField(2)
public void setName(String name) {
this.name = name;
}
}
3.1.3 GenderEnum.java
=====================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 16:41
-
@description: 性别定义(作为输出参数)
**/
@ThriftEnum
public enum GenderEnum {
GENDER_MALE(1, “male”, “男性”),
GENDER_FEMALE(2, “female”, “女性”),
GENDER_UNKNOWN(0, “unknown”, “未知性别”);
private Integer id;
private String value;
private String desc;
GenderEnum(Integer id, String value, String desc) {
this.id = id;
this.value = value;
this.desc = desc;
}
// @ThriftEnumValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
3.1.4 DemoThriftService.java
============================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 16:19
-
@description: Thrift 接口定义
**/
@ThriftService
public interface DemoThriftService {
@ThriftMethod
String getVersion() throws TException;
@ThriftMethod
StudentParam getGenderStudent(GenderEnum gender) throws TException;
}
3.2 服务提供者 service-provider
==========================
引入依赖包:service-interface 是刚定义的公用接口,hystrix 用于容错。 本模块中,先是实现公用接口,再定义发布相关配置,再运行
ServiceProviderApplication 启动服务提供者。
3.2.1 pom.xml
=============
com.meituan
service-interface
1.0.0
com.netflix.hystrix
hystrix-javanica
1.5.12
3.2.2 DemoThriftServiceImpl.java
================================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 17:29
-
@description: Thrift 接口实现(服务提供者)
**/
public class DemoThriftServiceImpl implements DemoThriftService {
@Override
public String getVersion() throws TException {
return “1.0.0”;
}
@Override
@HystrixCommand
public StudentParam getGenderStudent(GenderEnum gender) throws TException {
return new StudentParam(1, “张三”);
}
}
3.2.3 DemoServiceProviderConfig.java
====================================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 17:45
-
@description: Thrift 发布(服务提供者)
**/
@Configuration
public class DemoServiceProviderConfig {
@Resource(name = “serviceProcessor”)
private DemoThriftService serviceProcessor;
@Bean(name = “serviceProcessor”)
public DemoThriftService getDemoThriftService() {
return new DemoThriftServiceImpl();
}
@Bean(name = “serverPublisher”, initMethod = “publish”, destroyMethod = “destroy”)
public ThriftServerPublisher getThriftServerPublisher() {
ThriftServerPublisher serverPublisher = new ThriftServerPublisher();
serverPublisher.setServiceInterface(DemoThriftService.class); // [MUST] 接口类
serverPublisher.setServiceImpl(serviceProcessor); // [MUST] 实现类
serverPublisher.setAppKey(APPKEY_TEST_SERVER); // [MUST] 服务提供者 appkey
serverPublisher.setPort(9001); // [MUST] 服务提供者监听端口
return serverPublisher;
}
}
3.2.4 ServiceProviderApplication.java
=====================================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 17:50
-
@description: 启动(服务提供者)
**/
@SpringBootApplication
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
3.3 服务消费者 service-consumer
==========================
引入依赖包:service-interface 是刚定义的公用接口。 本模块中,先是指定服务提供者、消费选项,再使用共用接口定义 Controller 来调用,再运行
ServiceConsumerApplication 启动服务消费者。 启动浏览器访问
http://localhost:8080/demo,即可调用成功。
3.3.1 pom.xml
=============
com.meituan
service-interface
1.0.0
3.3.2 DemoServiceConsumerConfig.java
====================================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 18:01
-
@description: Thrift 消费者
**/
@Configuration
public class DemoServiceConsumerConfig {
@Bean(name = “thriftPoolConfig”)
public MTThriftPoolConfig getMTThriftPoolConfig() {
MTThriftPoolConfig thriftPoolConfig = new MTThriftPoolConfig();
thriftPoolConfig.setMaxActive(100);
thriftPoolConfig.setMaxIdle(20);
thriftPoolConfig.setMinIdle(5);
thriftPoolConfig.setMaxWait(3000);
thriftPoolConfig.setTestOnBorrow(true);
thriftPoolConfig.setTestOnReturn(false);
thriftPoolConfig.setTestWhileIdle(false);
return thriftPoolConfig;
}
@Bean(name = “demoThriftService”, destroyMethod = “destroy”)
public ThriftClientProxy getThriftClientProxy(MTThriftPoolConfig thriftPoolConfig) {
ThriftClientProxy thriftClientProxy = new ThriftClientProxy();
thriftClientProxy.setMtThriftPoolConfig(thriftPoolConfig); // [可选]配置
thriftClientProxy.setServiceInterface(DemoThriftService.class); // [MUST]接口类
thriftClientProxy.setAppKey(APPKEY_TEST_CLIENT); // [MUST]服务消费者 appkey
thriftClientProxy.setRemoteAppkey(APPKEY_TEST_SERVER); // [MUST]服务提供者 appkey
thriftClientProxy.setRemoteServerPort(9001); // [常用]服务提供者 port
thriftClientProxy.setTimeout(30000); // [常用]调用超时
return thriftClientProxy;
}
}
3.3.3 DemoConsumerController.java
=================================
/**
-
@author: kefeng.wang
-
@date: 2018-06-29 18:10
-
@description: Thrift 演示的外部入口
**/
@RestController
public class DemoConsumerController {
private static final Logger logger = LoggerFactory.getLogger(DemoConsumerController.class);
@Resource
private DemoThriftService demoThriftService;
@GetMapping(“/demo”)