Spring Cloud Building MicroServices Architecture (ii) Service consumers

Source: Internet
Author: User

Original source: http://blog.didispace.com/springcloud2/

In the previous article, "Spring Cloud Building MicroServices Architecture (a) service registration and discovery", we have successfully created a "service registry", Implementing and registering a "service provider: Compute-service". So how do we go about consuming the interface content of a service provider?

Ribbon

The Ribbon is a load balancer based on HTTP and TCP clients. The feign also uses the Ribbon, which is followed by an introduction to the use of feign.

The ribbon can poll for access in a ribbonserverlist server-side list configured through the client to achieve a balanced load.

When the ribbon is used in conjunction with Eureka, Ribbonserverlist is discoveryenabledniwsserverlist rewritten to get a list of the server from the Eureka Registry. It also replaces iping with niwsdiscoveryping, which delegates responsibilities to Eureka to determine whether the server has been started.

Let's take a look at how the Ribbon is used to invoke the service and achieve a balanced load on the client.

Preparatory work
    • Start the service registry in Chapter-9-1-1: Eureka-server
    • Start the service provider in Chapter-9-1-1: Compute-service
    • Modify the Server-port in Compute-service to 2223, and then start a service provider: Compute-service

Visit Now: http://localhost:1111/

Alt

You can see that the Compute-service service has two units running:

    • 192.168.21.101:compute-service:2222
    • 192.168.21.101:compute-service:2223
Consumer with ribbon for client load Balancing

Build a basic spring boot project and include the following in Pom.xml:

123456789101112131415161718192021222324252627282930313233343536373839 <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>brixton.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>

In the application main class, the @EnableDiscoveryClient Discovery service capability is added through annotations. Creates a Resttemplate instance and @LoadBalanced opens the load balancing capability with annotations.

12345678910111213141516  @s Pringbootapplication @EnableDiscoveryClient public class ribbonapplication {   @Bean @LoadBalanced resttemplate () { return new resttemplate (); }  public static void main (string[] args) { Springapplication.run (Ribbonapplication.class, args);}  }

Create ConsumerController COMPUTE-SERVICE An add service to consume. The service is invoked by direct resttemplate, and the value of 10 + 20 is computed.

123456789101112 @RestController public class Consumercontroller { @Autowiredresttemplate resttemplate; @RequestMapping (value = "/add", method = Requestmethod.get) Public String add() {return resttemplate.getforentity ("HTTP/ Compute-service/add?a=10&b=20 ", String.class). GetBody ();} }

application.propertiesConfiguring the Eureka Service Registry in

12345 spring.application.name=ribbon-consumerserver.port=3333 eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/

Launch the app and visit two times: Http://localhost:3333/add

Then, open the Compute-service's two service providers, output a log content similar to the following:

    • The port is 2222 the service-provider log:
1 2016-06-02 11:16:26.787 INFO 90014---[io-2222-exec-10] com.didispace.web.ComputeController:/add, host : 192.168.21.101, Service_id:compute-service, result:30
    • The port is 2223 the service-provider log:
1 2016-06-02 11:19:41.241 INFO 90122---[nio-2223-exec-1] com.didispace.web.ComputeController:/add, host : 192.168.21.101, Service_id:compute-service, result:30

As you can see, the two Compute-service service ports that were previously started were called once. Here we have already implemented a balanced load of service calls through the Ribbon at the client.

For a complete example, refer to: Chapter9-1-2/eureka-ribbon

Feign

Feign is a declarative Web service client that makes it easier to write Web Serivce clients. We only need to use feign to create an interface and configure it with annotations to be done. It features pluggable annotation support, including feign annotations and jax-rs annotations. The feign also supports pluggable encoders and decoders. Spring Cloud adds support for feign to spring MVC annotations, and integrates the Ribbon and Eureka to provide an HTTP client implementation that provides a balanced load.

Here is an example of how feign can easily declare the definition and invocation of the Computer-service service described above.

Create a Spring boot project, configuration pom.xml , to replace the Ribbon dependencies in the above configuration with feign dependencies, as follows:

123456789101112131415161718192021222324252627282930313233343536373839 <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-feign</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>brixton.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>

Use annotations to turn on the feign feature in the main class of the application @EnableFeignClients , as follows:

12345678910 @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class feignapplication { public static void main(string[] args) {springapplication.run (Feignapplication.class, args);} }

Define compute-servic the interface of the E service, as follows:

1234567 @FeignClient ("Compute-service") public Interface computeclient { @RequestMapping (method = requestmethod.get, value = "/add") integer Add(@RequestParam (value = "a") integer A, @requestparam(value = "B") Integer b ); }
    • Use @FeignClient("compute-service") annotations to bind the interface corresponding to the Compute-service service
    • Use spring MVC Annotations to configure specific implementations under the Compute-service service.

In the Web layer, call the definition above ComputeClient , as follows:

123456789101112 @RestController public class Consumercontroller { @Autowiredcomputeclient computeclient; @RequestMapping (value = "/add", method = Requestmethod.get)  Public Integer add() {return Computeclient.add (ten );} }

application.propertiesDo not change, specify the Eureka service registry, such as:

1234 spring.application.name=feign-consumerserver.port=3333 eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/

Launch the app, visit several times: Http://localhost:3333/add

Looking at the log again, you can get the same results as before using the Ribbon, which provides a balanced load on the service provider.

In this section we can easily bind the Compute-service service by feign in the way of interface and annotation configuration, which allows us to invoke it as a local service in the local application and to do the client-side load balancing.

Spring Cloud Building MicroServices Architecture (ii) Service consumers

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.