Spring Cloud Learning: 服务注册
Posted 在旅途上追忆寻梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Learning: 服务注册相关的知识,希望对你有一定的参考价值。
官网https://projects.spring.io/spring-cloud/,spring cloud官网各组件版本为:
Component | Edgware.SR4 | Finchley.SR1 | Finchley.BUILD-SNAPSHOT |
---|---|---|---|
spring-cloud-aws | 1.2.3.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-cloud-bus | 1.3.3.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-cloud-cli | 1.4.1.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-cloud-commons | 1.3.4.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-contract | 1.2.5.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-config | 1.4.4.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-netflix | 1.4.5.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-security | 1.2.3.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-cloud-cloudfoundry | 1.1.2.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-cloud-consul | 1.3.4.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-sleuth | 1.3.4.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-stream | Ditmars.SR4 | Elmhurst.SR1 | Elmhurst.BUILD-SNAPSHOT |
spring-cloud-zookeeper | 1.2.2.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-boot | 1.5.14.RELEASE | 2.0.4.RELEASE | 2.0.4.BUILD-SNAPSHOT |
spring-cloud-task | 1.2.3.RELEASE | 2.0.0.RELEASE | 2.0.1.BUILD-SNAPSHOT |
spring-cloud-vault | 1.1.1.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-gateway | 1.0.2.RELEASE | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT |
spring-cloud-openfeign | 2.0.1.RELEASE | 2.0.2.BUILD-SNAPSHOT | |
spring-cloud-function | 1.0.0.RELEASE | 1.0.0.RELEASE | 1.0.1.BUILD-SNAPSHOT |
其中,在使用spring cloud时,需注意spring boot版本,Finchley构建并使用Spring Boot 2.0.x,预计不会与Spring Boot 1.5.x一起使用。
注意:Dalston发布将于2018年12月达到使用寿命.Eginware将遵循Spring Boot 1.5.x的生命周期结束周期。
Dalston和Edgware发布版基于Spring Boot 1.5.x构建,预计不会与Spring Boot 2.0.x一起使用。
一、注册与发现
Spring Cloud使用eureka, 所有需要访问配置文件的应用都作为一个erureka client注册上去。eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送请求,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。
maven依赖如下:
<?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.chm</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud</name> <description>Spring cloud eureka server project module</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.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>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <!--<artifactId>spring-cloud-starter-eureka-server</artifactId>--> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 使用里程碑版需要下面的配置(Spring官网上的配置更为简洁, 更适合每个版本 http://projects.spring.io/spring-boot/)。新版本新特性,如开发热部署 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
启动类为:
@EnableEurekaServer @SpringBootApplication public class SpringcloudEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudEurekaServerApplication.class, args); } }
配置文件为:
server: port: 9999 eureka: instance: hostname: 127.0.0.1 client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
结果报异常为:java.lang.IllegalStateException: Failed to load property source from location ‘classpath:/application.yml‘,需严格注意yml文件格式。
启动eureka server,然后访问http://localhost:9999,可看到Eureka启动界面。
客户端
maven依赖如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.chm</groupId> 7 <artifactId>springcloud</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>springcloud</name> 12 <description>Spring cloud eureka client project module</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <!--eureka server --> 30 <dependency> 31 <groupId>org.springframework.cloud</groupId> 32 <!--<artifactId>spring-cloud-starter-eureka-server</artifactId>--> 33 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-starter-web</artifactId> 38 </dependency> 39 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-test</artifactId> 43 <scope>test</scope> 44 </dependency> 45 </dependencies> 46 47 <dependencyManagement> 48 <dependencies> 49 <dependency> 50 <groupId>org.springframework.cloud</groupId> 51 <artifactId>spring-cloud-dependencies</artifactId> 52 <version>${spring-cloud.version}</version> 53 <type>pom</type> 54 <scope>import</scope> 55 </dependency> 56 </dependencies> 57 </dependencyManagement> 58 59 <build> 60 <plugins> 61 <plugin> 62 <groupId>org.springframework.boot</groupId> 63 <artifactId>spring-boot-maven-plugin</artifactId> 64 </plugin> 65 </plugins> 66 </build> 67 68 <!-- 使用里程碑版需要下面的配置(Spring官网上的配置更为简洁, 69 更适合每个版本 http://projects.spring.io/spring-boot/)。新版本新特性,如开发热部署 --> 70 <repositories> 71 <repository> 72 <id>spring-milestones</id> 73 <name>Spring Milestones</name> 74 <url>https://repo.spring.io/milestone</url> 75 <snapshots> 76 <enabled>false</enabled> 77 </snapshots> 78 </repository> 79 </repositories> 80 81 </project>
启动类为:
1 @SpringBootApplication 2 @EnableEurekaClient 3 @RestController 4 public class SpringcloudEurekaClientApplication { 5 6 public static void main(String[] args) { 7 SpringApplication.run(SpringcloudEurekaClientApplication.class, args); 8 } 9 10 @Value("${server.port}") 11 String port; 12 @RequestMapping("/") 13 public String home() { 14 return "hello world from port " + port; 15 } 16 17 }
配置文件为:
1 eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://localhost:9999/eureka/ 5 server: 6 port: 8888 7 spring: 8 application: 9 name: service-eurekaclient
访问即可发现服务已经注册。
以上是关于Spring Cloud Learning: 服务注册的主要内容,如果未能解决你的问题,请参考以下文章
整合spring cloud云服务架构 - Spring Cloud简介