Spring Cloud builds a microservices architecture-create a service provider

Source: Internet
Author: User

Below we create the client that provides the service, and register ourselves with the Service registration center. In this article we mainly introduce the registration and discovery of services, so we may try to provide an interface in the service provider to obtain all the current service information.

First, create a basic spring boot app. Named Eureka-client, in Pom.xml, add the following configuration:

  <parent><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-parent</artifactid><version>1.5.4.release</version><relativepath/> <!--lookup parent from repository--></parent><dependencies><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></dependencies><dependencymanagement> <dependencies><dependency><groupid>org.springframework.cloud</groupid><artifactid >spring-cloud-dependencies</artifactid><version>dalston.sr1</version><type>pom</ Type><scope>import</scope></dependency></dependencies></dependencymanagement> 

Second, the implementation of the/DC request processing interface, through the Discoveryclient object, in the log to print out the relevant content of the service instance.

@RestControllerpublic class DcController {@AutowiredDiscoveryClient discoveryClient;@GetMapping("/dc")public String dc() {String services = "Services: " + discoveryClient.getServices();System.out.println(services);return services;}}

Finally, by adding @enablediscoveryclient annotations to the main class, this annotation activates the discoveryclient implementation in the Eureka so that the output of the service information in the controller can be realized.

@EnableDiscoveryClient@SpringBootApplicationpublic class Application {public static void main(String[] args) {new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);}}

After we have completed the implementation of the service content, we will continue to do some configuration work for Application.properties, as follows:

Spring.application.name=eureka-client
server.port=2001
eureka.client.serviceurl.defaultzone=http://localhost:1001/eureka/
With the Spring.application.name property, we can specify that the name of the microservices will follow the call and only use that name to access the service. The Eureka.client.serviceUrl.defaultZone property corresponds to the configuration content of the service registry, specifying the location of the service registry. In order to test differentiated service providers and service registries on this computer, use the Server.port property to set different ports.

Of course, we can also get the current list of services by directly accessing the/DC interface provided by the Eureka-client service

Services: [Eureka-client]
Among these, the eureka-client in square brackets is the list of all services that are obtained in the implementation of Eureka through the Discoveryclient interface defined by spring cloud. Since spring cloud has done a very good job of abstraction in the service discovery layer, for the above program, we can seamlessly switch from Eureka's service governance system to Consul's service governance system Central.

From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects.

Spring Cloud builds a microservices architecture-create a service provider

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.