wordpress能做什么网站,广东省网站设计师,上海有哪些做网站的公司,高校门户网站系统高并发处理的思路#xff1a;
扩容#xff1a;水平扩容、垂直扩容缓存#xff1a;将基础的数据放入缓存进行处理使用SpringCloud的注册中心#xff0c;分服务注册到同一个注册中心#xff0c;服务器检测使用Spring的熔断操作#xff0c;检测服务器的心跳那个正常随机跳转…
高并发处理的思路
扩容水平扩容、垂直扩容缓存将基础的数据放入缓存进行处理使用SpringCloud的注册中心分服务注册到同一个注册中心服务器检测使用Spring的熔断操作检测服务器的心跳那个正常随机跳转到正常的服务器上
也可以使用熔断机制通过实现Hystrix会监测微服务间调用的状况当失败的调用到一定阈值缺省是5秒内20次调用失败就会启用熔断机制
熔断机制的注解是HystrixCommand Hystrix会找到有这个的注解并将这类方法关联到和熔断器连在一起的代理上HystrixCommand仅当类的注解为Service和Component时
才会发挥作用。
微服务之间的调用有两种方式一种是一个是RestTemplate另一个是Feign。相对应在这两种调用方式下都有Hystrix调用方法
数据量大的在数据库做集成处理
对于微服务项目开发中多个微服务之间不仅是相对独立的而且也是相对关联的。也就是说微服务之间需要相互访问多个微服务之间的接口可能会被互相调用多次我们称之为微服务之间的通信。
微服务之间的通信方式有很多种 一般都是使用RestTemplate 或者Feign
RestTemplate是Spring中方便使用rest资源的一个对象交互访问的资源通过URL进行识别和定位。每次调用都使用模板方法的设计模式模板方法依赖于具体的接口调用从而实现了资源交互和调用。它的交互方法有30多种大多数都是基于HTTP的方法
例如delete()getForEntity(),getForObject(),put(),headForHeaders()
添加对应依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
启动添加负载均衡标识
LoadBalanced
Bean
public RestTemplate getRestTemplate() {return new RestTemplate();
}
服务提供类服务名称SERVICE1端口7082
RestController
RequestMapping(/service1)
public class TestController {RequestMapping(value test, method {RequestMethod.POST,RequestMethod.GET})public String testService(RequestParam(value testParam) String testParam) {System.println.out(testParam);return success;}}
服务消费类
RestController
RequestMapping(/serviceFront)
public class ServiceFrontController {private final static String SERVICE1_URL http://SERVICE1:7082;private final static String SERVICE1 SERVICE1;Autowired LoadBalancerClient loadBalancerClient;AutowiredRestTemplate restTemplate;RequestMapping(value testFront, method RequestMethod.POST)public HashMapString,Object testFront(RequestParam String testParam) {this.loadBalancerClient.choose(SERVICE1);// 随机访问策略String result restTemplate.getForObject(SERVICE1_URL /service1/test?testParam{1}, String.class, testParam);HashMapString,Object map new HashMapString,Object();map.put(result, 测试结果result);return map;}
}
RestTemplate发送post请求主要的参数有如下几种
String url 请求的路径Object request请求体【RequestBody 注解接收】或者是一个HttpEntity对象(包含请求参数请求头)Class T responseType接收返回数据的类型MapString,? uriVariables uri 变量 这是放置变量的地方Object… uriVariables可变长 Object 类型 参数 restTemplate.postForObject(http://XXXXXXXX?name{name}age{age}, request, JSONObject.class, name,age); Feign,是声明式的伪HTTP客户端使得编写HTTP客户端更新容易只需要创建一个接口并且使用注解的方式去配置即可完成对服务提供方接口的绑定大大简化了代码量同时它还具有可拔插的注解特性而且支持feign自定义的注解和springMvc的注解。
添加具体的Feign依赖
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-feign/artifactId
/dependency
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-ribbon/artifactId
/dependency 在启动类Application添加feign注解声明启动feign客户端
EnableFeignClients
服务提供类服务名称SERVICE2 端口7083
RestController
RequestMapping(/service2)
public class TestController{RequestMapping(value test2, method {RequestMethod.POST,RequestMethod.GET})public String test2(RequestParam(value testParam2) String testParam2) {System.println.out(testParam2);return success;}} 服务消费接口类
FeignClient(name SERVICE2)
public interface TestFeignClient { RequestMapping(value/service2/test2,method RequestMethod.GET)public String test2(RequestParam(testParam2) String testParam2);}
服务消费控制层
RestController
RefreshScope
RequestMapping(/serviceFront2)
public class TestFeignController {Autowiredprivate TestFeignClient testFeignClient;RequestMapping(value test2, method { RequestMethod.POST })public HashMapString,Object test2(RequestParam String testParam2) { String result testFeignClient.test2(testParam2);HashMapString,Object map new HashMapString,Object();map.put(result, 测试结果result);return map;}
}
总之微服务之间的通讯方式可以多种并存各有优势在项目实践中可具体情况具体分析