Previous article we introduced the Eureka Service registry, this article describes how to use the Eureka Service registry to build a simple service-side registration service, the client to invoke the service use case.
There are three roles in the case: the Service registry, the service provider, the service consumer, the service registry is our previous Eureka stand-alone launch, the process is to start the registry, service provider production services and registered in the service center, consumers from the service center to obtain services and execution.
Services provided
We assume that the service provider has a Hello method that can provide the service of the output "Hello Xxx,this is first Messge" based on the parameters passed in
1, POM package configuration
To create a Springboot project, add the following configuration to the 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 files
The application.properties configuration is as follows:
spring.application.name=spring-cloud-producerserver.port=9000 Eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
The parameters have been explained in the previous article, and there is not much to say here.
3. Startup class
To add annotations to the startup class @EnableDiscoveryClient
@SpringBootApplication @enablediscoveryclient Public class producerapplication { publicstaticvoid 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"; }}
@EnableDiscoveryClient
When you add annotations, the project has the functionality to register the service. Once the project is started, you can see the Spring-cloud-producer service on the Registration Center page.
The service provider configuration is complete.
Service Call 1, POM package configuration
Consistent with service providers
<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 files
The application.properties configuration is as follows:
spring.application.name=spring-cloud-consumerserver.port=9001 Eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3. Startup class
Start class additions @EnableDiscoveryClient
and @EnableFeignClients
annotations.
@SpringBootApplication @enablediscoveryclient@enablefeignclients Public class consumerapplication { publicstaticvoid main (string[] args) { Springapplication.run (consumerapplication. class , args); } }
@EnableDiscoveryClient
: Enable service registration and discovery
@EnableFeignClients
: Enable feign for remote invocation
Feign is a declarative Web service client. Using feign can make it easier to write a Web service client by defining an interface and then adding annotations to it, as well as supporting annotations of the JAX-RS standard. The feign also supports pluggable encoders and decoders. Spring Cloud encapsulates the feign, enabling it to support Spring MVC standard annotations and httpmessageconverters. The feign can be used in combination with the Eureka and ribbon to support load balancing.
4. Feign Call Implementation
@FeignClient (name= "Spring-cloud-producer")publicinterface helloremote { = "/hello") public string Hello (@RequestParam (value = "Name") string name);}
- Name: remote service name, and Spring.application.name configuration
The methods in this class and the method names and parameters in Contoller in the remote service need to be consistent.
5. Web layer Invoke remote service
Inject the helloremote into the controller layer and invoke 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);} }
In this case, the simplest example of a service registration and invocation is complete.
Test a simple call
Start Spring-cloud-eureka, Spring-cloud-producer, spring-cloud-consumer three items in turn
First Enter: http://localhost:9000/hello?name=neo
check whether the Spring-cloud-producer service is normal
Return:hello neo,this is first messge
Description Spring-cloud-producer Normal start, the service provided is also normal.
In the browser, enter:http://localhost:9001/hello/neo
Return:hello neo,this is first messge
Indicates that the client has successfully invoked the remote service hello via feign and returned the result to the browser.
Load Balancing
With the above Spring-cloud-producer as an example of modification, the controller changes as follows:
@RestController Public class Hellocontroller { @RequestMapping ("/hello") public String Index (@ Requestparam String name) { return "Hello" +name+ ", this is producer 2 send first Messge" ; }}
To change the port in the configuration file:
spring.application.name=spring-cloud-producerserver.port=9003eureka.client.serviceUrl.defaultZone =http://localhost:8000/eureka/
After the package is started, two service providers will be found in Eureka, such as:
Then enter again in the browser: http://localhost:9001/hello/neo
to test:
Return results for the first time:hello neo,this is first messge
Second return result:hello neo,this is producer 2 send first messge
Continuous testing will find two results alternating, indicating that the two service centers automatically provide a service load balancing function. If we increase the number of service providers to N, the test results are the same, and the request is automatically polled to each server for processing. Source Source technical support for complete projects 2147775633
Springcloud Spring Boot mybatis distributed micro-service Cloud Architecture (III): service delivery and invocation