首页 文章 精选 留言 我的

精选列表

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

java版spring cloud+spring boot 社交电子商务平台-配置中心服务化和高可用

在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。 这篇文章我们基于配置中心git版本的内容来改造 server端改造 1、添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> 需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。 2、配置文件 server: server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git仓库的地址 search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。 username: username # git仓库的账号 password: password # git仓库的密码 eureka: client: serviceUrl: defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址 增加了eureka注册中心的配置 3、启动类 启动类添加@EnableDiscoveryClient激活对注册中心的支持 @EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } 这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问:http://localhost:8000/ 就会看到server端已经注册了到注册中心了。 客户端改造 1、添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。 2、配置文件 spring.application.name=spring-cloud-config-client server.port=8002 spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=spring-cloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ 主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置: spring.cloud.config.discovery.enabled :开启Config服务发现支持 spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值 eureka.client.serviceUrl.defaultZone :指向注册中心的地址 这三个配置文件都需要放到bootstrap.properties的配置中 3、启动类 启动类添加@EnableDiscoveryClient激活对配置中心的支持 @EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } } 启动client端,在浏览器中访问:http://localhost:8000/ 就会看到server端和client端都已经注册了到注册中心了。 高可用 为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。 我们先单独测试服务端,分别访问:http://localhost:8001/neo-config/dev、http://localhost:8003/neo-config/dev返回信息: { "name": "neo-config", "profiles": [ "dev" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", "source": { "neo.hello": "hello im dev" } } ] //社交电商平台源码请加企鹅求求: //一零三八七七四六二六。 } 说明两个server端都正常读取到了配置信息。 再次访问:http://localhost:8002/hello,返回:hello im dev update。说明客户端已经读取到了server端的内容,我们随机停掉一台server端的服务,再次访问http://localhost:8002/hello,返回:hello im dev update,说明达到了高可用的目的。

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

java版b2b2c社交电商spring cloud分布式微服务-服务提供与调用

上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。 案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。 服务提供 我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务 1、pom包配置 创建一个springboot项目,pom.xml中添加如下配置: <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 2、配置文件 application.properties配置如下: spring.application.name=spring-cloud-producer server.port=9000 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ 参数在上一篇都已经解释过,这里不多说。 3、启动类 启动类中添加@EnableDiscoveryClient注解 @SpringBootApplication @EnableDiscoveryClient public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } } 4、controller 提供hello服务 @RestController public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge"; } } 添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。 到此服务提供者配置就完成了。 服务调用 1、pom包配置 和服务提供者一致 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 2、配置文件 application.properties配置如下: spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ 3、启动类 启动类添加@EnableDiscoveryClient和@EnableFeignClients注解。 @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } @EnableDiscoveryClient`:启用服务注册与发现 @EnableFeignClients`:启用feign进行远程调用 Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。 4、feign调用实现 @FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); } name:远程服务名,及spring.application.name配置的名称 此类中的方法和远程服务中contoller中的方法名和参数需保持一致。 5、web层调用远程服务 将HelloRemote注入到controller层,像普通方法一样去调用即可。 @RestController public class ConsumerController { @Autowired HelloRemote HelloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return HelloRemote.hello(name); } } 到此,最简单的一个服务注册与调用的例子就完成了。 测试 简单调用 依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目 先输入:http://localhost:9000/hello?name=neo检查spring-cloud-producer服务是否正常 返回:hello neo,this is first messge 说明spring-cloud-producer正常启动,提供的服务也正常。 浏览器中输入:http://localhost:9001/hello/neo 返回:hello neo,this is first messge 说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。

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

Java程序员面试没带笔记本电脑,直接手写代码 HR:现在可以入职吗?

生活在这个忙碌的社会,我们总是在忙碌中丢三落四。我们会从家出门忘带钥匙,我们会上班路上想起忘带工牌,到公司想起昨天晚上在家加班的资料没拿,甚至上班时起身去打杯水,途中看了下手机回来忘记拿水杯。 最尴尬是也许就在于,去面试吃饭的家伙没带了! 这是一个真实而感人的故事,程序员去公司面试,走到流程需要程序员打一段需求代码。程序员,习惯性回首一掏,这一次他没有掏出笔记本来。HR摇了摇头,内心已经否认了这个程序员:对于程序员来说,电脑是吃饭的家伙,可是他居然不带! HR却并没有声张,坐在那里静静的看着程序员。显然是想看他出丑。程序员,拿出简历,翻至空白处,拿起了HR记录的笔,开始了一段书写。 面试时程序员丝毫不显慌张,自信心爆棚的他,没有提笔忘字的感觉。但写出一段代码并非易事。但HR看出程序员手写代码特别厉害!随便一动笔就是行云流水,一泻千里。写完丢下笔,把纸递过去。HR激动地东北口音暴露出来大喊:老铁,66666 程序员也是一笑,看是妥了。后来HR把程序员送出公司才知道 当年电脑还没有普及,如果想编程,行得通的就是跑网吧去,可上网费贵啊,只能买个笔记本,提起水笔,把代码写好,然后在到网吧里去输、去调试、运行,这样可以节省很多上机的费用。才知道这程序员不一般。 你要是忘记带笔记本你会怎么办呢?对于程序员来说现在用的ecplise,已经省去了很多码代码的时间,同时有大量的词汇不用具体记忆了,但是真要你一个字符一个字符的打出来或者写出来,你做得到吗?或许,你会说没必要,很浪费时间,但是你就少了一项跟领导或客户表(zhuang)现(bi)的资本。

资源下载

更多资源
优质分享App

优质分享App

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

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

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

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码