配置映射主機名和IP
為每台虛擬主機的/etc/hosts文件加上如下內容,服務發現需要用到默認主機名作為訪問地址,這樣可以不用為每個服務實例配置IP
192.168.253.30 megumi-30 192.168.253.31 megumi-31 192.168.253.32 megumi-32
服務提供方
1.Maven依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-all</artifactId> </dependency> </dependencies>
2. 父模塊的依賴管理
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-zookeeper-dependencies</artifactId> <version>1.1.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-dependencies</artifactId> <version>1.3.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.reactivex</groupId> <artifactId>rxjava</artifactId> <version>1.3.1</version> </dependency> </dependencies> </dependencyManagement>
3. bootstrap.yml (這里配置應用程序的名稱,也就是服務的名稱,用於Feign查找服務,啟用服務發現和注冊)
spring: application: name: demo-service cloud: zookeeper: connect-string: 192.168.253.30:2181,192.168.253.31:2181,192.168.253.32:2181 discovery: enabled: true register: true
4. App.java 用@EnableDiscoveryClient注解配置啟用服務發現
package demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
5. DemoController.java
package demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/api/demo") public class DemoController { private static final UUID INSTANCE_UUID = UUID.randomUUID(); @GetMapping(value = "/instance") public Object instance() { return INSTANCE_UUID.toString(); } }
服務消費方
1. Maven依賴 (和提供方同父模塊)
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-all</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies>
2. bootstrap.yml 這里選擇不注冊服務
spring:
application:
name: demo-app
cloud:
zookeeper:
connect-string: 192.168.253.30:2181,192.168.253.31:2181,192.168.253.32:2181
discovery:
enabled: true
register: false
3. App.java 同樣用注解啟用Feign客戶端和服務發現
package demo2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
4. DemoService.java 用注解聲明Feign客戶端調用的服務名稱,與服務提供方yml配置的spring.application.name一致,注意這里雖然能用RequestMapping注解,但是可能引發URL路由注冊沖突的問題。
package demo2; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(value = "demo-service", path = "/api/demo") //@RequestMapping(value = "/api/demo") public interface DemoService { @GetMapping(value = "/instance") String instance(); }
5. DemoController.java
package demo2; import java.util.UUID; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/api/demo") public class DemoController { private static final UUID INSTANCE_UUID = UUID.randomUUID(); private final DemoService remoteService; public DemoController(final DemoService remoteService) { this.remoteService = remoteService; } @GetMapping(value = "/remote-instance") public Object remoteInstance() { return this.remoteService.instance(); } @GetMapping(value = "/instance") public Object instance() { return INSTANCE_UUID.toString(); } }
服務啟動后,使用zkCli運行 ls /services/demo-service ,可以看到服務的各實例已經被注冊到這里。
使用瀏覽器訪問消費方的 /api/demo/remote-instance,不停刷新可以看到值是變化的,說明客戶端負載均衡已經在正常工作了。