SpringCloud-Eureka:服务的注册与发现

Posted grasp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud-Eureka:服务的注册与发现相关的知识,希望对你有一定的参考价值。

    SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。

 创建一个空的maven工程,然后把注册中心和服务提供者作为两个模块方便测试

搭建空的maven工程

技术分享图片

创建完成后界面如下:

技术分享图片

 

 

一、搭建服务注册中心服务注册中心 (eureka-server):

  1.构建一个maven模块项目

   创建工程:

技术分享图片

 

技术分享图片

技术分享图片

技术分享图片

也可以不选择。看个人喜好。

创建完成后窗体如下:

技术分享图片

 

注意要修改springboot的版本号,因为如果版本太高可能回和eureka冲突,而引起各种错误

修改eureka-server模块的pom.xml文件:

 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.xuan</groupId>
 7     <artifactId>eureka-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>eureka-server</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.2.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     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.cloud</groupId>
30             <artifactId>spring-cloud-starter-eureka-server</artifactId>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-test</artifactId>
36             <scope>test</scope>
37         </dependency>
38     </dependencies>
39     <dependencyManagement>
40         <dependencies>
41             <dependency>
42                 <groupId>org.springframework.cloud</groupId>
43                 <artifactId>spring-cloud-dependencies</artifactId>
44                 <version>Dalston.SR3</version>
45                 <type>pom</type>
46                 <scope>import</scope>
47             </dependency>
48         </dependencies>
49     </dependencyManagement>
50 
51     <build>
52         <plugins>
53             <plugin>
54                 <groupId>org.springframework.boot</groupId>
55                 <artifactId>spring-boot-maven-plugin</artifactId>
56             </plugin>
57         </plugins>
58     </build>
59 
60     <repositories>
61         <repository>
62             <id>spring-milestones</id>
63             <name>Spring Milestones</name>
64             <url>https://repo.spring.io/milestone</url>
65             <snapshots>
66                 <enabled>false</enabled>
67             </snapshots>
68         </repository>
69         <repository>
70             <id>repository.springframework.maven.release</id>
71             <name>Spring Framework Maven Release Repository</name>
72             <url>http://maven.springframework.org/milestone/</url>
73         </repository>
74         <repository>
75             <id>org.springframework</id>
76             <url>http://maven.springframework.org/snapshot</url>
77         </repository>
78         <repository>
79             <id>spring-milestone</id>
80             <name>Spring Maven MILESTONE Repository</name>
81             <url>http://repo.spring.io/libs-milestone</url>
82         </repository>
83         <repository>
84             <id>spring-release</id>
85             <name>Spring Maven RELEASE Repository</name>
86             <url>http://repo.spring.io/libs-release</url>
87         </repository>
88     </repositories>
89 </project>

注意

1.spring-cloud-dependencies的版本号

2.仓库地址,我用阿里的地址报错,还不清楚是什么原因

 

想要实现一个服务注册中心的功能非常简单,只需要在项目的启动类EurekaServerApplication上使用@EnableEurekaServer注解即可

修改EurekaServerApplication.java,增加@EnableEurekaServer注解:

 1 package com.xuan.eurekaserver;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @SpringBootApplication
 8 @EnableEurekaServer
 9 public class EurekaServerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(EurekaServerApplication.class, args);
13     }
14 }

修改application.properties文件,设置相关属性

1 server.port=8080
2 eureka.instance.hostname=localhost
3 #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
4 eureka.client.register-with-eureka=false
5 #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
6 eureka.client.fetch-registry=false
7 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

运行EurekaServerApplication启动服务

技术分享图片

 

