当前位置:   article > 正文

EureKa服务注册发现与调用_前端能注入eureka吗

前端能注入eureka吗

公司需要对提高项目的响应速度,经研究决定使用eureka微服务改造项目,于是自己做了一个小demo进行学习。

一、eureka简介

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。Eureka采用cs架构:Eureka Server和Eureka Client,即服务端和客户端。

客户端将服务注册到服务端,客户端之间可以通过eureka服务的注册信息进行调用。客户端可以同时注册多个eureka服务,从而实现高可用,客户端可以发布多个到eureka上实现负载均衡。

二、Eureka Serve服务端

1、首先构建一个springboot项目,并在pom.xml中添加依赖

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.itcast</groupId>
  12. <artifactId>eureka</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>eureka</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring-cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. <repositories>
  51. <repository>
  52. <id>spring-milestones</id>
  53. <name>Spring Milestones</name>
  54. <url>https://repo.spring.io/milestone</url>
  55. </repository>
  56. </repositories>
  57. </project>

 2、配置application.yml

  1. server:
  2. port: 8761
  3. eureka:
  4. client:
  5. #是否向服务注册中心注册自己
  6. register-with-eureka: false
  7. #是否启用获取服务注册信息,不需要同步其他的Eureka Server节点的数据,故而设为false
  8. fetch-registry: false

 

3、主函数类中添加注解EurekaApplication

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

4、启动主函数就可以访问eureka注册中心了http://localhost:8761/

 三、Eureka Client客户端

首先创建一个生产者,即服务的提供者,生产者和消费者没有绝对的区分,只是在服务调用的时候扮演的不同角色。

1、首先构建一个springboot项目,并在pom.xml中添加依赖

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.vtits</groupId>
  12. <artifactId>provider</artifactId>
  13. <version>1.0.0-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. </dependency>
  31. </dependencies>
  32. <dependencyManagement>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-dependencies</artifactId>
  37. <version>${spring-cloud.version}</version>
  38. <type>pom</type>
  39. <scope>import</scope>
  40. </dependency>
  41. </dependencies>
  42. </dependencyManagement>
  43. <build>
  44. <defaultGoal>compile</defaultGoal>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. <repositories>
  53. <repository>
  54. <id>spring-milestones</id>
  55. <name>Spring Milestones</name>
  56. <url>https://repo.spring.io/milestone</url>
  57. </repository>
  58. </repositories>
  59. </project>

2、配置application.yml

  1. server:
  2. port: 8762
  3. spring:
  4. application:
  5. name: hello-service
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://localhost:8761/eureka #注册eureka的地址,由于eureka在本地

3、主函数添加注解

  1. @Configuration
  2. @EnableAutoConfiguration
  3. @ComponentScan
  4. public class ProviderApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ProviderApplication.class, args);
  7. }
  8. }

4、添加测试用例

  1. @RestController
  2. public class MainController {
  3. @GetMapping("/test/{name}")
  4. String home(@PathVariable("name") String t) {
  5. return "Hello World!"+t;
  6. }
  7. }

5、启动主函数打开eureka注册中心

看到我们的服务注册到eureka服务上了。

访问http://localhost:8762/test/JAVA

创建一个服务消费者

6、构建一个springboot项目,并在pom.xml中添加依赖

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.vtits</groupId>
  12. <artifactId>provider</artifactId>
  13. <version>1.0.0-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-openfeign</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. </dependencies>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-dependencies</artifactId>
  45. <version>${spring-cloud.version}</version>
  46. <type>pom</type>
  47. <scope>import</scope>
  48. </dependency>
  49. </dependencies>
  50. </dependencyManagement>
  51. <build>
  52. <defaultGoal>compile</defaultGoal>
  53. <plugins>
  54. <plugin>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-maven-plugin</artifactId>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. <repositories>
  61. <repository>
  62. <id>spring-milestones</id>
  63. <name>Spring Milestones</name>
  64. <url>https://repo.spring.io/milestone</url>
  65. </repository>
  66. </repositories>
  67. </project>

 7、主函数添加注解

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

8、配置application.yml

  1. server:
  2. port: 8763
  3. spring:
  4. application:
  5. name: consumer
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://localhost:8761/eureka

9、创建ServiceFeignClient类,作为服务调用接口

  1. //name为生产者提供的服务名称
  2. @FeignClient(name = "hello-service" )
  3. public interface ServiceFeignClient {
  4. //这里接口要与被生产者home一致
  5. @GetMapping("/test/{name}")
  6. String home(@PathVariable("name") String name);
  7. }

10、编写ConsumerController,调用ServiceFeignClient

  1. @RestController
  2. public class ConsumerController {
  3. @Autowired
  4. ServiceFeignClient serviceFeignClient;
  5. @GetMapping("/test1/{name}")
  6. public String index(@PathVariable("name") String name) {
  7. return serviceFeignClient.home(name);
  8. }
  9. }

11、启动主函数打开eureka注册中心

12、访问http://localhost:8763/test1/JDK

这样消费者就从eureka服务调用到了eureka生产者的服务。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/1000354
推荐阅读
相关标签
  

闽ICP备14008679号