Spring Cloud Hoxton.SR1 实战 之 Eureka

前期准备:Maven、JDK1.8

1. 在spring官网创建一个spring boot项目,带Eureka组件

https://start.spring.io/

Spring Cloud Hoxton.SR1 实战 之 Eureka_第1张图片

 

点击“Generate”按钮下载初始化工程的压缩包demo-eureka.zip,然后解压缩到eclipse的workspace。

2. Eclipse中import existing Maven Project

Spring Cloud Hoxton.SR1 实战 之 Eureka_第2张图片

 

3. 修改pom.xml,因为有错误 Maven Configuration Problem

4. 保存更改后,右击工程-》Maven-》Update project...更新maven依赖

5. 将/demo-eureka/src/main/resources下的application.properties更改为application.yml,并填入Eureka配置:

spring:
  application:
    name: demo-eureka

server:
  port: 8761
  
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      

6. 在DemoEurekaApplication中添加注解,并import这个类的包。

@EnableEurekaServer

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

package com.michael.cloud.demoeureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DemoEurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoEurekaApplication.class, args);
	}

}

7. Run as Spring Boot App -》DemoEurekaApplication

Spring Cloud Hoxton.SR1 实战 之 Eureka_第3张图片2019-12-26 21:05:28.341  INFO 9600 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2019-12-26 21:05:29.512  INFO 9600 --- [           main] c.m.c.demoeureka.DemoEurekaApplication   : Started DemoEurekaApplication in 11.272 seconds (JVM running for 12.169)

这两句log说明端口已经指向了application.xml中配置的8761, Eureka服务已经启动

8. 登录注册中心: http://localhost:8761/

 Spring Cloud Hoxton.SR1 实战 之 Eureka_第4张图片

9. 创建一个Eureka Client工程

Spring Cloud Hoxton.SR1 实战 之 Eureka_第5张图片

10. 同Eureka server的步骤,解压导入后,配置pom.xml

Spring Cloud Hoxton.SR1 实战 之 Eureka_第6张图片

	
		1.8
		Hoxton.SR1
		3.0.0
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

		
            org.springframework.boot
            spring-boot-starter-web
        
        

11. 保存更改后,右击工程-》Maven-》Update project...更新maven依赖

12. 编写application.yml如下

server:
  port: 7761


spring:
  application:
    name: demo-eureka-client1


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

13. 编辑DemoEurekaClient1Application

package com.michael.cloud.demoeurekaclient1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class DemoEurekaClient1Application {

	public static void main(String[] args) {
		SpringApplication.run(DemoEurekaClient1Application.class, args);
	}

	@RequestMapping("hi")
	public String helloDemoEurekaClient1(){
		return "hello Demo Eureka Client1";
	}
	
}

14. 启动Eureka Client: DemoEurekaClient1Application

看到下面的log,说明启动成功

2019-12-26 21:51:53.844  INFO 10936 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_DEMO-EUREKA-CLIENT1/OG64OJN3S45ZZO2:demo-eureka-client1:7761 - registration status: 204
2019-12-26 21:51:54.986  INFO 10936 --- [           main] c.m.c.d.DemoEurekaClient1Application     : Started DemoEurekaClient1Application in 10.296 seconds (JVM running for 11.129)

15. 刷新Eureka控制台http://localhost:8761/,会看到Client已经注册成功

Spring Cloud Hoxton.SR1 实战 之 Eureka_第7张图片

16. 访问client1

17. 复制client1的工程,命名为demo-eureka-client2

Spring Cloud Hoxton.SR1 实战 之 Eureka_第8张图片

18. 将包名、类名都改为client2

Spring Cloud Hoxton.SR1 实战 之 Eureka_第9张图片

19. 修改application.yml

Spring Cloud Hoxton.SR1 实战 之 Eureka_第10张图片

20. 启动DemoEurekaClient2Application

看到log即成功

2019-12-26 22:05:46.864  INFO 9852 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7762
2019-12-26 22:05:48.061  INFO 9852 --- [           main] c.m.c.d.DemoEurekaClient2Application     : Started DemoEurekaClient2Application in 10.455 seconds (JVM running for 11.3)

21. 查看Eureka控制台,client2注册成功

Spring Cloud Hoxton.SR1 实战 之 Eureka_第11张图片

22. 访问client2

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Spring,Cloud)