二、服务注册与服务发现---注册服务提供者(eureka-client

1.新建maven模块项目,创建后目录如下:

技术分享图片

 

修改eureka-client模块的pom.xml文件:

 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.xuan</groupId>
 7     <artifactId>eureka-client</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>eureka-client</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.2.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     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.cloud</groupId>
30             <artifactId>spring-cloud-starter-eureka</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-web</artifactId>
35         </dependency>
36         <dependency>
37             <groupId>org.springframework.boot</groupId>
38             <artifactId>spring-boot-starter-test</artifactId>
39             <scope>test</scope>
40         </dependency>
41     </dependencies>
42     <dependencyManagement>
43         <dependencies>
44             <dependency>
45                 <groupId>org.springframework.cloud</groupId>
46                 <artifactId>spring-cloud-dependencies</artifactId>
47                 <version>Dalston.SR3</version>
48                 <type>pom</type>
49                 <scope>import</scope>
50             </dependency>
51         </dependencies>
52     </dependencyManagement>
53 
54     <build>
55         <plugins>
56             <plugin>
57                 <groupId>org.springframework.boot</groupId>
58                 <artifactId>spring-boot-maven-plugin</artifactId>
59             </plugin>
60         </plugins>
61     </build>
62 
63     <repositories>
64         <repository>
65             <id>spring-milestones</id>
66             <name>Spring Milestones</name>
67             <url>https://repo.spring.io/milestone</url>
68             <snapshots>
69                 <enabled>false</enabled>
70             </snapshots>
71         </repository>
72         <repository>
73             <id>repository.springframework.maven.release</id>
74             <name>Spring Framework Maven Release Repository</name>
75             <url>http://maven.springframework.org/milestone/</url>
76         </repository>
77         <repository>
78             <id>org.springframework</id>
79             <url>http://maven.springframework.org/snapshot</url>
80         </repository>
81         <repository>
82             <id>spring-milestone</id>
83             <name>Spring Maven MILESTONE Repository</name>
84             <url>http://repo.spring.io/libs-milestone</url>
85         </repository>
86         <repository>
87             <id>spring-release</id>
88             <name>Spring Maven RELEASE Repository</name>
89             <url>http://repo.spring.io/libs-release</url>
90         </repository>
91     </repositories>
92 </project>

想要实现一个服务提供方也很简单,只要在项目的启动类EurekaClientApplication上使用@EnableEurekaClient注解即可

修改EurekaClientApplication.java,增加@EnableEurekaClient注解:

 1 package com.xuan.eurekaclient;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 
 7 @SpringBootApplication
 8 @EnableEurekaClient
 9 public class EurekaClientApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(EurekaClientApplication.class, args);
13     }
14 }

增加HelloController.java,实现一个restful接口:

 1 package com.xuan.eurekaclient;
 2 
 3 import org.apache.log4j.Logger;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.cloud.client.ServiceInstance;
 6 import org.springframework.cloud.client.discovery.DiscoveryClient;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 @RestController
12 public class HelloController {
13     private final Logger logger = Logger.getLogger(getClass());
14 
15     @Autowired
16     private DiscoveryClient client;
17 
18     @RequestMapping(value = "hello", method = RequestMethod.GET)
19     public String index() {
20         ServiceInstance instance = client.getLocalServiceInstance();
21         logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
22         return "Hello World";
23     }
24 }

在application.properties中进行如下配置

1 spring.application.name=eureka-client
2 server.port=8090
3 eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/

spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。

eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。

server.port属性设置不同的端口。

设置完成后目录如下:

技术分享图片

 

先启动 EurekaServerApplication

在启动EurekaClientApplication

得到如下结果:

技术分享图片

 

 

在浏览器中输入http://localhost:8090/hello

技术分享图片

最后说明一下@EnableEurekaClient 与@EnableDiscoveryClient这两个注解

首先这个两个注解都可以实现服务发现的功能,在spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等)

@EnableEurekaClient基于spring-cloud-netflix。服务采用eureka作为注册中心,使用场景较为单一。

@EnableDiscoveryClient基于spring-cloud-commons。服务采用其他注册中心。

以上是关于SpringCloud-Eureka:服务的注册与发现的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud-Eureka:服务的注册与发现

7-springcloud-eureka-3-搭建与配置eureka服务注册中心

SpringCloud-Eureka

springcloud-Eureka服务注册发现02

SpringCloud-Eureka-服务注册是如何发起的

云原生微服务SpringCloud-eureka(server)集群搭建