java怎么拦截某个对象
215
2022-09-23
springboot2原理实战(20)--微服务自己实现注册发现,负载均衡小demo
文章目录
概要服务端代码地址:客户端代码地址:
概要
之前了解了很多的springboot的基础知识,现在实战下,做个微服务,什么是微服务呢? 就是按业务拆分模块,然后模块间通过restful或者rpc调用。 本文主要写
①一个服务端,一个客户端,服务端写个接口,客户端通过restful查询。②加个注册中心:服务端把注册地址到给zookeeper,客户端通过zookeeper查询数据③多个服务注册到zookeeper,实现负载均衡。
服务端代码地址:
* 服务注册 */@Componentpublic class ServiceRegister implements ApplicationRunner { @Value("${zookeeper.address}") private String zkAddress; @Override public void run(ApplicationArguments args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(zkAddress, new RetryOneTime(1000)); client.start(); client.blockUntilConnected(); ServiceInstance
客户端代码地址:
最简单的resful版调用
public class Client { public static String BASE_URL =" public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); Response object = restTemplate.getForObject(BASE_URL + "/soa/product/1", Response.class); System.out.println(new Gson().toJson(object)); String data = restTemplate.getForObject(BASE_URL + "/soa/product/1", String.class); System.out.println(data); System.out.println(new Gson().toJson(new Gson().fromJson(data, Response.class))); System.out.println(new Gson().toJson(new Gson().fromJson(data, Map.class).get("data"))); }}
zookeeper版调用
public class AMallProductWebApplication { public static String ZKADDRESS ="192.168.157.111:2181"; public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(ZKADDRESS, new RetryOneTime(1000)); client.start(); client.blockUntilConnected(); ServiceDiscovery
负载均衡+zookeeper版调用
/** * 轮询器 */public class LoaderBalance { private int index = 0; private List
public class AppBanlance { public static String ZKADDRESS ="192.168.157.111:2181"; public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(ZKADDRESS, new RetryOneTime(1000)); client.start(); client.blockUntilConnected(); ServiceDiscovery
搜索: 怒放de每一天
不定时推送相关文章,期待和大家一起成长!!
完
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~