Spring Cloud (i): Service Registration and discovery

Source: Internet
Author: User

What is Spring cloud?

Spring Cloud is an ordered collection of frames. It uses spring boot's development convenience to subtly simplify the development of distributed system infrastructures, such as service discovery registration, Configuration center, message bus, load balancer, circuit breakers, data monitoring, and so on, all of which can be started and deployed with spring boot development style. Spring does not reinvent the wheel, it simply combines the more mature and proven service frameworks currently developed by companies, masking the complexities of configuration and implementation through the spring boot style, and ultimately leaving developers with a set of easy-to-understand, Easy-to-deploy and maintainable Distributed system Development Kit.

MicroServices are service units that can be deployed independently, horizontally scaled, independently accessible (or have a separate database), Springcloud is the butler of these microservices, and with the micro-service architecture, the number of projects can be very large, and Springcloud as a big housekeeper needs to manage these microservices, Nature needs a lot of little brother to help.

What's the relationship with spring Boot?

Spring boot is a set of spring's fast-provisioning scaffolding that enables rapid development of a single microservices based on spring boot, and Spring cloud is a spring boot-based cloud application development tool; Spring boot focuses on fast, A single individual that is easy to integrate, Spring Cloud is a global service governance framework; Spring boot uses a default greater than the configuration concept, many integration options have been chosen for you, can not be configured without configuration, a large part of Spring cloud is based on spring Boot to implement, can not be based on spring boot? No.

Spring boot can leave spring cloud to use the development project independently, but spring cloud is dependent on spring boot.

Spring, Spring booot > Spring Cloud.

The benefits of Spring cloud

Micro-service Framework so many such as: Dubbo, Kubernetes, why should you use Spring cloud?

    • Spring in the production of the family, spring in the enterprise-level development framework of no one can rival, a big, can guarantee the subsequent update, perfect. For example, Dubbo is almost dead now.
    • Spring boot this independent gan can save a lot of things, big and small live spring boot are doing quite well.
    • As a micro-service governance of the big guy, considered very comprehensive, almost all aspects of service governance are taken into account, easy to develop out of the box.
    • Spring Cloud is highly active, the tutorials are rich, and it's easy to find a solution when you encounter problems
    • A few lines of code to complete the fuse, the balance of responsibility, service center of the various platform functions

Spring Cloud is a boon for small and medium-sized internet companies, which often have little or no money to develop their own distributed system infrastructure, and the spring cloud one-stop solution can dramatically reduce development costs while managing business growth. At the same time, with the popularity of the microservices architecture and Docker container concept in recent years, Spring Cloud will have a niche in the future of "cloud" software development style, especially in the current variety of distributed solutions to provide a standardized, full-site technology solutions, The meaning may be comparable to the birth of the current servlet specification, and effectively advance the technical level of software system in the server.

Spring Cloud Eureka

We use Spring Cloud Eureka to implement service governance.

Create a Service registration center

Create a basic spring boot project, named Springboot-register, and pom.xml introduce the required dependencies in:

1<dependencies>2<dependency>3<groupId>org.springframework.boot</groupId>4<artifactId>spring-boot-starter-web</artifactId>5</dependency>6 7<dependency>8<groupId>org.springframework.cloud</groupId>9<artifactId>spring-cloud-starter-eureka-server</artifactId>Ten</dependency> One<dependency> A<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-test</artifactId> -<scope>test</scope> the</dependency> -</dependencies> -<dependencyManagement> -<dependencies> +<dependency> -<groupId>org.springframework.cloud</groupId> +<artifactId>spring-cloud-dependencies</artifactId> A<version>Brixton.SR5</version> at<type>pom</type> -<scope>Import</scope> -</dependency> -</dependencies> -</dependencyManagement>

@EnableEurekaServerStart a service registry with annotations to make conversations with other apps:

@EnableEurekaServer @springbootapplication  Public class Applicationregister {    publicstaticvoid  main (string[] args) {        Springapplication.run (Applicationregister. class , args);}    }

application.propertiesAdd the following information in the configuration file:

server.port=1111eureka.server.enable-self-preservation=falseEureka.instance.hostname =Localhosteureka.client.register-with-eureka=falseEureka.client.fetch-registry =falseeureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:$ {server.port}/eureka/

Visit: http://localhost:1111/,:

There are no services on the page!

Sign up for "service provider"

Create a basic spring boot app. Named spring boot-client , in pom.xml , add the following configuration:

1<dependency>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-web</artifactId>4</dependency>5 6<dependency>7<groupId>org.springframework.cloud</groupId>8<artifactId>spring-cloud-starter-eureka-server</artifactId>9</dependency>Ten<dependency> One<groupId>org.springframework.boot</groupId> A<artifactId>spring-boot-starter-test</artifactId> -<scope>test</scope> -</dependency> the</dependencies> -<dependencyManagement> -<dependencies> -<dependency> +<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-dependencies</artifactId> +<version>Brixton.SR5</version> A<type>pom</type> at<scope>Import</scope> -</dependency> -</dependencies> -</dependencyManagement>

Write a controller:

1 @RestController2  Public classHellocontroller {3     Private FinalLogger Logger =Logger.getlogger (GetClass ());4 5 @Autowired6     Privatediscoveryclient client;7 8@RequestMapping (value = "/hello", method =requestmethod.get)9      PublicString index ()throwsexception{Ten         One         return"Hello World"; A     } -}

Finally, the output of the service information in the controller is realized by adding annotations in the main class of the application @EnableDiscoveryClient :

 1   @EnableDiscoveryClient  2   @SpringBootApplication  3  public   Class   applicationclient { 4  5  public  static  void   main (string[] args) { 6  springapplication.run (applicationclient. Class   7  }  8  9 } 

Add the following information in the Application.properties configuration file:

server.port=2224spring.application.name=hello-serviceeureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/

After starting the project, visit again: http://localhost:1111/,

We can also access: Http://localhost:2224/hello by direct access to the/hello interface provided by the spring Boot-client service, with the following output:

So our service registration and discovery is complete.

Spring Cloud (i): Service Registration and discovery

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.