赞
踩
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解
采用Eureka作为服务注册与发现的组件
首先创建一个主Maven工程
创建完成后
在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom
- <?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.xuxu</groupId>
- <artifactId>springcloud01</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>pom</packaging>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.3.RELEASE</version>
- <relativePath/>
- </parent>
-
- <modules>
- <module>eureka-server</module>
- <module>eureka-client</module>
- </modules>
- <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>Finchley.RELEASE</spring-cloud.version>
- </properties>
-
- <dependencies>
- <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>
- </dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
工程上右键== >新建== >module工程
创建完成,pom.xml继承父pom文件,并引入spring-cloud-starter-netflix-eureka-server的依赖
- <?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.xuxu</groupId>
- <artifactId>eureka-server</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>eureka-server</name>
- <description>Demo project for Spring Boot</description>
-
- <parent>
- <groupId>com.xuxu</groupId>
- <artifactId>springcloud01</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
- </project>
在启动类上加上@EnableEurekaServer 注解
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaServerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
配置application.yml文件
- server:
- port: 8761
-
- eureka:
- instance:
- hostname: localhost
- status-page-url:
- client:
- registerWithEureka: false
- fetchRegistry: false
- serviceUrl:
- defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
-
- spring:
- application:
- name: eurka-server
eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件
上诉文件中 通过配置
registerWithEureka: false
fetchRegistry: false
表明是eureka server服务端
启动工程 输入http://localhost:8761
因为没有注册服务当然不可能有服务被发现了
工程上右键== >新建== >module工程
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
创建过程同server类似,创建完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.xuxu</groupId>
- <artifactId>eureka-client</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>eureka-client</name>
- <description>Demo project for Spring Boot</description>
-
- <parent>
- <groupId>com.xuxu</groupId>
- <artifactId>springcloud01</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
启动类上加上注解 @EnableEurekaClient
- @SpringBootApplication
- @EnableEurekaClient
- @RestController
- public class EurekaClientApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaClientApplication.class, args);
- }
-
- @Value("${server.port}")
- private String port;
-
- @RequestMapping(value = "/hi")
- public String hello(@RequestParam(value = "name",defaultValue = "xuxu") String name){
- return "hello"+name+":my port is "+port;
- }
- }
配置application.yml配置文件
- server:
- port: 8763
-
- spring:
- application:
- name: eureka-client
-
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动工程,打开http://localhost:8761 ,即eureka server 的网址
你会发现一个服务已经注册在服务中了,服务名为EUREKA-CLIENT ,端口为8762 同时访问接口
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。