Spring Cloud服务治理(Eureka)
Eureka
-
Eureka是Netflix公司开源的一个服务注册与发现的组件(注册中心)
-
Eureka和其他Netflix公司的服务组件(例如负载均衡、熔断器、网关等)一起,被SpringCloud社区整合为Spring-Cloud-NetFlix模块
-
Eureka包含两个组件:Eureka Server(注册中心)和Eureka Client(服务提供者、服务消费者)
Eureka-Server服务方搭建
-
父工程导入依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- spring cloud版本信息 --> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <!-- spring cloud依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
Eureka-Server服务方导入依赖
<!-- eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
编写服务方启动类
@SpringBootApplication @EnableEurekaServer//启用eureakServer,此注解可以把该项目(模块)作为注册中心 public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class,args); } }
-
配置eureka服务方的yml配置文件
server: port: 8761 #默认端口8761 #eureka配置 #eureka一共4部分配置 #1.dashboard:eureka的web控制台配置 #2.server:eureka的服务端配置 #3.client:eureka的客户端配置 #4.instance:eureka的项目实例配置 eureka: instance: hostname: localhost #主机名 client: service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #http://localhost:8761/eureak eureak服务端地址,将来客户端使用该地址和eureak进行通信,不写默认为此值 register-with-eureka: false #是否将自己的路径注册到eureak上,默认为true,一般提供方才需要把自己的路径注册到eureak上 fetch-registry: false #是否需要从eureak中抓取路径,默认为true,一般消费方才需要从eureak中抓取路径
-
测试
Eureka-Provider提供方搭建
-
Eureak-Provider提供方导入依赖
<!-- eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
编写服务方启动类
@EnableEurekaClient//提供方,在新版本中此注解可以省略,但是建议加上 @SpringBootApplication public class ProviderApp { public static void main(String[] args) { SpringApplication.run(ProviderApp.class,args); } }
-
编写提供方yml文件
server: port: 8001 eureka: instance: hostname: localhost #主机名 prefer-ip-address: true #是否将自己的ip注册到eureka中,默认false ip-address: 127.0.0.1 #设置当前实例ip instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} #web控制台显示的实例 ip:应用名称:端口 client: service-url: defaultZone: http://localhost:8761/eureka #localhost:注册中心地址 8761:注册中心端口号 spring: application: name: eureka-provider #设置当前应用名称
-
测试结果
Eureka-Consumer消费方搭建
-
Eureka-Consumer消费方导入依赖
<!-- eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
编写消费方启动类
@EnableDiscoveryClient //激活DiscoveryClient,动态获取 @EnableEurekaClient @SpringBootApplication public class ConsumerApp { public static void main(String[] args) { SpringApplication.run(ConsumerApp.class,args); } }
-
编写消费方yml文件
server: port: 9000 eureka: instance: hostname: localhost #主机名 client: service-url: defaultZone: http://localhost:8761/eureka #localhost:注册中心地址 8761:注册中心端口号 spring: application: name: eureka-consumer #设置当前应用名称
-
动态从eureka server中获取provider的ip和端口
- 步骤
- 注入DiscoveryClient对象并激活
- 调用方法
@RestController @RequestMapping("/order") public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/goods/{id}") public Goods findGoodsById(@PathVariable("id") int id){ /* 动态从eureka server中获取provider的ip和端口 1.注入DiscoveryClient对象并激活 2.调用方法 */ List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");//不能填错,一旦填错发生空指针异常 if (instances==null || instances.size()==0){ return null; } ServiceInstance instance = instances.get(0); String host = instance.getHost();//获取ip int port = instance.getPort();//获取端口 //拼接 String url="http://"+host+":"+port+"/goods/findOne/"+id; Goods goods = restTemplate.getForObject(url, Goods.class); return goods; } }