RestTemplate is a RESTful API provide by the Spring framework. ServiceComb provides the API for service calling. Users can call microservices using customized URL and RestTemplate instance provided by ServiceComb regardless of the specific address of the service.
- The URL must be in format of ServiceComb:
cse://microserviceName/path?querystring. - During use of this URL format, the ServiceComb framework will perform internal microservice descovery, fallbreak, and fault tolerance and send the requests to the microservice providers.
-
Start the ServiceComb/Service Center
- how to start the service center
- make sure service center address is configured correctly in
microservice.yamlfile
cse: service: registry: address: http://127.0.0.1:30100 #service center address
-
Start the springmvc-provider service
-
Start provider service by maven
Compile the source code at root directory of ServiceComb Java Chassis, which is
servicecomb-java-chassis/, and usemvn execto execute the main classSpringmvcProviderMain.cd servicecomb-java-chassis/ mvn clean install -Psamples -DskipTests #Only need to install at first time cd samples/springmvc-sample/springmvc-provider/ mvn exec:java -Dexec.mainClass="org.apache.servicecomb.samples.springmvc.provider.SpringmvcProviderMain"
-
Start provider service by IDE
Import the project by InteliJ IDEA or Eclipse, add sample module to pom.xml file in root module
servicecomb-java-chassis/pom.xml, and add<module>samples</module>to<modules></modules>block, Then findmainfunction of provider service andRUNit like any other Java Program.
-
-
Start the springmvc-consumer service
Just like how to start springmvc-provider service. But the main class of springmvc-consumer service is
SpringmvcConsumerMain.cd samples/springmvc-sample/springmvc-consumer/ mvn exec:java -Dexec.mainClass="org.apache.servicecomb.samples.springmvc.consumer.SpringmvcConsumerMain"
-
How to verify On the producer side, the output should contain the following stuffs if the producer starts up successfully:
- 'swagger: 2.0 info: version: 1.0.0 ...' means the producer generated swagger contracts
- 'rest listen success. address=0.0.0.0:8080' means the rest endpoint is listening on port 8080
- 'highway listen success. address=0.0.0.0:7070' means the highway endpoint is listening on port 7070
- 'Register microservice instance success' means the producer has registered successfully to service center
On the consumer side, you can see the following outputs if the consumer can invoke the producer:
- 'RestTemplate consumer sayhi services: Hello Java Chassis' means the consumer calls sayhi by RestTemplate successfully
- 'RestTemplate consumer sayhello services: Hello person ServiceComb/Java Chassis' means the consumer calls sayhello by RestTemplate successfully
- 'POJO consumer sayhi services: Hello Java Chassis' means the consumer calls sayhi by RpcReference successfully
- 'POJO consumer sayhello services: Hello person ServiceComb/Java Chassis' means the consumer calls sayhello by RpcReference successfully