Eureka集群主要有三個部分Eureka服務器,服務提供者,服務調用者
簡單的來說就是服務提供者將服務注冊到Eureka服務器,服務調用者對其服務進行查找調用。
Eureka服務程序的搭建可參考官方文檔:http://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi_spring-cloud.html
Eureka架構圖:
使用IDEA分模塊搭建三個工程Eureka服務器(server),服務提供者(police),服務調用者(person)
一.搭建服務器
1.引入maven依賴,使用官方文檔中的依賴的結果還是啟動不起來,缺少日志相關的依賴,另外自己添加了幾個依賴后就OK了
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <dependency> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> <version>1.7.10</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
因為Spring cloud集成了很多項目。所以引入spring-cloud-starter-eureka-server就相當於引入了spring-boot-starter-web等,就具有了web容器的功能了。
2.配置yml文件:設置服務器端口以及相關信息(在文檔中都可以找到相關配置)
server: port: 8761 #更改端口為8761 eureka: client: register-with-eureka: false #服務器不用注冊到其他服務器 fetch-registry: false #服務器不用去服務器抓取注冊信息
3.編寫服務啟動類,也就是main方法啟動spring boot,注意使用@EnableEurekaServer注解
package com.nijunyang; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class ServerApp { public static void main(String[] args){ new SpringApplicationBuilder(ServerApp.class).web(true).run(args); } }
啟動之后訪問http://localhost:8761/就可以看到Eureka服務器控制台。
二.服務提供者(警察局)
1.maven依賴將服務器的spring-cloud-starter-eureka-server依賴改為spring-cloud-starter-eureka即可
2.yml配置文件:需要將服務提供者注冊到Eureka服務器上,服務器的端口設置的8761
server:
port: 8080
spring:
application:
name: first-police #服務提供者名字
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #注冊到服務器
3.編寫一個實體類police和PoliceController,有人報警則派出一個警察
package com.nijunyang; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class PoliceController { @RequestMapping(value = "/call/{id}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Police call(@PathVariable Integer id){ Police police = new Police(); police.setId(id); police.setName("zhangsan"); return police; } }
4.該模塊的啟動類,main方法啟動和服務器的一樣 new SpringApplicationBuilder(XXX(啟動類類名).class).web(true).run(args);
三.服務調用者(報警)
1.maveny依賴:在服務提供者的基礎上再加入負載均衡(后續了解)相關的依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
2.yml配置:同樣需要注冊到服務器,上面的服務提供者由於沒有設置端口所以默認是8080,現在將調用者端口設置8081,否則啟動會出錯
server: port: 8081 #更改端口為8081 spring: application: name: first-person eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ #注冊到服務器
3.編寫PersonController,需要新加@Configuration注解,以及配置RestTemplate,RestTemplate本來是spring-web下面的類用來調用REST服務。本身不具備調用分布式服務的能力,但是被@LoadBalanced修飾后就具有訪問分布式服務的能力了(具體涉及負載均衡,后續深入了解)。因為是注冊到Eureka服務器的,所以我在內部請求的時候只需要轉到相應的服務提供者就可以了:http://first-police/xxx
package com.nijunyang; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; @Controller @Configuration public class PersonController { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } @GetMapping(value = "/call/{id}") @ResponseBody public String call(@PathVariable Integer id){ RestTemplate restTemplate = getRestTemplate(); return restTemplate.getForObject("http://first-police/call/" + id ,String.class); } }
4.不多說,啟動類編寫
依次啟動服務,服務提提供者和服務調用者。訪問Eureka控制台就可以看到注冊進去的first-police和first-person
訪問http://localhost:8081/call/id,頁面就會返回某個police的josn信息