1. Introduction to Spring Cloud
Spring Cloud is a microservice architecture development tool based on SpringBoot implementation. It provides a simple development method for the operations involved in the microservice architecture such as configuration management, service governance, circuit breakers, intelligent routing, micro-agents, control bus, global locks, decision-making campaigns, distributed sessions and cluster state management.
Spring Cloud includes multiple subprojects (for multiple different open source products involved in distributed systems, and may also be added), as described below.
Spring Cloud Config: Configuration Management Tools, Spring Cloud Netflix: Core Components, Spring Cloud Bus: Events, Message Bus, and more.
2. Spring Cloud Eureka
Spring Cloud Eureka is part of the Spring Cloud Netflix microservice suite. It is secondary encapsulated based on NetflixEureka, which is mainly responsible for completing service governance functions in the microservice architecture. Spring Cloud adds Spring Boot-style automated configurations to Eureka, we can easily integrate microservice applications built by Spring Boot with the Eureka service governance system by simply introducing dependencies and annotation configurations.
Service governance can be said to be the most core and basic module in the microservice architecture. It is mainly used to realize the automated registration and discovery of various microservice instances.
III. Example
(1) Tool: IntelliJ IDEA
(2) Create a new empty project
(3) Create a new Eureka Server, Module, named: eurekaserver
Right-click on the project -> Create Module -> Select springinitialir -> Fill in the project name, and next step ->, select Eureka Server as shown in the figure:
(3-1) pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
(3-2) application.yml
server: port: 5555 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ Note: eureka.client.register-with-eureka: Since this application is a registry, it is set to false, which means that you do not register yourself with the registry.
eureka.client.fetch-registry: Since the registry's responsibility is to maintain service instances, it does not need to retrieve services, so it is also set to false.
(3-3) Entrance Class
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }(3-4) Start the test
After completing the above configuration, start the application and visit http://localhost:5555/. You can see the Eureka information panel shown in the figure below. The Instances currently registered with Eureka column is empty, indicating that the registration center has not registered any services.
(4) After the registration service provider has completed the establishment of the service registration center, we will try to add an existing Spring Boot application to Emeka's service governance system.
(5) Create a new Eureka Client, Module, named: helloserver, this helloserver is used as a child model of eurekaserver
(6) Reform the pom configuration of parent model and child model (6-1) eurekaserver's pom.xml configuration:
<packaging>pom</packaging> <modules> <module>../helloserver</module> </modules>
(6-2) All pom.xml of eurekaserver:
<?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.example</groupId> <artifactId>demoeurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>../hellserver</module> </modules> <name>eurekaserver</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> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencies> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>(6-3) Helloserver's pom.xml configuration:
<parent> <groupId>com.example</groupId> <artifactId>demoeurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../eurekaserver/pom.xml</relativePath> </parent>
(6-4) All pom.xml configurations of helloserver:
<?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> <parent> <groupId>com.example</groupId> <artifactId>demoeurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../eurekaserver/pom.xml</relativePath> </parent> <artifactId>helloserver</artifactId> <packaging>jar</packaging> <name>helloserver</name> <description>Demo project for Spring Boot</description> <properties> <start-class>com.example.helloserver.HelloserverApplication</start-class> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependency> </dependency> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependency> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>(6-5) Helloserver's application.yml configuration:
server: port: 5556 spring: application: name: helloserver eureka: client: serviceUrl: defaultZone: http://localhost:5555/eureka/
(6-6) Helloserver's startup class:
@EnableEurekaServer @SpringBootApplication @RestController public class HelloserverApplication { private final Logger log = (Logger) LoggerFactory.getLogger(HelloserverApplication.class); @Autowired private DiscoveryClient client; @RequestMapping(name = "/hello", method = RequestMethod.GET) public String index() { ServiceInstance instance = client.getLocalServiceInstance(); log.info("/hello, host:" + instance.getHost() + ",service_id:" + instance.getServiceId()); return "Hello SpringCloud~"; } public static void main(String[] args) { SpringApplication.run(HelloserverApplication.class, args); } }(7) Start eurekaserver and helloserver respectively, and test:
(7-1) Visit eurekaserver: (You can clearly see the HELLOSERVER information)
(7-2) Visit helloserver:
(7-3) Check the helloserver console information:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.