本文共 9302 字,大约阅读时间需要 31 分钟。
2019年6月,当前最新版Greenwich.SR1发布。
参考官方文档我们新建一个普通的Maven项目,项目名称cloud,对应的pom.xml文件说明如下。
该pom文件作为父级pom文件,起到依赖版本控制的作用,其他Module模块均继承该pom。
4.0.0 com.cntaiping.tpa cloud 1.0-SNAPSHOT pom cloud Demo project for Spring Boot UTF-8 UTF-8 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE register-server producer consumer org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
我们需要在cloud项目中新建3个Module模块,服务注册中心(register-server)、服务生产者(producer)和服务消费者(consumer)
新建Module模块的基本方法:
选择Maven项目cloud,右键单击–>New–>Module–>选择spring initialir 也就是新建一个SpringBoot模块。新建服务注册中心模块(register-server),也是一个SpingBoot模块。
修改对应的pom文件,内容如下,继承父级pom文件。
4.0.0 com.cntaiping.tpa.cloud register-server 0.0.1-SNAPSHOT jar register-server Demo project for Spring Boot com.cntaiping.tpa cloud 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-server
#注册中心服务IDspring.application.name=register-server#端口号server.port=8800# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。# 由于当前这个应用就是Eureka Server,故而设为falseeureka.client.register-with-eureka=false# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,# 不需要同步其他的Eureka Server节点的数据,故而设为false。eureka.client.fetch-registry=false# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
需要指明spring.application.name
参数,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name。
需要添加一个注解@EnableEurekaServer
package com.cntaiping.tpa.cloud.registerserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class RegisterServerApplication { public static void main(String[] args) { SpringApplication.run(RegisterServerApplication.class, args); }}
No application available
表示没有服务被发现, 因为现在还没有服务提供者进行注册服务。
新建服务生产者模块(producer)。服务的提供方可以看做是服务注册中心的客户端,所以需要引入spring-cloud-starter-netflix-eureka-client。
修改对应的pom文件,内容如下。
4.0.0 com.cntaiping.tpa producer 0.0.1-SNAPSHOT jar producer Demo project for Spring Boot com.cntaiping.tpa cloud 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web
#服务名称spring.application.name=producer#端口号server.port=8700#在注册中心中进行注册eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/#启动服务发现的功能,开启了才能调用其它服务spring.cloud.config.discovery.enabled=true#发现的服务的名字--对应注测中心的服务名字spring.cloud.config.discovery.serviceId=register-server
需要添加一个@EnableEurekaClient注解 表明自己是一个eurekaclient
package com.cntaiping.tpa.producer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); }}
服务生产者提供的具体服务如下,这里只是一个简单例子,返回一个字符串。
package com.cntaiping.tpa.producer.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MessageController { @Value("${server.port}") String port; @GetMapping("/get") public String getMessage(@RequestParam("name")String name){ return "Hi " + name + " ,I am from port:" + port; }}
当服务提供方任务繁重时,可以多起几个该服务的实例,通过负载均衡来解决。
这里我们通过多端口来启动服务生产者producer模块的多个实例。第一步:在IntelliJ IDEA右上角点击XXXXApplication右边的下三角,弹出下拉框,点击“Edit Configuration…”
实例配置:将默认的Single instance only(单实例)的钩去掉,VM options中添加“-Dserver.port=8700”,表示将从8700端口启动一个实例。另一个实例端口修改为8701即可。
分别运行服务生产者模块(producer)的两个实例,效果如下。
同样新建一个服务消费者模块,名称是consumer。
修改pom文件内容如下。
4.0.0 com.cntaiping.tpa consumer 0.0.1-SNAPSHOT jar consumer Demo project for Spring Boot com.cntaiping.tpa cloud 1.0-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-ribbon
Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。这里我们采用ribbon+restTemplate方式,pom文件中增加了spring-cloud-starter-netflix-ribbon依赖。
这里配置文件采用yml格式,服务消费者模块的端口号是 8600。
eureka: client: serviceUrl: defaultZone: http://localhost:8800/eureka/server: port: 8600spring: application: name: consumer
相同点:@EnableDiscoveryClient和@EnableEurekaClient都是能够让注册中心能够发现,扫描到改服务。
不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。从Spring Cloud Edgware开始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
package com.cntaiping.tpa.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @LoadBalanced //使用负载均衡机制 @Bean public RestTemplate restTemplate(){ return new RestTemplate(); }}
服务消费类如下,通过Spring DI注入的restTemplate来消费producer的“/get”接口
package com.cntaiping.tpa.consumer.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class MessageController { @Autowired RestTemplate restTemplate; @GetMapping("/show") public String showMessage(@RequestParam String name){ return restTemplate.getForObject("http://producer/get?name="+name, String.class); }}
转载地址:http://egvab.baihongyu.com/