Spring Cloud Building MicroServices Architecture (i) service registration and discovery

Source: Internet
Author: User

About Spring Cloud

Spring Cloud is a spring boot-based cloud application development tool for configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control buses, global locks, decision-making campaigns, Operations such as distributed sessions and cluster state management provide a simple way to develop.

Spring Cloud contains multiple sub-projects (for many different open source products involved in distributed systems), such as spring Cloud Config, Spring Cloud Netflix, Spring Cloud Cloudfoundry, Spring Cloud AWS, Spring Cloud Security, Spring Cloud Commons, Spring Cloud Zookeeper, Spring Cloud CLI, and more.

Micro-Service Architecture

The "MicroServices architecture" has been so hot in recent years that the product communities associated with microservices architectures have become more active (for example, Netflix, Dubbo), and Spring cloud is also being watched by architects and developers due to the strong visibility and influence of the spring community.

So what is a "microservices architecture"? To put it simply, a microservices architecture is to split a complete application from the data store vertically into multiple different services, each of which can be deployed independently, independently maintained, independently scaled, and invoked between services and services through, for example, restful APIs.

For the "micro-service Architecture", we can find a lot of relevant introduction and research articles on the Internet to learn and understand. You can also read the ancestor Martin Fowler's "MicroServices", this article does not do more introduction and description.

Service Registration and Discovery

After a brief introduction to Spring cloud and the microservices architecture, here's how to use spring Cloud to build a service registration and Discovery module.

We're going to use Spring cloud Netflix, one of Spring Cloud's sub-projects, mainly on the packaging of Netflix's range of open source products, which provides a self-configuring Netflix OSS integration for spring boot applications. With some simple annotations, developers can quickly configure common modules in the application and build large, distributed systems. It mainly provides modules including: Service Discovery (Eureka), Circuit breaker (hystrix), intelligent Path (Zuul), Client load Balancing (Ribbon), etc.

So, our core content here is the Service Discovery module: Eureka. Let's do some experimenting here.

Create a Service registration center

Create a basic spring boot project and pom.xml introduce the dependent content you need in:

<Parent>
<Groupid>org.springframework.boot</Groupid>
<Artifactid>spring-boot-starter-parent</Artifactid>
<Version>1.3.5.release</Version>
<Relativepath/><!--lookup parent from repository--
</Parent>

<Dependencies>
<Dependency>
<Groupid>org.springframework.boot</Groupid>
<Artifactid>spring-boot-starter-test</Artifactid>
<Scope>test</Scope>
</Dependency>

<Dependency>
<Groupid>org.springframework.cloud</Groupid>
<Artifactid>spring-cloud-starter-eureka-server</Artifactid>
</Dependency>
</Dependencies>

<Dependencymanagement>
<Dependencies>
<Dependency>
<Groupid>org.springframework.cloud</Groupid>
<artifactid>spring-cloud-dependencies</ARTIFACTID>
<version> Brixton.release</VERSION>
<type>pom</TYPE>
span class= "tag" ><scope>import</SCOPE>
Span class= "line" ></DEPENDENCY>
</DEPENDENCIES>
</DEPENDENCYMANAGEMENT>

Use the @EnableEurekaServer annotations to start a service registry for conversations with other apps. This step is very simple, just add this annotation to a normal spring boot application to enable this feature, such as the following example:

  
@EnableEure Kaserver
@SpringBootApplication
public class application {

public static void main (string[] args) {
new springapplicationbuilder (Application.class). Web ( true). Run (args);

/span>

Under the default setting, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by simply application.properties adding the following configuration in the Ask:

server.port=1111

Eureka.client.register-with-eureka=False
Eureka.client.fetch-registry=False
Eureka.client.serviceurl.defaultzone=http://localhost:${server.port}/eureka/

To differentiate the service from subsequent registrations, the port for the service registry is server.port set to 1111 .

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

You can see the following page, which has not found any services

Alt

The project can be found in: chapter9-1-1/eureka-server

Create a "service provider"

Below we create the client that provides the service, and register ourselves with the Service registration center.

Assuming we have a microservices module that provides computational functionality, we implement a restful API by passing in two parameters A and B, and finally returning the result of a + B.

First, create a basic spring boot application, in pom.xml which the following configuration is added:


<Parent>
<Groupid>org.springframework.boot</Groupid>
<Artifactid>spring-boot-starter-parent</Artifactid>
<Version>1.3.5.release</Version>
<Relativepath/><!--lookup parent from repository--
</Parent>

<Dependencies>
<Dependency>
<Groupid>org.springframework.boot</Groupid>
<Artifactid>spring-boot-starter-test</Artifactid>
<Scope>test</Scope>
</Dependency>

<Dependency>
<Groupid>org.springframework.cloud</Groupid>
<Artifactid>spring-cloud-starter-eureka</Artifactid>
</Dependency>
</Dependencies>

<Dependencymanagement>
<Dependencies>
<Dependency>
<Groupid>org.springframework.cloud</Groupid>
<artifactid>spring-cloud-dependencies</ARTIFACTID>
<version> Brixton.release</VERSION>
<type>pom</TYPE>
span class= "tag" ><scope>import</SCOPE>
Span class= "line" ></DEPENDENCY>
</DEPENDENCIES>
</DEPENDENCYMANAGEMENT>

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

@RestController
PublicClassComputecontroller {

PrivateFinal Logger Logger = Logger.getlogger (GetClass ());

@Autowired
private discoveryclient client;

@RequestMapping (value = public Integer add (@RequestParam integer A, @RequestParam integer b) {
Serviceinstance instance = Client.getlocalserviceinstance ();
Integer r = a + B;
Logger.info ( return r;
/span>

Finally in the main class by adding @EnableDiscoveryClient annotations, which can activate the discoveryclient implementation in Eureka, To implement the output of the service information in the controller.

  
@EnableDisc Overyclient
@SpringBootApplication
public class computeserviceapplication {
Public static void main ( String[] (args) {
new springapplicationbuilder ( Computeserviceapplication.class). Web (true). Run (args);
/span>

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

Spring.application.name=compute-service

server.port=2222

eureka.client.serviceurl.defaultzone=http://localhost:1111/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.

Once the project is started, visit: http://localhost:1111/

As you can see, our defined service is registered.

Alt

The project can be found in: Chapter9-1-1/compute-service

"Reprint Please specify source": http://blog.didispace.com/springcloud1/

Spring Cloud Building MicroServices Architecture (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.