2018-09-03 15:51:53.0|分类: spring cloud|浏览量: 3073
服务发现组件单独拿出来 服务发现组件有consul、zookeeper、eureka等,下面列出优缺点
eureka官方架构图 application service 服务提供者 application client 服务消费者 eureka组件有:eureka server和eureka client eureka server项目结构如下图: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cookqq</groupId> <artifactId>euraka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <name>euraka</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> application.yml server: port: 8761 # 指定该Eureka实例的端口 eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/ 启动类 package com.cookqq.euraka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * *eureka注册中心 */ @SpringBootApplication @EnableEurekaServer public class EurakaApp { public static void main( String[] args ) { SpringApplication.run(EurakaApp.class, args); } } 启动类,访问浏览器http://localhost:8761/ eureka client集成过程 provider-user/consumer-order配置eureka client pom.xml增加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> application.yml增加 spring: application: name: provider-user ... eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true 改变订单控制层代码OrderController.java package com.cookqq.consumer.controller; import java.util.List; import java.util.Random; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.cookqq.consumer.bean.User; @RestController public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/user/{id}") public User findUserById(@PathVariable Long id){ // return this.restTemplate.getForObject("http://127.0.0.1:8000/"+id, User.class); List<ServiceInstance> listInfo = discoveryClient.getInstances("provider-user"); if(listInfo.size()>0){ return this.restTemplate.getForObject( listInfo.get(new Random().nextInt(listInfo.size())).getUri()+"/"+id, User.class); } return null; } } 启动所有项目,访问浏览器 |