preface
The previous few blogs focused on the use and principles of Apache Thrift. With the popularity of microserver architectures today, Spring Boot and Spring Cloud naturally come to mind as basic frameworks for microservices. However, Spring Cloud has been a lightweight Restful API based on the HTTP protocol as a means of communication between services since its inception.
In the design of microservices architecture, it can be divided into external services and internal services. The main differences between the two are:
- External servicesBased on:
Restful
In the style ofHTTP
Agreement, approvedThe networkProviding services externally, relatively speakingSimple and versatile. - Internal serviceBased on:
RPC
message-communicationTCP/IP
Agreement, offerIntranetCalls between services to achieveTo reduce the bandwidth,Lower latency rate,To improve performance.
In some application scenarios, especially internal services that need to be invoked frequently, it is necessary to consider whether to transform RPC implementation to improve throughput and system performance, such as authentication services.
The body of the
The paper
Download spring-cloud-starter-thrift and import IDEA development environment. The project address is github.com/ostenant/sp…
spring-cloud-starter-thrift
Spring Cloud
scalable
cross-language
Apache Thrift
Spring-cloud-starter-thrift includes two modules, spring-cloud-starter-thrift-client and spring-cloud-starter-thrift-server. The spring-Cloud-starter-thrift -examples submodule provides three sample projects: Calculator, deposit, and test.
- Calculator: Sample hands-on project.
- Deposit: complex business scenario project example.
- Test: performance test project example.
The service side
- support
Apache Thrift
A variety of primitiveThreaded service model, includingSingle-thread blocking model(simple
),Single-threaded non-blocking model(nonBlocking
),Thread pool blocking model(threadPool
),Semi-synchronous and semi-asynchronous model(hsHa
) andThread selector model(threadedSelector
). - support
Apache Thrift 0.10.0
Available after versionMultiplexing processorTo provide unified registration and management of services. - Supported by theService signature(service
ID
+ clientStub
Interface name + Service version) uniquely identifies the serviceStub
theConcrete implementation classTo support the service versionA smooth upgrade. - support
Server Group
Form the boot mode for eachService instanceYou can turn on more than oneThrift Server
, through differentThe port numberExposure to the client.
The client
- Supported by theService signature(service
ID
+ clientStub
Interface name + service version number) uniquely identifies and invokes the serverStub
Concrete implementation class. - support
Apache Thrift
theTransport
Layer of theConnection Pool Management.To reduceBetween the client and the serverThe connectionThe frequency ofcreateandThe destruction. - Support and
Spring Cloud Consul
theSeamless integrationThe client passesThe heartbeat detectionwithService RegistryConsul
Stay connected, dynamically timedRefreshing the Service List,monitoringThe service ofTo enable the,Shut downandA healthy state. - supportClient load balancing, includingrandom,pollingLoad balancing policies for clients
Thrift
Program through localService cache listImplement dynamic forwarding of calls.
Quick learning
Project Structure:
- calculator
- calculator-client
- calculator-iface
- calculator-server
Spring-cloud-starter-thrift uses version 0.10.0 of thrift. Start with Calculator project, first, compile client Stub Stub and server Skeleton through Thrift IDL (Interface description Language), define interface specification through. Thrift file.
Start with the spring-cloud-starter-thrift root directory, where pum.xml is defined as follows:
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.icekredit.rpc.thrift.examples</groupId>
<version>1.0 the SNAPSHOT</version>
<modules>
<module>calculator-client</module>
<module>calculator-server</module>
<module>calculator-iface</module>
</modules>
<artifactId>calculator</artifactId>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
Copy the code
Package and install the project into a local Maven repository:
mvn clean install
Copy the code
Thrift IDL to write
namespace java com.icekredit.rpc.thrift.example
service CalculatorService {
i32 add(1: i32 arg1, 2: i32 arg2)
i32 subtract(1: i32 arg1, 2: i32 arg2)
i32 multiply(1: i32 arg1, 2: i32 arg2)
i32 division(1: i32 arg1, 2: i32 arg2)
}
Copy the code
Download and install 0.10.0 Thrift of IDL compiler generator, download address: thrift.apache.org/docs/instal… . Java Stub class file generated by the compiler.
thrift -gen java ./CalculatorService.thrift
Copy the code
Calculatorservice.java file generated by the compiler. Calculatorservice.java has thousands of lines of code. For developers, there are only four core interfaces/classes to focus on: Iface, AsyncIface, Client, and AsyncClient.
- Iface:The service sideBy implementing
HelloWorldService.Iface
Interface to theThe clientProvide specificsynchronousBusiness logic. - AsyncIface:The service sideBy implementing
HelloWorldService.Iface
Interface to theThe clientProvide specificasynchronousBusiness logic. - Client:The clientthrough
HelloWorldService.Client
To the instance object ofsynchronousThe way ofAccessing the serverThe service method provided. - AsyncClient:The clientthrough
HelloWorldService.AsyncClient
To the instance object ofasynchronousThe way ofAccessing the serverThe service method provided.
Calculator-iface
Introduce thrift’s Maven dependency into the intermediate contract module, and copy the CalculatorService source file generated by the thrift compiler generator from the previous step into this module.
pom.xml
<parent>
<artifactId>calculator</artifactId>
<groupId>com.icekredit.rpc.thrift.examples</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>calculator-iface</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>
Copy the code
The server (calculator – server)
In the server module:
- spring-cloud-starter-thrift-server:
thrift
server-sidestarter
The program. - calculator-iface: Intermediate contract module, here as server skeleton (
Skeleton
) program.
pom.xml
<parent>
<artifactId>calculator</artifactId>
<groupId>com.icekredit.rpc.thrift.examples</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>calculator-server</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-server</artifactId>
<version>1.0 the SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.icekredit.rpc.thrift.examples</groupId>
<artifactId>calculator-iface</artifactId>
<version>1.0 the SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
Configure the thrift server run parameters in application.yml:
application.yml
## HTTP port number of the Restful service
server:
port: 8080
## Used for Consul health check
endpoints:
actuator:
sensitive: false
enabled: true
management:
security:
enabled: false
## Spring Thrift server configuration
spring:
thrift:
server:
service-id: thrift-rpc-calculator # #
service-model: hsHa ## Semi-synchronous/semi-asynchronous service model
port: 25000 Server Indicates the TCP port number of the RPC service
worker-queue-capacity: 1000
Semi-synchronous/semi-asynchronous service model parameter configuration
hs-ha:
min-worker-threads: 5 Minimum number of working threads
max-worker-threads: 20 ## Maximum number of working threads
keep-alived-time: 3 ## Idle thread lifetime
Copy the code
Implement the internal interface Iface of the Skeleton class CalculatorService generated by Thrift IDL and write specific business logic:
A few things to note here:
- implementation
CalculatorService.Iface
Interface. - Implementation class tag
@ThriftService
Annotations, containing the following properties:- nameThrough:
name
logoThe service name, the default value isThe class name starts with a lowercase letter. - versionThrough:
version
logoThe service version, the default value is1.0
That is to say the sameThe service nameYou can haveMultiple version implementation.
- nameThrough:
RpcCalculatorService.java
@ThriftService(name = "rpcCalculatorService", version = 2.0)
public class RpcCalculatorService implements CalculatorService.Iface {
@Override
public int add(int arg1, int arg2) {
BigDecimal arg1Decimal = new BigDecimal(arg1);
BigDecimal arg2Decimal = new BigDecimal(arg2);
return arg1Decimal.add(arg2Decimal).intValue();
}
@Override
public int subtract(int arg1, int arg2) {
BigDecimal arg1Decimal = new BigDecimal(arg1);
BigDecimal arg2Decimal = new BigDecimal(arg2);
return arg1Decimal.subtract(arg2Decimal).intValue();
}
@Override
public int multiply(int arg1, int arg2) {
BigDecimal arg1Decimal = new BigDecimal(arg1);
BigDecimal arg2Decimal = new BigDecimal(arg2);
return arg1Decimal.multiply(arg2Decimal).intValue();
}
@Override
public int division(int arg1, int arg2) {
BigDecimal arg1Decimal = new BigDecimal(arg1);
BigDecimal arg2Decimal = new BigDecimal(arg2);
returnarg1Decimal.divide(arg2Decimal).intValue(); }}Copy the code
Package the server application:
mvn clean package -Dmaven.test.skip=true
Copy the code
Dockerfile:
FROM openjdk:8-jdk-alpine
ADDThe target/spring - the boot - thrift - server - 0.0.1 - the SNAPSHOT. Jar calculator - server. The jarENTRYPOINT ["java"."-jar"."calculator-server.jar"]
Copy the code
Copy Dockerfile and target/spring-boot-thrift- server-0.0.1-snapshot. jar to thrift server.
docker build . -t icekredit/calculator-server
Copy the code
The client (calculator – the client)
Introduced in the client module:
- spring-cloud-starter-thrift-client:
thrift
The client’sstarter
The program. - calculator-iface: Intermediate contract module, here as client pile (
Stub
) program.
pom.xml
<parent>
<artifactId>calculator</artifactId>
<groupId>com.icekredit.rpc.thrift.examples</groupId>
<version>1.0 the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>calculator-client</artifactId>
<dependencies>
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-client</artifactId>
<version>1.0 the SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.icekredit.rpc.thrift.examples</groupId>
<artifactId>calculator-iface</artifactId>
<version>1.0 the SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
Configure the thrift client’s running parameters in application.yml, which must be consistent with the server configuration:
## HTTP port number of the client Restful service
server:
port: 8080
## Used for Consul health check
endpoints:
actuator:
sensitive: false
enabled: true
management:
security:
enabled: false
## Spring Thrift Client configuration (Automatic configuration of Thrift Client depends on the correct configuration of Spring Cloud Consul)
spring:
application:
name: thrift-calculator-client
cloud:
consul:
host: 192.16891.128. ## Consul's IP address
port: 8500 ## Consul HTTP port number
discovery:
register: false ## do not use the service-based application registration provided by SpringCloud
register-health-check: false ## Do not use Spring Cloud for health checks
retry:
max-attempts: 3
max-interval: 2000
## Thrift Client configuration
thrift:
client:
package-to-scan: com.icekredit.rpc.thrift.example.rpc ## Marks the package path annotated by the @thriftClient interface
service-model: hsHa ## Server thread model (this must be consistent with server, default is hsHa)
Client connection pool configuration
pool:
retry-times: 3 Number of retries after connection timeout
## key consists of IP and Port, which uniquely identifies a service instance
pool-max-total-per-key: 200 The maximum number of connections maintained by the client, including different services and service instances
pool-min-idle-per-key: 10 Minimum number of idle connections per service instance
pool-max-idle-per-key: 40 The maximum number of idle connections per service instance
pool-max-wait: 30000 Maximum idle connection lifetime
connect-timeout: 2000 Connection timeout
Copy the code
There are two considerations for writing the Thrift Client’s Client proxy interface:
- Interfaces need to be inherited fromThe parent interface
ThriftClientAware
And,ThriftClientAware
In theThe generic parameterFill in forThrift IDL
The generatedStub
类CalculatorService
In theClient
Inner classes. - Interface Requirement Identification
@ThriftClient
Annotations,@ThriftClient
Contains the following attributes:- serviceId: bound to this client proxy interface
Thrift
The service sidetheService Registration ID(With the serverconsistent). - refer: Client pile
Stub
For example, here isCalculatorService.class
. - version: of the concrete business implementation classThe version number(No default value
1.0
), needed with the serverconsistent.
- serviceId: bound to this client proxy interface
CalculatorThriftClient.java
@ThriftClient(serviceId = "thrift-rpc-calculator", refer = CalculatorService.class, version = 2.0)
public interface CalculatorThriftClient extends ThriftClientAware<CalculatorService.Client> {}Copy the code
Inject CalculatorThriftClient into the Controller of the client using the @thriftreferer annotation. When used by CalculatorThriftClient. ThriftClient () method, you can call Thrift Server service method.
RpcCalculatorController.java
@RestController
@RequestMapping("/rpc")
public class RpcCalculatorController {
@ThriftReferer
private CalculatorThriftClient calculators;
@GetMapping("/add")
public int add(@RequestParam("arg1") int arg1, @RequestParam("arg2") int arg2) throws Exception {
return calculators.client().add(arg1, arg2);
}
@GetMapping("/subtract")
public int subtract(@RequestParam("arg1") int arg1, @RequestParam("arg2") int arg2) throws Exception {
return calculators.client().subtract(arg1, arg2);
}
@GetMapping("/multiply")
public int multiply(@RequestParam("arg1") int arg1, @RequestParam("arg2") int arg2) throws Exception {
return calculators.client().multiply(arg1, arg2);
}
@GetMapping("/division")
public int division(@RequestParam("arg1") int arg1, @RequestParam("arg2") int arg2) throws Exception {
returncalculators.client().division(arg1, arg2); }}Copy the code
Consul’s address can be configured in the local development environment and run the client program. For container environment tests, configure to package the client program:
mvn clean package -Dmaven.test.skip=true
Copy the code
Dockerfile:
FROM openjdk:8-jdk-alpine
ADDTarget/spring - the boot - thrift - the client - 0.0.1 - the SNAPSHOT. Jar calculator - client. The jarENTRYPOINT ["java"."-jar"."calculator-client.jar"]
Copy the code
Copy Dockerfile and target/spring-boot-thrift-client-0.0.1- snapshot. jar to the server and create a service image of thrift Client.
docker build . -t icekredit/calculator-client
Copy the code
A simple test
Publish the server program
To facilitate the test, start three Thrift Server Docker containers on a host with different ports and specify the corresponding port number and Consul registration information:
Thrift Server Instance 1(port 25001) :
docker run -d -p 8081:8080 -p 25001:25000 --name calculator-server-01 \
-e "SERVICE_25000_NAME=thrift-rpc-calculator" \
-e "SERVICE_25000_CHECK_TCP=/" \
-e "SERVICE_25000_CHECK_INTERVAL=30s" \
-e "SERVICE_25000_CHECK_TIMEOUT=3s" \
-e "SERVICE_25000_TAGS=thrift-rpc-calculator-25001" \
icekredit/calculator-server
Copy the code
Thrift Server Instance 2(port 25002) :
docker run -d -p 8081:8080 -p 25002:25000 --name calculator-server-01 \
-e "SERVICE_25000_NAME=thrift-rpc-calculator" \
-e "SERVICE_25000_CHECK_TCP=/" \
-e "SERVICE_25000_CHECK_INTERVAL=30s" \
-e "SERVICE_25000_CHECK_TIMEOUT=3s" \
-e "SERVICE_25000_TAGS=thrift-rpc-calculator-25002" \
icekredit/calculator-server
Copy the code
Thrift Server instance 3(port 25003) :
docker run -d -p 8081:8080 -p 25003:25000 --name calculator-server-01 \
-e "SERVICE_25000_NAME=thrift-rpc-calculator" \
-e "SERVICE_25000_CHECK_TCP=/" \
-e "SERVICE_25000_CHECK_INTERVAL=30s" \
-e "SERVICE_25000_CHECK_TIMEOUT=3s" \
-e "SERVICE_25000_TAGS=thrift-rpc-calculator-25003" \
icekredit/calculator-server
Copy the code
Check the startup logs of each container. If the following information is displayed, it indicates that the Thrift Server is started successfully and provides RPC services normally.
The 2017-11-19 22:28:47. 12960-779 the INFO [main] C.I.R.T.S.C ontext. ThriftServerContext: Build thrift Server from HsHaServerContext 2017-11-19 22:28:47.820 INFO 12960 -- [main] c.i.r.t.s.p.TRegisterProcessorFactory : Processor bean org.ostenant.springboot.learning.examples.CalculatorService$Processor@445bce9a with signature [thrift-rpc-calculator$org.ostenant.springboot.learning.examples.CalculatorService$2. 0] is instantiated 22:28:47 2017-11-19. 12960-822 the INFO [main] C.I.R.T.S.P.T RegisterProcessorFactory: Single processor org.ostenant.springboot.learning.examples.CalculatorService$Processor@445bce9a register onto multiplexed processor with signature [thrift-rpc-calculator$org.ostenant.springboot.learning.examples.CalculatorService$2. 0] 22:28:47 2017-11-19. 12960-822 the INFO [main] C.I.R.T.S.P.T RegisterProcessorFactory: Multiplexed processor totally owns 1 service processorsCopy the code
Consul and Registrator containers are started and three service instances of Thrift Server are successfully registered with Consul service list:
For details on how to install Consul and Registrator, see Docker+Consul+Registrator.
The server program runs successfully and the Thrift RPC service publishes normally!
Start the client program
Start the Thrift client on local port 8080. After the Thrift client is started normally, the following startup logs are displayed:
The 2017-11-20 11:00:20. 4052-025 the INFO [main]. R.T.C.T hriftClientBeanScannerConfigurer: Base package org.ostenant.springboot.learning.examples.rpc is to be scanned with Com. Icekredit. RPC. Thrift. Client. Scanner. ThriftClientBeanScanner @ 37496720 2017-11-20 11:00:20. 4052-029 the INFO [main] c.i.r.t.c.s.ThriftClientBeanScanner : Packages scanned by thriftClientBeanDefinitionScanner is [org.ostenant.springboot.learning.examples.rpc] 2017-11-20 11:00:20. 4052-029 the INFO [main] C.I.R.T.C.S.T hriftClientBeanScanner: Scanned and found thrift client, bean calculatorThriftClient assigned from org.ostenant.springboot.learning.examples.rpc.CalculatorThriftClient The 2017-11-20 11:00:20. 4052-050 the INFO [main] F.A.A utowiredAnnotationBeanPostProcessor: JSR - 330'javax.inject.Inject' annotation found and supported forAutowiring 2017-11-20 11:00:20.134 INFO 4052 - [main] trationDelegate$BeanPostProcessorChecker : Bean 'org.ostenant.springboot.learning.examples.rest.CalculatorFeignClient' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible forAuto - proxying) 11:00:20 2017-11-20. 4052-136 WARN [main] C.I.R.T.C.S.T hriftClientFactoryBean: Bean class is not found the 2017-11-20 11:00:20. 142 INFO - 4052 [main] C.I.R.T.C.S.T hriftClientFactoryBean: Succeed to instantiate an instance of ThriftClientFactoryBean: Com. Icekredit. RPC. Thrift. Client. Scanner. ThriftClientFactoryBean @ 7 bac686b 11:00:20 2017-11-20. 4052-142 the INFO [main] trationDelegate$BeanPostProcessorChecker : Bean 'calculatorThriftClient' of type [com.icekredit.rpc.thrift.client.scanner.ThriftClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible forAuto-proxying) 2017-11-20 11:00:20.411 INFO 4052 -- [main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration' of type [org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration$$EnhancerBySpringCGLIB$$a9ef18dc] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible forAuto-proxying) 2017-11-20 11:00:20.423 INFO 4052 -- [main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$93dc7598] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible forAuto - proxying) 11:00:21 2017-11-20. 4052-592 the INFO [main] S.B.C.E.T.T omcatEmbeddedServletContainer: Tomcat initialized with port(s): 8080 (http)Copy the code
During startup, all interfaces marked with @thriftClient annotation generate proxy objects and inject them into the Controller with @thriftreferer annotation.
At the same time, a ServerUpdater is enabled when the client is started. The server obtains healthy service node information from the Consul registration list periodically and dynamically and caches it to the local service list.
The 11:02:26 2017-11-20. 4052-726 the INFO [erListUpdater - 0] T.C.L.T hriftConsulServerListLoadBalancer: Refreshed thrift serverList: [thrift-rpc-calculator: [ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25001], host='192.168.91.128', port=25001, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25002], host='192.168.91.128', port=25002, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25003], host='192.168.91.128', port=25003, address='192.168.91.128', isHealth=true}], consul-8301: [ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=8301, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=9301, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=10301, address='192.168.91.128', isHealth=true}], consul-8302: [ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=8302, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=9302, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=10302, address='192.168.91.128', isHealth=true}], thrift-rest-calculator: [ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8081], host='192.168.91.128', port=8081, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8082], host='192.168.91.128', port=8082, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8083], host='192.168.91.128', port=8083, address='192.168.91.128', isHealth=true}]] 2017-11-20 11:02:56. 752 INFO - 4052 [erListUpdater - 0] T.C.L.T hriftConsulServerListLoadBalancer: Refreshed thrift serverList: [thrift-rpc-calculator: [ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25001], host='192.168.91.128', port=25001, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25002], host='192.168.91.128', port=25002, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25003], host='192.168.91.128', port=25003, address='192.168.91.128', isHealth=true}], consul-8301: [ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=8301, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=9301, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=10301, address='192.168.91.128', isHealth=true}], consul-8302: [ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=8302, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=9302, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=10302, address='192.168.91.128', isHealth=true}], thrift-rest-calculator: [ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8081], host='192.168.91.128', port=8081, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8082], host='192.168.91.128', port=8082, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8083], host='192.168.91.128', port=8083, address='192.168.91.128', isHealth=true}]] 2017-11-20 11:03:26. 764 INFO - 4052 [erListUpdater - 0] T.C.L.T hriftConsulServerListLoadBalancer: Refreshed thrift serverList: [thrift-rpc-calculator: [ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25001], host='192.168.91.128', port=25001, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25002], host='192.168.91.128', port=25002, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rpc-calculator', tags=[thrift-rpc-calculator-25003], host='192.168.91.128', port=25003, address='192.168.91.128', isHealth=true}], consul-8301: [ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=8301, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=9301, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8301', tags=[udp], host='192.168.91.128', port=10301, address='192.168.91.128', isHealth=true}], consul-8302: [ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=8302, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=9302, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='consul-8302', tags=[udp], host='192.168.91.128', port=10302, address='192.168.91.128', isHealth=true}], thrift-rest-calculator: [ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8081], host='192.168.91.128', port=8081, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8082], host='192.168.91.128', port=8082, address='192.168.91.128', isHealth=true}, ThriftServerNode{node='node1', serviceId='thrift-rest-calculator', tags=[thrift-rest-calculator-8083], host='192.168.91.128', port=8083, address='192.168.91.128', isHealth=true}]]
Copy the code
Accessing the local Thrift client:
Access to the address | Parameter arg1 | Parameter arg2 | Page output result |
---|---|---|---|
/rpc/add | 200 | 100 | 300 |
/rpc/subtract | 200 | 100 | 100 |
/rpc/multiply | 200 | 100 | 20000 |
/rpc/division | 200 | 100 | 2 |
conclusion
This article briefly introduces how to use starter to integrate Apache Thrift into Spring Cloud. For more complex application scenarios and the internal design and implementation principle of starter, detailed introduction will be given step by step.
Welcome to pay attention to the technical public number: Zero one Technology Stack
This account will continue to share learning materials and articles on back-end technologies, including virtual machine basics, multithreaded programming, high-performance frameworks, asynchronous, caching and messaging middleware, distributed and microservices, architecture learning and progression.