SpringCloud搭建Eureka集群

第一部分:搭建Eureka Server集群

Step1:新建工程,引入依赖

依赖文件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.fangxing.springcloud</groupId>
 7     <artifactId>euruka-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>euruka-server</name>
12     <description>Demo project for Eureka Server</description>
13 
14     <!--基于Springboot-->
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.4.0.RELEASE</version>
19         <relativePath/>
20     </parent>
21 
22     <properties>
23         <!--设置字符编码及java版本-->
24         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25         <java.version>1.8</java.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         </dependency>
34         <!--用于测试的,本例可省略-->
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40     </dependencies>
41 
42     <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
43     <dependencyManagement>
44         <dependencies>
45             <dependency>
46                 <groupId>org.springframework.cloud</groupId>
47                 <artifactId>spring-cloud-dependencies</artifactId>
48                 <version>Camden.SR3</version>
49                 <type>pom</type>
50                 <scope>import</scope>
51             </dependency>
52         </dependencies>
53     </dependencyManagement>
54 
55     <build>
56         <plugins>
57             <plugin>
58                 <groupId>org.springframework.boot</groupId>
59                 <artifactId>spring-boot-maven-plugin</artifactId>
60             </plugin>
61         </plugins>
62     </build>
63 
64 
65 </project>
View Code

 

Step2:开启Eureka的注册中心

 1 /**
 2  * 开启Eureka的注册中心,只需要添加这个注解即可
 3  */
 4 @EnableEurekaServer
 5 @SpringBootApplication
 6 public class EurukaServerApplication {
 7 
 8     public static void main(String[] args) {
 9         SpringApplication.run(EurukaServerApplication.class, args);
10     }
11 }

  

Step3:添加多实例的配置文件

既然是集群,1个实例就太寒碜了,所以我们通过配置文件制定了多个实例。

application-peer1.properties文件内容如下:

1 spring.application.name=eureka-server
2 server.port=1111
3 
4 eureka.instance.hostname=peer1
5 eureka.client.serviceUrl.defaultZone=http://peer2:2222/eureka/

 

application-peer2.properties文件内容如下:

1 spring.application.name=eureka-server
2 server.port=2222
3 
4 eureka.instance.hostname=peer2
5 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/

 

其中:eureka.client.serviceUrl.defaultZone指的是注册中心的地址,所以两个实例互相指向。

PS:这里的peer1、peer2需要再host中配置一番,如下所示:

1 127.0.0.1 peer1
2 127.0.0.1 peer2

 

 

Step4:启动注册中心的多实例

再idea中配置多实例的启动参数,如下所示:

 

配置好了,依次启动peer1和peer2

 

 Step5:Eureka的注册中心去验证

 

第二部分:搭建Eureka Client集群

 Step1:新建工程,引入配置文件

我测试时的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.fangxing.springcloud</groupId>
 7     <artifactId>euruka-client</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>euruka-server</name>
12     <description>Demo project for Eureka Server</description>
13 
14     <!--基于Springboot-->
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.4.0.RELEASE</version>
19         <relativePath/>
20     </parent>
21 
22     <properties>
23         <!--设置字符编码及java版本-->
24         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25         <java.version>1.8</java.version>
26     </properties>
27 
28     <dependencies>
29         <!--增加eureka-server的依赖-->
30         <dependency>
31             <groupId>org.springframework.cloud</groupId>
32             <artifactId>spring-cloud-starter-eureka</artifactId>
33         </dependency>
34         <!--用于测试的,本例可省略-->
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40     </dependencies>
41 
42     <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
43     <dependencyManagement>
44         <dependencies>
45             <dependency>
46                 <groupId>org.springframework.cloud</groupId>
47                 <artifactId>spring-cloud-dependencies</artifactId>
48                 <version>Brixton.SR5</version>
49                 <type>pom</type>
50                 <scope>import</scope>
51             </dependency>
52         </dependencies>
53     </dependencyManagement>
54 
55     <build>
56         <plugins>
57             <plugin>
58                 <groupId>org.springframework.boot</groupId>
59                 <artifactId>spring-boot-maven-plugin</artifactId>
60             </plugin>
61         </plugins>
62     </build>
63 
64 
65 </project>
Eureka Client的pom.xml

 

 

