第一部分:搭建Eureka Server集羣java
Step1:新建工程,引入依賴git
依賴文件pom.xml以下github
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>
Step2:開啓Eureka的註冊中心spring
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:添加多實例的配置文件apache
既然是集羣,1個實例就太寒磣了,因此咱們經過配置文件制定了多個實例。app
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文件內容以下:maven
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指的是註冊中心的地址,因此兩個實例互相指向。ide
PS:這裏的peer一、peer2須要再host中配置一番,以下所示:spring-boot
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>
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>
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