springcloud的eureka集群

末蓝、 2021-12-10 03:49 373阅读 0赞

使用的版本

springcloud版本:Greenwich.SR1
springboot 版本:2.1.6.RELEASE

项目目录

在这里插入图片描述

eureka集群 peer1

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" 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. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.1.6.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <groupId>com.wuk</groupId>
  11. <artifactId>movie-service-eureka-peer1</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>movie-service-eureka-peer1</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. </dependencies>
  30. <dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-dependencies</artifactId>
  35. <version>${spring-cloud.version}</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. </dependencies>
  40. </dependencyManagement>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

application.yml

  1. server:
  2. port: 8761
  3. spring:
  4. application:
  5. name: peer1
  6. eureka:
  7. client:
  8. register-with-eureka: false #是否注册到eureka上
  9. fetch-registry: true #获取注册表(eureka集群时候用到)
  10. service-url:
  11. defaultZone: http://peer2:8762/eureka #配置eureka的server的注册地址
  12. instance:
  13. hostname: peer1

启动类

  1. package com.wuk;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer
  6. @SpringBootApplication
  7. public class MovieServiceEurekaPeer1Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(MovieServiceEurekaPeer1Application.class, args);
  10. }
  11. }

eureka集群 peer2

application.yml

  1. server:
  2. port: 8762
  3. spring:
  4. application:
  5. name: peer2
  6. eureka:
  7. client:
  8. register-with-eureka: false #是否注册到eureka上
  9. fetch-registry: true #获取注册表(eureka集群时候用到)
  10. service-url:
  11. defaultZone: http://peer1:8761/eureka #配置eureka的server的注册地址
  12. instance:
  13. hostname: peer2

其他的文件都和peer1一致。
注意:
要在host里面配置peer1和peer2
C:\Windows\System32\drivers\etc\hosts

  1. 127.0.0.1 peer1 peer2

服务者 movie-service-user

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" 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. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.1.6.RELEASE</version>
  8. <!-- <version>1.4.3.RELEASE</version> -->
  9. <relativePath /> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.wuk</groupId>
  12. <artifactId>movie-service-user</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>movie-service-user</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-jpa</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-actuator</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>mysql</groupId>
  35. <artifactId>mysql-connector-java</artifactId>
  36. <version>5.1.47</version>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-devtools</artifactId>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.projectlombok</groupId>
  50. <artifactId>lombok</artifactId>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework.cloud</groupId>
  54. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  55. </dependency>
  56. <dependency>
  57. <groupId>com.zaxxer</groupId>
  58. <artifactId>HikariCP</artifactId>
  59. <version>3.3.1</version>
  60. </dependency>
  61. </dependencies>
  62. <dependencyManagement>
  63. <dependencies>
  64. <dependency>
  65. <groupId>org.springframework.cloud</groupId>
  66. <artifactId>spring-cloud-dependencies</artifactId>
  67. <version>${spring-cloud.version}</version>
  68. <type>pom</type>
  69. <scope>import</scope>
  70. </dependency>
  71. </dependencies>
  72. </dependencyManagement>
  73. <build>
  74. <plugins>
  75. <plugin>
  76. <groupId>org.springframework.boot</groupId>
  77. <artifactId>spring-boot-maven-plugin</artifactId>
  78. </plugin>
  79. </plugins>
  80. </build>
  81. </project>

application.yml

  1. server:
  2. port: 8000
  3. spring:
  4. application:
  5. name: movie-service-user
  6. jpa:
  7. show-sql: true
  8. hibernate:
  9. ddl-auto: none
  10. database: MYSQL
  11. datasource:
  12. url: jdbc:mysql://127.0.0.1:3306/movieservice
  13. username: root
  14. password: root
  15. driver-class-name: com.mysql.jdbc.Driver
  16. type: com.zaxxer.hikari.HikariDataSource
  17. hikari:
  18. minimum-idle: 20
  19. maximum-pool-size: 60
  20. max-lifetime: 180000
  21. auto-commit: true
  22. idle-timeout: 30000
  23. pool-name: DatebookHikariCP
  24. connection-test-query: SELECT 1
  25. connection-timeout: 30000
  26. #schema: classpath:sql/user.sql
  27. #data: classpath:sql/data.sql
  28. logging:
  29. level:
  30. root: info
  31. com.wuk: debug
  32. eureka:
  33. client:
  34. service-url:
  35. defaultZone: http://peer1:8761/eureka, http://peer2:8762/eureka
  36. instance:
  37. prefer-ip-address: true #表示将自己的ip注册到Eureka上面
  38. instance-id: movie-service-user:8000

