Eureka就是帮助我们维护所有服务的信息,以便服务之间的相互调用
image.png

1 Eureka的快速入门

1.1 创建EurekaServer

1.1.1 创建一个父工程,并且在父工程中指定SpringCloud的版本,并且将packaing修改为pom

  1. <packaging>pom</packaging>
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.3.12.RELEASE</version>
  6. <relativePath/> <!-- lookup parent from repository -->
  7. </parent>
  8. <dependencyManagement>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-dependencies</artifactId>
  13. <version>Hoxton.SR12</version>
  14. <type>pom</type>
  15. <scope>import</scope>
  16. </dependency>
  17. </dependencies>
  18. </dependencyManagement>

创建eureka的server,创建SpringBoot工程,并且导入依赖,在启动类中添加注解,编写yml文件
1.1.2 导入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. </dependencies>

1.1.3 启动类添加注解

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaApplication.class,args);
  6. }
  7. }

1.1.4 编写yml

  1. server:
  2. port: 8761 # 端口号
  3. eureka:
  4. instance:
  5. hostname: localhost # localhost
  6. client:
  7. # 当前的eureka服务是单机版的
  8. registerWithEureka: false
  9. fetchRegistry: false
  10. serviceUrl:
  11. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.2 创建EurekaClient

创建Maven工程,修改为SpringBoot
1.2.1 导入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

1.2.2
在启动类上添加注解

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class CustomerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(CustomerApplication.class,args);
  6. }
  7. }

1.2.3 编写配置文件

  1. # 指定Eureka服务地址
  2. eureka:
  3. client:
  4. service-url:
  5. defaultZone: http://localhost:8761/eureka
  6. # 指定服务的名称
  7. spring:
  8. application:
  9. name: CUSTOMER

1.3 测试Eureka
  1. @Autowired
  2. private EurekaClient eurekaClient;
  3. @GetMapping("/customer")
  4. public String customer(){
  5. //1. 通过eurekaClient获取到SEARCH服务的信息
  6. InstanceInfo info = eurekaClient.getNextServerFromEureka("SEARCH", false);
  7. //2. 获取到访问的地址
  8. String url = info.getHomePageUrl();
  9. System.out.println(url);
  10. //3. 通过restTemplate访问
  11. String result = restTemplate.getForObject(url + "/search", String.class);
  12. //4. 返回
  13. return result;
  14. }

1.4 启动类中配置(各人配置不一样) RestTemplate

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class UserApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(UserApplication.class,args);
  6. }
  7. @Bean
  8. public RestTemplate restTemplate(){
  9. return new RestTemplate();
  10. }
  11. }