Spring Cloud Eureka 服务注册与发现中心(一)

太过爱你忘了你带给我的痛 2022-06-05 09:12 373阅读 0赞

Spring Cloud 项目中的子项目,Eureka 一个注重于AP的服务注册与发现服务,对比Zookeeper的集群,注重高可用与分区容错性。

如图:

![Image 1][]![Image 1][]

  • Eureka-discovery 2个服务互为伙伴,增加高可用性;
  • Business-Service 多个业务服务想discovery进行注册,互为伙伴的discovery 会自动同步节点下注册的服务;
  • discovery-Client 像discovery注册的同时,获取注册服务列表,通过Ribbon进行自身软负载

相关代码:

1.总体父工程pom

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.test.cloud</groupId>
  5. <artifactId>test-cloud-parent</artifactId>
  6. <version>1.0.RELEASE</version>
  7. <packaging>pom</packaging>
  8. <name>test-cloud-parent</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.5.6.RELEASE</version>
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-dependencies</artifactId>
  25. <version>Edgware.RC1</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. </dependencies>
  30. </dependencyManagement>
  31. <build>
  32. <pluginManagement>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. </plugin>
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-surefire-plugin</artifactId>
  41. <version>2.12.4</version>
  42. <configuration>
  43. <skipTests>true</skipTests>
  44. </configuration>
  45. </plugin>
  46. </plugins>
  47. </pluginManagement>
  48. </build>
  49. </project>

2.Eureka-discovery相关POM代码

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.test.cloud</groupId>
  5. <artifactId>test-cloud-discovery</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>test-cloud-discovery</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>com.test.cloud</groupId>
  12. <artifactId>test-cloud-parent</artifactId>
  13. <version>1.0.RELEASE</version>
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. <scope>test</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <plugins>
  31. <plugin>
  32. <groupId>org.apache.maven.plugins</groupId>
  33. <artifactId>maven-compiler-plugin</artifactId>
  34. <configuration>
  35. <source>1.8</source>
  36. <target>1.8</target>
  37. <encoding>UTF-8</encoding>
  38. </configuration>
  39. </plugin>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. <executions>
  44. <execution>
  45. <goals>
  46. <goal>repackage</goal>
  47. </goals>
  48. </execution>
  49. </executions>
  50. <configuration>
  51. <mainClass>com.hytera.test.cloud.App</mainClass>
  52. <fork>true</fork>
  53. </configuration>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

3.discovery的入口程序

  1. package com.test.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * Hello world!
  7. *
  8. * @author Administrator
  9. */
  10. @SpringBootApplication
  11. @EnableEurekaServer
  12. public class App
  13. {
  14. public static void main( String[] args )
  15. {
  16. SpringApplication.run(App.class, args);
  17. }
  18. }

4.discovery相关配置application.yml

  1. server:
  2. port: 9080
  3. eureka:
  4. client:
  5. g-zip-content: true
  6. service-url:
  7. defaultZone: http://IP:${server.port}/eureka/
  8. fetch-registry: false
  9. register-with-eureka: false
  10. server:
  11. enable-self-preservation: false
  12. eviction-interval-timer-in-ms: 1000
  13. spring:
  14. application:
  15. name: Eureka-discovery-0

这里为注册中心的一个节点,如需要配置伙伴,defaultZone请配置对端IP及端口,

  1. fetch-registry: true

5.business-server pom

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.test.cloud</groupId>
  5. <artifactId>test-cloud-server</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>test-cloud-server</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>com.test.cloud</groupId>
  12. <artifactId>test-cloud-parent</artifactId>
  13. <version>1.0.RELEASE</version>
  14. </parent>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-actuator</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-eureka</artifactId>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.apache.maven.plugins</groupId>
  34. <artifactId>maven-compiler-plugin</artifactId>
  35. <configuration>
  36. <source>1.8</source>
  37. <target>1.8</target>
  38. <encoding>UTF-8</encoding>
  39. </configuration>
  40. </plugin>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. <executions>
  45. <execution>
  46. <goals>
  47. <goal>repackage</goal>
  48. </goals>
  49. </execution>
  50. </executions>
  51. <configuration>
  52. <mainClass>com.hytera.test.cloud.App</mainClass>
  53. <fork>true</fork>
  54. </configuration>
  55. </plugin>
  56. </plugins>
  57. </build>
  58. </project>

