1.Spring Cloud Eureka的作用(1)建立Eureka服务端(也称为注册中心)我使用的springboot的版本是2.0.5.RELEASE版本① 添加依赖在书中引入的是spring-cloud-eureka-server,但是当我引入这个依赖的时候,找不到@EnableEurekeServer<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency></dependencies><dependencyManagement> <dependencies> <!– https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies –> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>② 配置application-peer1.properties和application-peer2.properties文件以下是application-peer1.properties的配置:server.port=8001spring.application.name=eureka-server#eurekaeureka.instance.hostname=127.0.0.1eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.service-url.defaultZone=http://127.0.0.1:8002/eureka/eureka.instance.prefer-ip-address=true以下是application-peer2.properties的配置server.port=8002spring.application.name=eureka-server#eurekaeureka.instance.hostname=127.0.0.1eureka.client.register-with-eureka=falseeureka.client.fetch-registry=falseeureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/eureka.instance.prefer-ip-address=true③ 建立启动文件,添加@EnableEurekaServer注解@ComponentScan(basePackages = “gdut.ff”)@EnableAutoConfiguration@EnableEurekaServerpublic class Main { public static void main(String args[]) { SpringApplication.run(Main.class, args); }}④ 启动,指定使用的配置文件以上就建立了一个注册中心集群。⑤ 演示效果(2)建立Eureka客户端(也称为服务提供方)① 添加依赖<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency></dependencies><dependencyManagement> <dependencies> <!– https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies –> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>② 配置application.propertiesserver.port=9001spring.application.name=hello-serviceeureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/eureka.instance.prefer-ip-address=true③ 提供服务@RestController@RequestMapping("/hello")public class HelloController { private final Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired private DiscoveryClient discoveryClient; @RequestMapping(value = “/index”,method = RequestMethod.GET) public String index() { String serviceId = “hello-service”; List<ServiceInstance> instance = discoveryClient.getInstances(serviceId); if(null != instance) { for(int i = 0;i < instance.size();i++) { System.out.println(instance.get(i).getHost() + “:” + instance.get(i).getPort()); } } return “Hello,world”; }}④ 建立启动文件,添加EnableDiscoveryClient注解@ComponentScan(basePackages = “gdut.ff”)@EnableAutoConfiguration@EnableDiscoveryClientpublic class Main { public static void main(String args[]) { SpringApplication.run(Main.class, args); }}⑤ 启动,指定使用的端口启动Main文件时,指定–server.port=xxx访问http://ip:port/hello/index(3)建立ribbon-consumer(也称为服务调用方)① 添加依赖<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency></dependencies><dependencyManagement> <dependencies> <!– https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies –> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>② 配置application.propertiesserver.port=7001spring.application.name=ribbon-consumereureka.client.service-url.defaultZone=http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/eureka.instance.prefer-ip-address=true③ 获取服务@RestControllerpublic class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = “/ribbon-consumer”, method= RequestMethod.GET) public String helloConsumer() { return restTemplate.getForEntity(“http://hello-service/hello/index”,String.class).getBody(); }}④ 建立启动文件,添加EnableDiscoveryClient注解@ComponentScan(basePackages = “gdut.ff”)@EnableAutoConfiguration@EnableDiscoveryClientpublic class Main { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String args[]) { SpringApplication.run(Main.class, args); }}⑤ 启动访问http://ip:port/ribbon-consumery示例代码