Eureka集羣主要有三個部分Eureka服務器,服務提供者,服務調用者java
簡單的來講就是服務提供者將服務註冊到Eureka服務器,服務調用者對其服務進行查找調用。
須要JAVA Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼一七九一七四三三八零 web
一 搭建服務器1.引入maven依賴,使用官方文檔中的依賴的結果仍是啓動不起來,缺乏日誌相關的依賴,另外本身添加了幾個依賴後就OK了
spring
<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容器的功能了。api
2.配置yml文件:設置服務器端口以及相關信息服務器
server: port: 8761 #更改端口爲8761 eureka: client: register-with-eureka: false #服務器不用註冊到其餘服務器 fetch-registry: false #服務器不用去服務器抓取註冊信息
3.編寫服務啓動類,也就是main方法啓動spring boot,注意使用@EnableEurekaServer註解app
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服務器控制檯。負載均衡
二 服務提供者(警察局)maven
1.maven依賴將服務器的spring-cloud-starter-eureka-server依賴改成spring-cloud-starter-eureka便可分佈式
2.yml配置文件:須要將服務提供者註冊到Eureka服務器上,服務器的端口設置的8761spring-boot
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信息。