6.business-server 配置

  1. server:
  2. port: 9070
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://IP:${server.port}/eureka/
  7. instance:
  8. #instance-id: ${spring.application.name}:${server.port}
  9. instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  10. prefer-ip-address: true
  11. lease-expiration-duration-in-seconds: 15
  12. lease-renewal-interval-in-seconds: 5
  13. spring:
  14. application:
  15. name: Eureka-Server

7.business-server 入口类

  1. package com.test.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. /**
  6. * Hello world!
  7. *
  8. * @author Administrator
  9. */
  10. @SpringBootApplication
  11. @EnableDiscoveryClient
  12. public class App
  13. {
  14. public static void main( String[] args )
  15. {
  16. SpringApplication.run(App.class, args);
  17. }
  18. }

8.business-server 服务生产类

  1. package com.test.cloud.controller;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.client.ServiceInstance;
  6. import org.springframework.cloud.client.discovery.DiscoveryClient;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. /**
  10. *
  11. * @author Administrator
  12. * @date 2017/11/23
  13. */
  14. @RestController
  15. public class MessageController {
  16. private static final Logger LOGGER = LoggerFactory.getLogger(MessageController.class);
  17. @Autowired
  18. private DiscoveryClient client;
  19. @RequestMapping("/getMsgInfo")
  20. public String getMessageInfo(String strGuid, String strUserName){
  21. String strMsg = strGuid + strUserName;
  22. ServiceInstance instance = client.getLocalServiceInstance();
  23. LOGGER.info("uri={},serviceId={},strMsg={}", instance.getUri(), instance.getServiceId(), strMsg);
  24. return strMsg;
  25. }
  26. }

9.discovery-client pom

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.test.cloud</groupId>
  5. <artifactId>test-cloud-client</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>test-cloud-client</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>com.test.cloud</groupId>
  12. <artifactId>test-cloud-parent</artifactId>
  13. <version>1.0.RELEASE</version>
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. <scope>test</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-eureka</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-boot-starter-actuator</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-ribbon</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-feign</artifactId>
  39. </dependency>
  40. </dependencies>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.apache.maven.plugins</groupId>
  45. <artifactId>maven-compiler-plugin</artifactId>
  46. <configuration>
  47. <source>1.8</source>
  48. <target>1.8</target>
  49. <encoding>UTF-8</encoding>
  50. </configuration>
  51. </plugin>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. <executions>
  56. <execution>
  57. <goals>
  58. <goal>repackage</goal>
  59. </goals>
  60. </execution>
  61. </executions>
  62. <configuration>
  63. <mainClass>com.hytera.test.cloud.App</mainClass>
  64. <fork>true</fork>
  65. </configuration>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. </project>

10.discovery-client 配置

  1. server:
  2. port: 9061
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone : http://192.168.72.30:9080/eureka/
  7. instance:
  8. prefer-ip-address: true
  9. lease-expiration-duration-in-seconds: 15
  10. lease-renewal-interval-in-seconds: 5
  11. #instance-id: ${spring.application.name}:${server.port}
  12. instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  13. spring:
  14. application:
  15. name: cloudclient

11.discovery-client 入口

  1. package com.test.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. /**
  9. * Hello world!
  10. * @author Administrator
  11. */
  12. @SpringBootApplication
  13. @EnableDiscoveryClient
  14. public class App {
  15. @Bean
  16. @LoadBalanced
  17. RestTemplate restTemplate() {
  18. return new RestTemplate();
  19. }
  20. public static void main(String[] args) {
  21. SpringApplication.run(App.class, args);
  22. }
  23. }

12.discovery-client 服务消费类

  1. package com.test.cloud;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. *
  9. * @author Administrator
  10. * @date 2017/11/22
  11. */
  12. @RestController
  13. public class ConsumerController {
  14. @Autowired
  15. private RestTemplate restTemplate;
  16. @RequestMapping(value = "/add", method = RequestMethod.GET)
  17. public String add() {
  18. return restTemplate.getForEntity("http://Eureka-Server/getMsgInfo?strGuid=10&strUserName=20", String.class).getBody();
  19. }
  20. }

全部代码完成。

访问discovery-client ,会使用自身软负载功能进行选择server。

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,373人围观)

还没有评论,来说两句吧...

相关阅读