在spring cloud體系中,有多種手段實現注冊中心,本例中采用zookeeper作為注冊中心的角色。服務提供者向zookeeper注冊,服務消費者從zookeeper中發現服務提供者的相關信息,從而遠程調用服務提供方。
服務提供者
引入相關依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!-- 熱部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
spring cloud與zookeeper的集成主要依賴spring-cloud-starter-zookeeper-discovery模塊
定義DTO對象
public class UserDTO { private Long id; private String name; private Date birthday; /* 省略getter,setter方法 */ }
定義服務提供接口
@RestController public class ComputeController { @Autowired private DiscoveryClient client; @RequestMapping(value = "/show", method = {RequestMethod.POST}) @ResponseBody public UserDTO show(@RequestParam(value="id") Long id) { ServiceInstance instance = client.getLocalServiceInstance(); UserDTO dto = new UserDTO(); dto.setId(id); dto.setName("scott"); dto.setBirthday(new Date()); return dto; } }
在show方法中返回了dto對象。
配置文件
application.properties server.port=8080 spring.application.name=server bootstrap.properties spring.cloud.zookeeper.connectString=192.168.179.200:2181 spring.cloud.zookeeper.discovery.instanceHost=127.0.0.1 spring.cloud.zookeeper.discovery.instancePort=${server.port}
啟動項目
@SpringBootApplication @EnableDiscoveryClient public class AppStart { public static void main(String[] args) { SpringApplication.run(AppStart.class, args); } }
啟動項目后可以在zookeeper中看到新增了/service節點,該節點有一個子節點server
服務消費者
引入與依賴項,這里用feign作為rest調用工具
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <!-- 熱部署工具 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
定義遠程調用代理對象
@Component @FeignClient(value="server",fallback=FeignConsumerClientHystrix.class) public interface FeignConsumerClient { @RequestMapping(method = RequestMethod.POST, value = "/show") public String getUser(@RequestParam(value = "id") Long id); }
以POST方式發起遠程調用,調用地址根據@FeignClient中value值決定。
通過@FeignClient創建消費端代理對象。value指定服務名,該值與服務提供者的spring.application.name參數值相等,fallback指定異常處理類,異常處理類需實現本接口。
異常處理
@Component public class FeignConsumerClientHystrix implements FeignConsumerClient{ @Override public String getUser(@RequestParam("id") Long id) { return "error"; } }
所實現方法即為異常處理代碼
注入代理對象,發起遠程調用
@RestController public class ClientController { @Autowired private FeignConsumerClient feignConsumerClient; @RequestMapping(value = "/get", method = RequestMethod.GET) @ResponseBody public String add(@RequestParam(value = "id") Long id) { return feignConsumerClient.getUser(id); } }
配置文件
application.properties server.port=8081 spring.application.name=client #斷路器,斷路器跳閘后等待多長時間重試 hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=1000 #斷路器,請求發出后多長時間超時 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 #ribbon連接到服務端的超時時間 ribbon.ConnectTimeout=5000 #ribbon連接到服務端后,多長時間沒有獲取到響應的超時時間 ribbon.ReadTimeout=5000 bootstrap.properties spring.cloud.zookeeper.connectString=192.168.179.200:2181 #不向zookeeper注冊 spring.cloud.zookeeper.discovery.register=false
服務消費者啟動項目
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker public class AppStart { public static void main(String[] args) { SpringApplication.run(AppStart.class, args); } }
啟動項目后瀏覽器訪問http://127.0.0.1:8081/get?id=xxx,正常情況即可看到由userDTO對象轉換得到的json字符串。