Introduction of Spring Cloud
Spring Cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-proxies, event busses, global locks, decision-making campaigns, distributed sessions, and more. It runs in a simple environment and can run on a developer's computer. In addition, Spring Cloud is based on springboot, so need to develop a certain understanding of springboot, if you do not understand the words can read this article: 2 hours to learn springboot. In addition to the "micro-service Architecture" Do not understand, you can search through the search engine "microservices architecture" under the understanding.
Third, create a service provider (Eureka client)
When the client registers with the server, it provides some metadata, such as the host and port, the URL, the home page, and so on. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is typically removed from the registration server.
The creation process is similar to the server, and the Pom.xml is created as follows:
<?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.forezp</groupid> <artifactId>service-hi</artifactId> <version>0.0.1-snapshot</ version> <packaging>jar</packaging> <name>service-hi</name> <description>demo PR Oject for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.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.ve rsion>1.8</java.version> </properties> <dependencies> <dependency> < Groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifact id> </dependency> <dependency> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-starter-web</artifactId> </dependency> <depende Ncy> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starte r-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupid>org . SPRINGFRAMEWORK.CLOUD</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version> ;D Alston. Rc1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> ; <plugin> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPR ing-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <repos itories> <repository> <id>spring-milestones</id> <name>spring Mile Stones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositorie s≫</project>
The annotated @enableeurekaclient shows that he is a eurekaclient.
@SpringBootApplication@EnableEurekaClient@RestControllerpublic class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi "+name+",i am from port:" +port; }}
Just @enableeurekaclient is not enough, you also need to specify the address of your service registry in the configuration file, the APPLICATION.YML configuration file is as follows:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8762spring: application: name: service-hi
It is important to specify the Spring.application.name, which is typically based on this name in the subsequent invocation of services and services.
To start the project, open http://localhost:8761, which is the Eureka server URL:
You will find a service already registered in the service, the service name is Service-hi, the port is 7862
When you open Http://localhost:8762/hi?name=forezp, you will see it on your browser:
Sources of information and source
Enterprise Springcloud Tutorial (i) Registration and discovery of services (Eureka)