Step2:启动发现服务

1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class EurekalclientApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(EurekalclientApplication.class, args);
7     }
8 }

 

 

Step3:配置Client的应用

1 spring.application.name=eureka-client
2 
3 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:2222/eureka/

 提供一个测试的Controller。

 1 @RestController
 2 public class HelloController {
 3 
 4     @Autowired
 5     private DiscoveryClient client;
 6 
 7     @RequestMapping(value = "/hello",method = RequestMethod.GET)
 8     public String index(){
 9         ServiceInstance instance=client.getLocalServiceInstance();
10         System.out.println("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
11         return "hello world!";
12     }
13 
14 }

 

 

Step4:启动多个实例

 因为这里的多个实例,我们需要不同的端口,所以,我们还是采用前面的方法来启动多个实例。

启动后,我们可以在Eureka的注册中心中可以看到(Peer1和Peer2都可以看到)

PS:这里localhost:eureka-client这个是测试时残留的,这里没有清除掉,可以不用管它。

 

到这里,我们的服务注册中心和服务提供者都写好了。

 

第三部分:消费Eureka Client的消息

 

Step1:新建工程,引入依赖

注意,这里我们要引入新的依赖ribbon。

 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.fangxing.springcloud</groupId>
 7     <artifactId>euruka-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>euruka-server</name>
12     <description>Demo project for Eureka Server</description>
13 
14     <!--基于Springboot-->
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.4.0.RELEASE</version>
19         <relativePath/>
20     </parent>
21 
22     <properties>
23         <!--设置字符编码及java版本-->
24         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25         <java.version>1.8</java.version>
26     </properties>
27 
28     <dependencies>
29         <!--增加eureka-server的依赖-->
30         <dependency>
31             <groupId>org.springframework.cloud</groupId>
32             <artifactId>spring-cloud-starter-eureka</artifactId>
33         </dependency>
34         <!--增加ribbon的依赖-->
35         <dependency>
36             <groupId>org.springframework.cloud</groupId>
37             <artifactId>spring-cloud-starter-ribbon</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     <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
48     <dependencyManagement>
49         <dependencies>
50             <dependency>
51                 <groupId>org.springframework.cloud</groupId>
52                 <artifactId>spring-cloud-dependencies</artifactId>
53                 <version>Brixton.SR5</version>
54                 <type>pom</type>
55                 <scope>import</scope>
56             </dependency>
57         </dependencies>
58     </dependencyManagement>
59 
60     <build>
61         <plugins>
62             <plugin>
63                 <groupId>org.springframework.boot</groupId>
64                 <artifactId>spring-boot-maven-plugin</artifactId>
65             </plugin>
66         </plugins>
67     </build>
68 
69 
70 </project>
Eureka消费者的pom.xml

 

 

 Step2:启用发现服务并开启客户端负载均衡

 1 @EnableDiscoveryClient
 2 @SpringBootApplication
 3 public class RibbonConsumerApplication {
 4 
 5     public static void main(String[] args) {
 6         SpringApplication.run(RibbonConsumerApplication.class, args);
 7     }
 8 
 9     //开启客户端负载均衡
10     @Bean
11     @LoadBalanced
12     RestTemplate restTemplate(){
13         return new RestTemplate();
14     }
15 }

 

 

Step3:配置消费者(跟生产者类似)

1 spring.application.name=ribbon-consumer
2 server.port=9000
3 
4 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:2222/eureka/

 

 

Step4:编写消费者实例

 1 @RestController
 2 public class ConsumerController {
 3 
 4     @Autowired
 5     RestTemplate restTemplate;
 6 
 7     @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
 8     public String helloConsumer(){
 9         return restTemplate.getForEntity("http://eureka-client/hello",String.class).getBody();
10     }
11 
12 }

 

 

Step5:验证Eureka注册中心

 

 Step6:验证请求

 

参考文档:

《SpringCloud 微服务实践》

 源码:https://github.com/iprometheus/spring-cloud-eureka-test

posted @ 2018-03-25 18:34  gudi  阅读(18081)  评论(1编辑  收藏  举报