在spring cloud體系中,有多種手段實(shí)現(xiàn)注冊中心,本例中采用zookeeper作為注冊中心的角色。服務(wù)提供者向zookeeper注冊,服務(wù)消費(fèi)者從zookeeper中發(fā)現(xiàn)服務(wù)提供者的相關(guān)信息,從而遠(yuǎn)程調(diào)用服務(wù)提供方。
服務(wù)提供者
引入相關(guān)依賴
<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方法
*/
}
定義服務(wù)提供接口
@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}
啟動(dòng)項(xiàng)目
@SpringBootApplication
@EnableDiscoveryClient
public class AppStart {
public static void main(String[] args) {
SpringApplication.run(AppStart.class, args);
}
}
啟動(dòng)項(xiàng)目后可以在zookeeper中看到新增了/service節(jié)點(diǎn),該節(jié)點(diǎn)有一個(gè)子節(jié)點(diǎn)server
服務(wù)消費(fèi)者
引入與依賴項(xiàng),這里用feign作為rest調(diào)用工具
<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>
定義遠(yuǎn)程調(diào)用代理對象
@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方式發(fā)起遠(yuǎn)程調(diào)用,調(diào)用地址根據(jù)@FeignClient中value值決定。
通過@FeignClient創(chuàng)建消費(fèi)端代理對象。value指定服務(wù)名,該值與服務(wù)提供者的spring.application.name參數(shù)值相等,fallback指定異常處理類,異常處理類需實(shí)現(xiàn)本接口。
異常處理
@Component
public class FeignConsumerClientHystrix implements FeignConsumerClient{
@Override
public String getUser(@RequestParam("id") Long id) {
return "error";
}
}
所實(shí)現(xiàn)方法即為異常處理代碼
注入代理對象,發(fā)起遠(yuǎn)程調(diào)用
@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
#斷路器,斷路器跳閘后等待多長時(shí)間重試
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=1000
#斷路器,請求發(fā)出后多長時(shí)間超時(shí)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
#ribbon連接到服務(wù)端的超時(shí)時(shí)間
ribbon.ConnectTimeout=5000
#ribbon連接到服務(wù)端后,多長時(shí)間沒有獲取到響應(yīng)的超時(shí)時(shí)間
ribbon.ReadTimeout=5000
bootstrap.properties
spring.cloud.zookeeper.connectString=192.168.179.200:2181
#不向zookeeper注冊
spring.cloud.zookeeper.discovery.register=false
服務(wù)消費(fèi)者啟動(dòng)項(xiàng)目
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class AppStart {
public static void main(String[] args) {
SpringApplication.run(AppStart.class, args);
}
}
啟動(dòng)項(xiàng)目后瀏覽器訪問http://127.0.0.1:8081/get?id=xxx,正常情況即可看到由userDTO對象轉(zhuǎn)換得到的json字符串。
后續(xù)補(bǔ)充spring cloud中的異常處理方法,故障轉(zhuǎn)移,負(fù)載均衡等。