Spring Cloud server-side registration and client invocation

Source: Internet
Author: User

Spring Cloud server-side registration and client invocation

In the previous article, we have set up the Spring cloud service Registry Eureka, a chapter that explains how to register a service with Eureka and how the client invokes the service.

First, registration services

First, the Eureka Client is introduced into the project, and the following configuration is added to the pom.xml:

<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud< /groupid><artifactid>spring-cloud-dependencies</artifactid><version>edgware.sr3</ Version><type>pom</type><scope>import</scope></dependency></dependencies ></dependencyManagement><dependencies><dependency><groupId> org.springframework.cloud</groupid><artifactid>spring-cloud-starter-netflix-eureka-client</ Artifactid></dependency></dependencies>

Then configure the address of the Eureka Registry in our Application.properties as follows:

eureka.client.service-url.defaultzone=http://localhost:8761/eureka/

Http://localhost:8761/eureka/is the default address and you can make changes. On the program we develop normally, using spring MVC. As follows:

@SpringBootApplication @restcontroller
@RequestMapping ("Demo") public class Application { @RequestMapping ("Home") public String Home () { return " Hello world "; } public static void Main (string[] args) { new Springapplicationbuilder (Application.class). Web (True). Run (args); }

This way,/demo/home will be registered with the Eureka Registration Center. Next we'll talk about how to call.

Second, use feign to call, Hystrix fuse

First we introduce the feign into the project and introduce Hystrix together, which can be fused when the service is unavailable. Add the following configuration to the Pom.xml:

<dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId> Spring-cloud-starter-netflix-hystrix</artifactid></dependency><dependency>    <groupId >org.springframework.cloud</groupId>    <artifactid>spring-cloud-starter-feign</artifactid >
</dependency>

And then join in the application.properties.

Feign.hystrix.enabled=true

The fuse of the feign is turned on. Then add annotations to the main class

@SpringBootApplication @enablecircuitbreaker@enablefeignclientspublic class Springcloudclientapplication {    public static void Main (string[] args) {        springapplication.run (springcloudclientapplication.class, args);}    }

Next we write feign's calling interface, Itempservice, as follows:

@FeignClient (name = "Eureka-server", fallback = tempservice.class) public interface Itempservice {    @RequestMapping ( METHOD = Requestmethod.get, value = "/demo/home") public    String index ();

@FeignClient that this interface is a feignclient, where name points to the name of the service, in the previous service, we apply the name Eureka-server, we will name this service, Fallback is the class that is executed after the fuse, our fuse execution class is tempservice.

@RequestMapping point to the specific interface in the Eureka-server service, where we point to/demo/home so that we invoke the/demo/home of the remote service when we call the index method. But what should we do if the remote service is not available? This will use the Hystrix fuse.

We write the implementation class Tempservice for the Itempservice interface, as follows:

@Componentpublic class Tempservice implements Itempservice {    @Override public    String Index () {        return ' Index error ";    }}

This way, when the remote service/demo/home is unavailable, the index method is executed and the index error is returned.

Finally, we write the controller and complete the call, as follows:

@RestController @requestmapping ("feign") public class Tempcontroller {    @Autowired    private Itempservice Tempservice;    @RequestMapping ("call") public    String call () {        return Tempservice.index ();}    }

So our service invocation and service Registration example is finished, is not very simple, there is a problem, welcome to the comment area communication.

  

Spring Cloud server-side registration and client invocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.