产品服务
controller:
@RestController
@RequestMapping("/product")
public class ProductController {
@RequestMapping("/getProduct/{id}")
public Object getProduct(HttpServletRequest request, @PathVariable("id") String id) {
return new Product(id,"name:"+request.getLocalPort());
}
}
启动类:
@SpringBootApplication
public class ProductApp {
public static void main(String[] args) {
SpringApplication.run(ProductApp.class,args);
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean() {
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new InitListener());
return servletListenerRegistrationBean;
}
}
listener:
public class InitListener implements ServletContextListener {
@Value("${server.port}")
private int port;
@Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getAutowireCapableBeanFactory().autowireBean(this);
try {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
ServiceRegister.register(hostAddress,port);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
register:
public class ServiceRegister {
private static final String BASE_SERVICES = "/services";
private static final String SERVICE_NAME="/products";
public static void register(String address,int port) {
try {
ZooKeeper zooKeeper = new ZooKeeper("localhost:2181",5000,(watchedEvent)->{});
Stat exists = zooKeeper.exists(BASE_SERVICES + SERVICE_NAME, false);
if(exists==null) {
zooKeeper.create(BASE_SERVICES + SERVICE_NAME,"".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
String server_path = address+":"+port;
zooKeeper.create(BASE_SERVICES + SERVICE_NAME+"/child",server_path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
} catch (Exception e) {
e.printStackTrace();
}
}
}
订单服务
controller:
@RequestMapping("/order")
@RestController
public class OrderController {
@Resource
private RestTemplate restTemplate;
private LoadBalance loadBalance = new RamdomLoadBalance();
@RequestMapping("/getOrder/{id}")
public Object getOrder(@PathVariable("id") String id ) {
Product product = restTemplate.getForObject("http://"+loadBalance.choseServiceHost()+"/product/getProduct/1", Product.class);
return new Order(id,"orderName",product);
}
}
均衡器:
public abstract class LoadBalance {
public volatile static List<String> SERVICE_LIST;
public abstract String choseServiceHost();
}
public class RamdomLoadBalance extends LoadBalance {
@Override
public String choseServiceHost() {
String result = "";
if(!CollectionUtils.isEmpty(SERVICE_LIST)) {
int index = new Random().nextInt(SERVICE_LIST.size());
result = SERVICE_LIST.get(index);
}
return result ;
}
}
启动类
@SpringBootApplication
public class OrderApp {
public static void main(String[] args) {
SpringApplication.run(OrderApp.class,args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean() {
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new InitListener());
return servletListenerRegistrationBean;
}
}
listner:
public class InitListener implements ServletContextListener {
private static final String BASE_SERVICES = "/services";
private static final String SERVICE_NAME="/products";
private ZooKeeper zooKeeper;
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
zooKeeper = new ZooKeeper("localhost:2181",5000,(watchedEvent)->{
if(watchedEvent.getType() == Watcher.Event.EventType.NodeChildrenChanged && watchedEvent.getPath().equals(BASE_SERVICES+SERVICE_NAME)) {
updateServiceList();
}
});
updateServiceList();
} catch (Exception e) {
e.printStackTrace();
}
}
private void updateServiceList() {
try{
List<String> children = zooKeeper.getChildren(BASE_SERVICES + SERVICE_NAME, true);
List<String> newServerList = new ArrayList<String>();
for(String subNode:children) {
byte[] data = zooKeeper.getData(BASE_SERVICES + SERVICE_NAME + "/" + subNode, false, null);
String host = new String(data, "utf-8");
System.out.println("host:"+host);
newServerList.add(host);
}
LoadBalance.SERVICE_LIST = newServerList;
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}