├── doc
└── Eureka集群.jpeg
├── springcloud-eureka-cluster
├── springcloud-eureka-cluster-client-provider
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ └── application.yml
│ │ │ └── java
│ │ │ └── org
│ │ │ └── spring
│ │ │ └── springcloud
│ │ │ ├── ProviderApplication.java
│ │ │ └── web
│ │ │ └── ProviderController.java
│ └── pom.xml
├── springcloud-eureka-cluster-client-customer
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ └── application.yml
│ │ │ └── java
│ │ │ └── org
│ │ │ └── spring
│ │ │ └── springcloud
│ │ │ ├── CustomerApplication.java
│ │ │ └── web
│ │ │ └── CustomerController.java
│ └── pom.xml
├── springcloud-eureka-cluster-server
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ ├── application-cluster8888.yml
│ │ │ └── application-cluster8889.yml
│ │ │ └── java
│ │ │ └── org
│ │ │ └── spring
│ │ │ └── springcloud
│ │ │ └── EurekaServerApplication.java
│ └── pom.xml
└── pom.xml
├── springcloud-eureka-sample
├── springcloud-eureka-client-customer
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ └── application.yml
│ │ │ └── java
│ │ │ └── org
│ │ │ └── spring
│ │ │ └── springcloud
│ │ │ ├── CustomerApplication.java
│ │ │ └── web
│ │ │ └── CustomerController.java
│ └── pom.xml
├── springcloud-eureka-client-provider
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ └── application.yml
│ │ │ └── java
│ │ │ └── org
│ │ │ └── spring
│ │ │ └── springcloud
│ │ │ ├── ProviderApplication.java
│ │ │ └── web
│ │ │ └── ProviderController.java
│ └── pom.xml
├── springcloud-eureka-server
│ ├── src
│ │ └── main
│ │ │ ├── resources
│ │ │ └── application.yml
│ │ │ └── java
│ │ │ └── org
│ │ │ └── spring
│ │ │ └── springcloud
│ │ │ └── EurekaServerApplication.java
│ └── pom.xml
└── pom.xml
├── pom.xml
└── README.md
/doc/Eureka集群.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpringForAll/springcloud-learning-example/HEAD/doc/Eureka集群.jpeg
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-client-provider/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | eureka:
2 | client:
3 | service-url:
4 | defaultZone: http://hostA:8888/eureka/,http://hostB:8889/eureka/
5 |
6 | spring:
7 | application:
8 | name: cluster-provider-service
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-client-customer/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8083
3 |
4 | eureka:
5 | client:
6 | service-url:
7 | defaultZone: http://localhost:8888/eureka/
8 |
9 | spring:
10 | application:
11 | name: cluster-customer-service
--------------------------------------------------------------------------------
/springcloud-eureka-sample/springcloud-eureka-client-customer/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8081 # 服务端口
3 |
4 | eureka:
5 | client:
6 | service-url:
7 | defaultZone: http://localhost:8888/eureka/ # 服务注册中心地址
8 |
9 | spring:
10 | application:
11 | name: customer-service # 服务名称
--------------------------------------------------------------------------------
/springcloud-eureka-sample/springcloud-eureka-client-provider/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8080 # 服务端口
3 |
4 | eureka:
5 | client:
6 | service-url:
7 | defaultZone: http://localhost:8888/eureka/ # 服务注册中心地址
8 |
9 | spring:
10 | application:
11 | name: provider-service # 服务名称
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-server/src/main/resources/application-cluster8888.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8888
3 |
4 | eureka:
5 | instance:
6 | hostname: hostA # 设置主机名
7 | client:
8 | service-url:
9 | defaultZone: http://hostB:8889/eureka/
10 |
11 | spring:
12 | application:
13 | name: eureka-cluster-server
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-server/src/main/resources/application-cluster8889.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8889
3 |
4 | eureka:
5 | instance:
6 | hostname: hostB # 设置主机名
7 | client:
8 | service-url:
9 | defaultZone: http://hostA:8888/eureka/
10 |
11 | spring:
12 | application:
13 | name: eureka-cluster-server
--------------------------------------------------------------------------------
/springcloud-eureka-sample/springcloud-eureka-server/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8888 # 服务端口
3 |
4 | eureka:
5 | instance:
6 | hostname: localhost # 设置主机名
7 | client:
8 | registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false
9 | fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
10 | server:
11 | waitTimeInMsWhenSyncEmpty: 0 # 设置同步为空时的等待时间。默认 5 * MINUTES
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
13 | * Created by bysocket on 06/22/17.
14 | */
15 | @RestController
16 | public class CustomerController {
17 |
18 | private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);
19 |
20 | @Autowired
21 | private RestTemplate restTemplate; // HTTP 访问操作类
22 |
23 | @RequestMapping("/customer")
24 | public String customer() {
25 | String providerMsg = restTemplate.getForEntity("http://PROVIDER-SERVICE/provider",
26 | String.class).getBody();
27 |
28 | return "Hello,Customer! msg from provider :
" + providerMsg;
29 | }
30 | }
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-client-customer/src/main/java/org/spring/springcloud/CustomerApplication.java:
--------------------------------------------------------------------------------
1 | package org.spring.springcloud;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 | import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.web.client.RestTemplate;
9 |
10 | /**
11 | * Spring Boot Eureka Server 应用启动类
12 | *
13 | * Created by bysocket on 21/06/17.
14 | */
15 | @EnableDiscoveryClient // Eureka Discovery Client 标识
16 | @SpringBootApplication // Spring Boot 应用标识
17 | public class CustomerApplication {
18 |
19 | @Bean
20 | @LoadBalanced
21 | RestTemplate restTemplate() {
22 | return new RestTemplate();
23 | }
24 |
25 | public static void main(String[] args) {
26 | // 程序启动入口
27 | // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
28 | SpringApplication.run(CustomerApplication.class,args);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-client-customer/src/main/java/org/spring/springcloud/web/CustomerController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springcloud.web;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RestController;
8 | import org.springframework.web.client.RestTemplate;
9 |
10 | /**
11 | * Customer HelloWorld 案例
12 | *
13 | * Created by bysocket on 06/22/17.
14 | */
15 | @RestController
16 | public class CustomerController {
17 |
18 | private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);
19 |
20 | @Autowired
21 | private RestTemplate restTemplate; // HTTP 访问操作类
22 |
23 | @RequestMapping("/customer")
24 | public String customer() {
25 | String providerMsg = restTemplate.getForEntity("http://CLUSTER-PROVIDER-SERVICE/provider",
26 | String.class).getBody();
27 |
28 | return "Hello,Customer! msg from provider :
" + providerMsg;
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/springcloud-eureka-cluster/springcloud-eureka-cluster-client-provider/src/main/java/org/spring/springcloud/web/ProviderController.java:
--------------------------------------------------------------------------------
1 | package org.spring.springcloud.web;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.cloud.client.ServiceInstance;
7 | import org.springframework.cloud.client.discovery.DiscoveryClient;
8 | import org.springframework.cloud.client.serviceregistry.Registration;
9 | import org.springframework.web.bind.annotation.RequestMapping;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * Provider HelloWorld 案例
16 | *
17 | * Created by bysocket on 06/22/17.
18 | */
19 | @RestController
20 | public class ProviderController {
21 |
22 | private static final Logger LOGGER = LoggerFactory.getLogger(ProviderController.class);
23 |
24 | @Autowired
25 | private DiscoveryClient discoveryClient;
26 |
27 | @Autowired
28 | private Registration registration;
29 |
30 | @RequestMapping("/provider")
31 | public String provider() {
32 | ServiceInstance instance = serviceInstance();
33 | LOGGER.info("provider service, host = " + instance.getHost()
34 | + ", service_id = " + instance.getServiceId());
35 | return "Hello,Provider!";
36 | }
37 |
38 | public ServiceInstance serviceInstance() {
39 | List
17 | * Created by bysocket on 06/22/17.
18 | */
19 | @RestController
20 | public class ProviderController {
21 |
22 | private static final Logger LOGGER = LoggerFactory.getLogger(ProviderController.class);
23 |
24 | @Autowired
25 | private Registration registration; // 服务注册
26 |
27 | @Autowired
28 | private DiscoveryClient discoveryClient; // 服务发现客户端
29 |
30 | @RequestMapping("/provider")
31 | public String provider() {
32 | ServiceInstance instance = serviceInstance();
33 | LOGGER.info("provider service, host = " + instance.getHost()
34 | + ", service_id = " + instance.getServiceId());
35 | return "Hello,Provider!";
36 | }
37 |
38 | /**
39 | * 获取当前服务的服务实例
40 | *
41 | * @return ServiceInstance
42 | */
43 | public ServiceInstance serviceInstance() {
44 | List
10 | Spring For All 社区 ② 123013854(满)
11 | Spring For All 社区 ③ 290714704(满)
12 | Spring For All 社区 ④ 112133511(满)
13 | Spring For All 社区 ⑤ 157525002(满)
14 | Spring For All 社区 ⑥ 564840207
15 | Spring For All 社区 ⑦ 470962790(满)
16 | Spring For All 社区 ⑧ 613456104(满)
17 | Spring For All 社区 ⑨ 534583667
18 | Spring For All 社区 ⑩ 210742970 (满)
19 | Spring For All 社区 ⑪ 517395240
20 | Spring For All 社区 ⑫ 498098401
21 |
22 | 博主微信:139-5868-6678
23 |
24 | ## 作者与学习乐园
25 | 源码地址:我的[GitHub地址](https://github.com/JeffLi1993 "GitHub")、[OSCGit地址](https://git.oschina.net/jeff1993/springboot-learning-example "OSCGit")
26 | 作者:[泥瓦匠BYSocket](http://www.bysocket.com/ "泥瓦匠BYSocket")
27 | 关注微信公众号【泥瓦匠BYSokcet】,及时得到技术文章推送
28 | 
29 |
30 |
31 | ## 一、项目结构
32 | 「Spring Cloud 那些事」:[传送门](http://www.bysocket.com/?p=1907)
33 |
34 | ### a. 『 Eureka 篇 』
35 |
36 | #### springcloud-eureka-sample (Spring Cloud 之 Eureka 入门案例)
37 | - springcloud-eureka-server (Spring Cloud Eureka 注册中心服务)
38 | - springcloud-eureka-client-provider (Spring Cloud 服务提供者)
39 | - springcloud-eureka-client-customer (Spring Cloud 服务消费者)
40 |
41 | 对应教程:
42 | - [《Spring Cloud Eureka 入门 (一)服务注册中心详解》](http://spring4all.com/article/101)
43 | - [《Spring Cloud Eureka 入门 (二)服务提供者详解》](http://spring4all.com/article/122)
44 | - [《Spring Cloud Eureka 入门 (三)服务消费者详解》](http://spring4all.com/article/131)
45 |
46 | - - -
47 |
48 | #### springcloud-eureka-cluster (Spring Cloud 之 Eureka cluster 集群案例)
49 | - springcloud-eureka-cluster-server (Spring Cloud Eureka 服务注册中心集群)
50 | - springcloud-eureka-cluster-client-provider (Spring Cloud 服务提供者集群)
51 | - springcloud-eureka-cluster-client-customer (Spring Cloud 服务消费者集群)
52 |
53 | 对应教程:
54 | - [《Spring Cloud 之 Eureka 集群搭建(一)Eureka 注册中心服务集群》](http://spring4all.com/explore/category-springcloud)
55 | - [《Spring Cloud 之 Eureka 集群搭建(二)Eureka 服务提供者和消费者集群》](http://spring4all.com/explore/category-springcloud)
56 |
57 |
58 | ## 二、项目 Quick Start 快速开发指南
59 |
60 | 推荐
61 | [《Spring Boot教程与Spring Cloud教程》](https://git.oschina.net/didispace/SpringBoot-Learning "Spring Boot教程与Spring Cloud教程")
62 |
--------------------------------------------------------------------------------