首页 文章 精选 留言 我的

精选列表

搜索[java],共10000篇文章
优秀的个人博客,低调大师

Java版Spring Cloud B2B2C o2o社交电商- zuul网关实现

一、简介 在Springcloud中用zuul来实现网关功能,客户端的请求首先经过负载均衡Ngnix,再到达服务网关(zuul集群),然后再到具体的服务。Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/server1转发到到server1服务。zuul默认和Ribbon结合实现了负载均衡的功能。 二、搭建 首先是POM文件 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 然后在applicaton类加上注解@EnableZuulProxy,开启zuul的功能 @SpringBootApplication @EnableZuulProxy @EnableEurekaClient @RefreshScope public class HfzZuulApplication { public static void main(String[] args) { SpringApplication.run(HfzZuulApplication.class, args); } } yml配置如下 eureka: client: serviceUrl: defaultZone: http://name:pass@IP/eureka/ instance: ip-address: Ip地址 prefer-ip-address: true server: port: 8769 spring: application: name: service-zuul sleuth: sampler: percentage: 1.0 cloud: config: discovery: enabled: true service-id: CONFIG-SERVER label: master profile: dev name: hfz-zuul username: name password: pass 以上是在项目中配置的,为了使项目更加灵活,所以将路由的配置放在github上,这样可以动态读取 zuul: routes: api-a: path: /api-a/** serviceId: service-ribbon api-b: path: /api-b/** serviceId: service-feign 以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务; 三、服务过滤 zuul不仅可以路由,并且还能通过过滤来拦截一些服务,可以用来做安全验证。 public class MyFilter extends ZuulFilter{ private static Logger log = LoggerFactory.getLogger(MyFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString())); Object accessToken = request.getParameter("token"); if(accessToken == null) { log.warn("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); }catch (Exception e){} return null; } log.info("ok"); return null; } } filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: pre:路由之前 routing:路由之时 post: 路由之后 error:发送错误调用 filterOrder:过滤的顺序 shouldFilter:逻辑判断,是否要过滤 run:过滤器的具体逻辑控制 接下来就可以测试访问了。

优秀的个人博客,低调大师

Java版Spring Cloud B2B2C o2o鸿鹄云商平台--部署架构

上一篇文章我们介绍了鸿鹄云商b2b2c o2o基础的部署方案,考虑到项目部署中架构拓展性、伸缩性、可用性、安全性、开放性、准确性、可维护性的特点,我们针对鸿鹄云商平台b2b2c制定整体部署架构方案,我这里不做更多的介绍,直接上图,希望能通过部署架构图让更多的读者理解。 以上是我参与的Spring Cloud B2B2C O2O鸿鹄云商平台-部署架构解决方案,从现在开始,我会将每一个细节点落实到文章上,希望能够帮助更多的朋友。

优秀的个人博客,低调大师

java版spring cloud+spring boot+redis社交电子商务平台-spring-cloud-config

创建配置管理服务器及实现分布式配置管理应用,实现统一配置管理。 提供三种方式: 基于git 基于svn(淘汰) 基于本地文件(测试使用) 如何使用 创建server端 创建client端 创建server端 让你的分布式的应用可以取到配置。服务端很简单,只需要配置你的配置文件位于哪里就行了。 pom.xml: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> 当然了,我已经在全局加入了一些其他配置文件,因为我使用了模块式的开发,所以这里很简单。 配置文件: spring.application.name=config-server server.port=8888 spring.cloud.config.server.git.uri=file:///${user.home}/config-repo 一般端口都是8888,可以随意设置,git这里我采用了本地git,方便测试。如果是远程的话,肯定是私有的内部公开的,可以使用用户名和密码登录。官网查看最新的配置文件即可。 启动: @SpringBootApplication @EnableConfigServer public class SpringCloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServerApplication.class, args); } } 在启动文件里,加入这样一句话就好啦。 创建client端 当然了,也很简单。 pom.xml: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> 配置文件: spring.application.name=appname1 server.port=8082 spring.profiles.active=dev spring.cloud.config.profile=dev spring.cloud.config.uri=http://localhost:8888/ 这里主要就是你的服务端在哪里。spring.application.name 和 spring.cloud.config.profile 决定了会去远程git里取哪一个git文件。spring.profiles.active决定了使用哪个版本。 其实,这里就是你的拥有一大堆逻辑代码的那个应用。所以这里可以用各种各样的配置文件。当然了,我们推荐你全部都配置在远程端。不然以后修改或者临时需求修改很麻烦。 使用配置: @Value("${foo}") String foo; 这是我使用了自己的配置的方法,如果是spring自己的话,比如数据库配置的datasource等,会直接使用。

优秀的个人博客,低调大师

java B2B2C 源码 springmvc mybatis多租户电子商城系统- Stream重新入队(RabbitMQ)

本文将介绍RabbitMQ的binder提供的重试功能:重新入队 准备一个会消费失败的例子,可以直接沿用前文的工程,也可以新建一个,然后创建如下代码的逻辑: @EnableBinding(TestApplication.TestTopic.class) @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @RestController static class TestController { @Autowired private TestTopic testTopic; /** * 消息生产接口 * * @param message * @return */ @GetMapping("/sendMessage") public String messageWithMQ(@RequestParam String message) { testTopic.output().send(MessageBuilder.withPayload(message).build()); return "ok"; } } /** * 消息消费逻辑 */ @Slf4j @Component static class TestListener { private int count = 1; @StreamListener(TestTopic.INPUT) public void receive(String payload) { log.info("Received payload : " + payload + ", " + count); throw new RuntimeException("Message consumer failed!"); } } interface TestTopic { String OUTPUT = "example-topic-output"; String INPUT = "example-topic-input"; @Output(OUTPUT) MessageChannel output(); @Input(INPUT) SubscribableChannel input(); } } 内容很简单,既包含了消息的生产,也包含了消息消费。消息消费的时候主动抛出了一个异常来模拟消息的消费失败。 在启动应用之前,还要记得配置一下输入输出通道对应的物理目标(exchange或topic名)、并设置一下分组,比如: spring.cloud.stream.bindings.example-topic-input.destination=test-topic spring.cloud.stream.bindings.example-topic-input.group=stream-exception-handler spring.cloud.stream.bindings.example-topic-input.consumer.max-attempts=1 spring.cloud.stream.rabbit.bindings.example-topic-input.consumer.requeue-rejected=true spring.cloud.stream.bindings.example-topic-output.destination=test-topic 完成了上面配置之后,启动应用并访问localhost:8080/sendMessage?message=hello接口来发送一个消息到MQ中了,此时可以看到程序不断的抛出了消息消费异常。这是由于这里我们多加了一个配置:spring.cloud.stream.rabbit.bindings.example-topic-input.consumer.requeue-rejected=true。在该配置作用之下,消息消费失败之后,并不会将该消息抛弃,而是将消息重新放入队列,所以消息的消费逻辑会被重复执行,直到这条消息消费成功为止。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册