Tars-java is compatible with the Spring Cloud system, users can integrate the Tars-java into Spring Cloud.
The following requirements should be met before operation.
JDK 1.8 or above.
If you want to use the discovery feature, you need an Eureka Server instance that has already run Spring Cloud. For details on how to start the configuration, please refer to the Spring Cloud related tutorial.
Here are the steps to create and publish a service:
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-spring-cloud-starter</artifactId>
<version>1.6.1</version>
</dependency>
@Servant
public interface HelloServant {
public String hello(int no, String name);
}
@TarsServant(name="HelloObj")
public class HelloServantImpl implements HelloServant {
@Override
public String hello(int no, String name) {
return String.format("hello no=%s, name=%s, time=%s", no, name, System.currentTimeMillis());
}
}
@SpringBootApplication
@EnableTarsConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
eureka:
client:
serviceUrl:
#Service registry address
defaultZone: http://localhost:8761/eureka/
#The configuration under this tab is unique to Tars-java
tars:
#Server configuration
server:
#Server port
port: 18601
#Application name, The specific meaning refers to the service naming chapter of tars_java_quickstart.md
application: TestApp
#Server name, The specific meaning refers to the service naming chapter of tars_java_quickstart.md
server-name: HelloJavaServer
#Specify the service log path, whichever is the case
log-path: /usr/local/tars-eureka-test/bin/log
#Specify data file path, whichever is the case
data-path: /usr/local/tars-eureka-test/data
#Client configuration
client:
async-invoke-timeout: 10000
#Service discovery center address, generally the same as the registration center address, can be left blank
locator: http://localhost:8761/eureka/
TESTAPP.HELLOJAVASERVER is the service we just registered, where TESTAPP corresponds to the application name in the configuration file(attributes corresponding to tars.server.application), HELLOJAVASERVER corresponds to the server name in the configuration file(attributes corresponding to tars.server.server-name). The name registered on Eureka is the application name of the configuration file plus the service name, separated by a '.'. The service name displayed on the Eureka page is all uppercase, but it is actually the name filled in our configuration file. Eureka.
So far, a service has been developed.
Here's how to discover and access a service.
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-spring-cloud-starter</artifactId>
<version>1.6.1</version>
</dependency>
@Servant
public interface HelloPrx {
String hello(int no, String name);
String hello(int no, String name, @TarsContext java.util.Map<String, String> ctx);
void async_hello(@TarsCallback HelloPrxCallback callback, int no, String name);
void async_hello(@TarsCallback HelloPrxCallback callback, int no, String name, @TarsContext java.util.Map<String, String> ctx);
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
//use service...
}
}
eureka:
client:
serviceUrl:
#Service center address
defaultZone: http://localhost:8761/eureka/
#The client does not need to register with the spring cloud master
register-with-eureka: false
@Component
public class Client {
@TarsClient(name = "TestApp.HelloJavaServer.HelloObj")
private HelloPrx proxy;
}
proxy.hello(10, "hello");