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:

1234567891011121314151617181920212223242526272829 <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.4.release</version> <relativepath/> <!--lookup parent from repository to</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.

1234567891011121314 @RestController public class Dccontroller { @Autowireddiscoveryclient discoveryclient; @GetMapping ("/DC") Public String DC() {string services = "Services:" + discoveryclient.getservices ( ); SYSTEM.OUT.PRINTLN (services); return services;} }

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

123456789 @EnableDiscoveryClient @SpringBootApplication public class application {public static void main(string[] args) { /c13>new Springapplicationbuilder (computeserviceapplication.class). Web ( true). Run (args);}}

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

123 spring.application.name=eureka-clientserver.port=2001eureka.client.serviceurl.defaultzone=http://localhost:1001/eureka/

By using spring.application.name properties, we can specify that the name of the microservices will follow the call and only use that name to access the service. eureka.client.serviceUrl.defaultZoneproperty 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 server.port the properties to set different ports.

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

1 Services: [Eureka-client]

Among these, the square brackets are all the service manifests that are obtained in the implementation of the eureka-client Eureka through the interfaces defined by spring cloud DiscoveryClient . 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. SOURCE Source

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.