启动类

  1. package com.wuk;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @EnableEurekaClient
  6. @SpringBootApplication
  7. public class MovieServiceUserApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(MovieServiceUserApplication.class, args);
  10. }
  11. }

User.ajva

  1. package com.wuk.domain;
  2. import java.io.Serializable;
  3. import java.math.BigDecimal;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  10. import lombok.Data;
  11. @Data
  12. @Entity
  13. @JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
  14. public class User implements Serializable{
  15. @Id
  16. @GeneratedValue(strategy=GenerationType.AUTO)
  17. private Long id;
  18. @Column
  19. private String username;
  20. @Column
  21. private String name;
  22. @Column
  23. private Integer age;
  24. @Column
  25. private BigDecimal balance;
  26. }

注意:
如果访问报错:
No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
加上如下注解
@JsonIgnoreProperties(value = { “hibernateLazyInitializer”, “handler” })

UserController.java

  1. package com.wuk.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.wuk.dao.UserRepository;
  7. import com.wuk.domain.User;
  8. import lombok.extern.slf4j.Slf4j;
  9. @Slf4j
  10. @RestController
  11. public class UserController {
  12. @Autowired
  13. private UserRepository userRepository;
  14. @GetMapping("/{id}")
  15. public User findById(@PathVariable Long id) {
  16. User user = userRepository.getOne(id);
  17. log.info("user={}",user);
  18. return user;
  19. }
  20. }

UserRepository.java

  1. package com.wuk.dao;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. import com.wuk.domain.User;
  4. public interface UserRepository extends JpaRepository<User, Long>{
  5. }

消费者

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.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.wuk</groupId>
  12. <artifactId>movie-service-movie</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>movie-service-movie</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</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. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-devtools</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>com.zaxxer</groupId>
  44. <artifactId>HikariCP</artifactId>
  45. <version>3.3.1</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.cloud</groupId>
  49. <artifactId>spring-cloud-netflix-ribbon</artifactId>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework.cloud</groupId>
  53. <artifactId>spring-cloud-starter-config</artifactId>
  54. </dependency>
  55. </dependencies>
  56. <dependencyManagement>
  57. <dependencies>
  58. <dependency>
  59. <groupId>org.springframework.cloud</groupId>
  60. <artifactId>spring-cloud-dependencies</artifactId>
  61. <version>${ spring-cloud.version}</version>
  62. <type>pom</type>
  63. <scope>import</scope>
  64. </dependency>
  65. </dependencies>
  66. </dependencyManagement>
  67. <build>
  68. <plugins>
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. </plugin>
  73. </plugins>
  74. </build>
  75. </project>

注意这两个特别重要
在这里插入图片描述

application.yml

  1. server:
  2. port: 8010
  3. spring:
  4. application:
  5. name: movie-service-movie
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://peer1:8761/eureka, http://peer2:8762/eureka
  10. instance:
  11. prefer-ip-address: true #表示将自己的ip注册到Eureka上面
  12. instance-id: movie-service-movie:8010

ConfigBean.java

  1. package com.wuk.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. @Configuration
  7. public class ConfigBean {
  8. /** * RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集 */
  9. @Bean
  10. @LoadBalanced//获取rest时候加入Ribbon的配置
  11. public RestTemplate getRestTemplate() {
  12. return new RestTemplate();
  13. }
  14. }

UserController.java

  1. package com.wuk.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. import com.wuk.domain.User;
  8. import lombok.extern.slf4j.Slf4j;
  9. @Slf4j
  10. @RestController
  11. public class UserController {
  12. @Autowired
  13. private RestTemplate restTemplate;
  14. private String URL="http://movie-service-user:8000";
  15. @GetMapping("/{id}")
  16. public User findById(@PathVariable Long id) {
  17. log.info(URL+"/1");
  18. User user=restTemplate.getForObject(URL+"/1", User.class);
  19. return user;
  20. }
  21. }

请求访问:

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 SpringCloud-eurekademo

    eureka的工作方式、原理: •会保存各个注册进来的微服务的基本信息:微服务名、ip、端口等 •eureka还会监控各个微服务的运行情况,做一个基本的监控,每隔30