springcloud (3): service provision and invocation

In the previous article, we introduced the construction of the eureka service registry. This article introduces how to use the eureka service registry to build a simple server-side registration service, and the client to call the service use case.

There are three roles in the case: service registration center, service provider, and service consumer. The service registration center is the eureka stand-alone version of our previous article. The process is to start the registration center first, and the service provider produces the service and registers it to In the service center, consumers obtain services from the service center and execute them.

service provision

We assume that the service provider has a hello method that can provide a service that outputs "hello xxx, this is first messge" according to the incoming parameters

1, pom package configuration

Create a springboot project and add the following configuration to pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Configuration file

application.properties is configured as follows:

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

The parameters have been explained in the previous article, so I won't say much here.

3. Startup class

@EnableDiscoveryClientAdd annotation to startup class

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

4、controller

Provide hello service

@RestController
 public  class HelloController {
    
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }
}

After adding @EnableDiscoveryClientannotations, the project has the function of service registration. After starting the project, you can see the SPRING-CLOUD-PRODUCER service on the registration center page.

At this point, the service provider configuration is complete.

service call

1, pom package configuration

consistent with service provider

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2. Configuration file

application.properties is configured as follows:

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3. Startup class

Start class adding @EnableDiscoveryClientand @EnableFeignClientsannotation.

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

}
  • @EnableDiscoveryClient: Enable service registration and discovery
  • @EnableFeignClients: enable feign to make remote calls

Feign is a declarative web service client. Using Feign can make it easier to write a Web Service client. Its use method is to define an interface, and then add annotations to it. It also supports JAX-RS standard annotations. Feign also supports pluggable encoders and decoders. Spring Cloud encapsulates Feign to support Spring MVC standard annotations and HttpMessageConverters. Feign can be combined with Eureka and Ribbon to support load balancing.

4. Feign call implementation

@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}
  • name: The name of the remote service, and the name of the spring.application.name configuration

The methods in this class must be consistent with the method names and parameters in the controller in the remote service.

5. The web layer calls remote services

Inject HelloRemote into the controller layer and call it like a normal method.

@RestController
 public  class ConsumerController {

    @Autowired
    HelloRemote HelloRemote;
    
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return HelloRemote.hello(name);
    }

}

At this point, the simplest example of service registration and invocation is completed.

test

simple call

Start the three projects spring-cloud-eureka, spring-cloud-producer, and spring-cloud-consumer in turn

Enter first: http://localhost:9000/hello?name=neoCheck whether the spring-cloud-producer service is normal

return:hello neo,this is first messge

It means that spring-cloud-producer starts normally and the services it provides are also normal.

Enter in the browser:http://localhost:9001/hello/neo

return:hello neo,this is first messge

It means that the client has successfully called the remote service hello through feign and returned the result to the browser.

load balancing

Taking the above spring-cloud-producer as an example, modify the controller as follows:

@RestController
 public  class HelloController {
    
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is producer 2  send first messge";
    }
}

Change the port in the configuration file:

spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

After the package is started, two service providers will be found in eureka, as shown below:

Then enter again in the browser: http://localhost:9001/hello/neoto test:

The first return result:hello neo,this is first messge

The second time returns the result:hello neo,this is producer 2 send first messge

After continuous testing, two kinds of results will appear alternately, indicating that the two service centers automatically provide the function of balancing the service load. If we increase the number of service providers to N, the test results are the same, and the request will be automatically polled to each server for processing.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324884755&siteId=291194637