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.
Micro-Service Architecture
The microservices architecture is about a complete application that is split vertically from the data store into multiple different services, each of which can be deployed independently, independently maintained, independently scaled, and invoked between services and services through restful APIs.
Service governance
Because spring Cloud provides a layer of abstraction for service governance, there are a number of different service governance frameworks that can be supported in spring cloud applications, such as Netfix Eureka, Consul, and Zookeeper. With the Spring cloud service Governance Abstraction layer, we can seamlessly switch service governance implementations without affecting any other logic such as service registration, service discovery, service invocation, and so on.
Spring Cloud Eureka
Spring Cloud Eureka is the Service governance module under the spring Cloud Netflix project. The spring cloud Netflix project is one of Spring cloud's sub-projects, primarily packaged with 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 breakers (Hystrix), intelligent routing (Zuul), Client load Balancing (Ribbon), etc.
Create a Service registration center
Create a Spring boot project, named Com.david.eureka, and introduce the required dependencies in the Pom.xml
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.david.eureka</groupId> <artifactid>eureka</artifactid > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka</ Name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties& Gt <dependencies> <!--Introduction Eureka-server---<dependency> <GROUPID>ORG.SPRINGFR Amework.cloud</groupid> <artifactId>spring-cloud-starter-eureka-server</artifactId> &L T;/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </depend encies> <dependencyManagement> <dependencies> <dependency> <gr Oupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifact Id> <version>Edgware.SR2</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> <BUILD&G T <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
Start a service registry with @enableeurekaserver annotations to provide conversations to other apps.
@EnableEurekaServer @springbootapplication Public class eurekaapplication { publicstaticvoid main (string[] args) { Springapplication.run (eurekaapplication. class , args);} }
Under the default setting, the service registry will also attempt to register itself as a client, so we need to disable its client registration behavior by simply configuring the following information in APPLICATION.YML:
Spring: application: name:david-eureka-serverserver: port:8761Eureka: Instance: 127.0.0.1 client: Registerfalse fetch False
Set yourself up as a server by setting Registerwitheureka:false and Fetchregistry:false
Start Engineering, visit http://localhost:8761
No instances available no service was found, because there is no registration service, so there is no service here.
Create a service Provider
Create a spring boot app, name com.david.client, and register yourself with the service registry. Pom.xml as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Com.david</groupId> <Artifactid>Client</Artifactid> <version>0.0.1-snapshot</version> <Packaging>Jar</Packaging> <name>Client</name> <Description>Demo Project for Spring Boot</Description> <Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>1.5.9.RELEASE</version> <RelativePath/> <!--Lookup parent from repository - </Parent> <Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </Properties> <Dependencies> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-starter-eureka</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter</Artifactid> </Dependency> </Dependencies> <dependencymanagement> <Dependencies> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-dependencies</Artifactid> <version>Edgware.sr2</version> <type>Pom</type> <Scope>Import</Scope> </Dependency> </Dependencies> </dependencymanagement> <Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>
New TestController
PackageCom.david.client.controller;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classTestController {@Value ("${server.port}") String Port; @GetMapping ("/test") PublicString Test () {return"Test;port:" +Port; }}
By @enableeurekaclient to explain himself to be a eurekaclient
@EnableEurekaClient @springbootapplication Public class clientapplication { publicstaticvoid main (string[] args) { Springapplication.run (clientapplication. class , args);} }
You will also need to specify the address of your service registry in the configuration file, as configured in APPLICATION.YML:
Spring: application: name:david-eureka-clientserver: 8762Eureka: client: service-URL: #指定服务中心地址 defaultzone:http://localhost:8761/eureka/
Spring.application.name indicates the service name, and subsequent calls between services are based on this name.
Start the project and visit http://localhost:8761/again to see that our defined service has been successfully registered.
Http://192.168.20.114:8762/test
Of course we also have direct access to the test excuse provided by the David-eureka-client service, with access only: Http://localhost:8762/test
Spring Cloud (1) registration and discovery of services (Eureka)