当前位置:   article > 正文

Spring Cloud Eureka的使用_euruka运行前需要先运行tomcat吗

euruka运行前需要先运行tomcat吗

eureka是个什么东西呢?它是一个服务注册中心。就拿电商系统的例子来说,如果要查看会员的订单详情,那么就要在会员系统的tomcat里面调用订单系统的tomcat里的方法。那么直接通过接口访问吗?显然这是不安全的。因此我们需要一个统一管理远程RPC调用的注册中心。

搭建一个注册中心服务端

 1、 创建一个maven工程命名为xx-cloud-eureka-server(不会创建的参考:http://www.sucai66.com/article/detail/20200605/13.html

 2、 在pom.xml增加依赖

  1. <parent>
  2. <artifactId>bg-cloud</artifactId>
  3. <groupId>com.bg</groupId>
  4. <version>1.0-SNAPSHOT</version>
  5. </parent>
  6. <properties>
  7. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  8. <maven.compiler.source>1.7</maven.compiler.source>
  9. <maven.compiler.target>1.7</maven.compiler.target>
  10. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  11. </properties>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  15. </dependency><dependencyManagement>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-dependencies</artifactId>
  20. <version>${spring-cloud.version}</version>
  21. <type>pom</type>
  22. <scope>import</scope>
  23. </dependency>
  24. </dependencies>
  25. </dependencyManagement>

3、 接着创建一个启动类

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

4、 编写配置文件application.yml

  1. # 本身是注册中心,但是也需要注册,这里可以向自己注册
  2. server:
  3. port: 9000
  4. eureka:
  5. instance:
  6. prefer-ip-address: true
  7. hostname: localhost
  8. client:
  9. service-url:
  10. defaultZone: http://http://${eureka.instance.hostname}:${server.port}/eureka/
  11. # 不在页面上显示当前 服务
  12. register-with-eureka: false
  13. # 为true时,可以启动,但报异常:Cannot execute request on any known server
  14. fetch-registry: false
  15. # 服务名 ,很重要 ,后期 config 配置中心会以 服务名为标识找对应的 服务
  16. spring:
  17. application:
  18. name: eureka-server
  19. # 设置当前项目端口号

5、 最后启动EurekaServerApplication,访问http://localhost:9000/就可以打开管理页面了。

搭建服务提供者

1、 创建一个maven工程命名为xx-cloud-eureka-client(不会创建的参考:http://www.sucai66.com/article/detail/20200605/13.html

2、 在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. <parent>
  5. <artifactId>bg-cloud</artifactId>
  6. <groupId>com.bg</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <artifactId>bg-cloud-user</artifactId>
  11. <packaging>war</packaging>
  12. <name>bg-cloud-user Maven Webapp</name>
  13. <!-- FIXME change it to the project's website -->
  14. <url>http://www.example.com</url>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <maven.compiler.source>1.7</maven.compiler.source>
  18. <maven.compiler.target>1.7</maven.compiler.target>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>4.11</version>
  25. <scope>test</scope>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot</artifactId>
  30. <version>2.1.4.RELEASE</version>
  31. <scope>compile</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-autoconfigure</artifactId>
  36. <version>2.1.4.RELEASE</version>
  37. <scope>compile</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-web</artifactId>
  42. <version>2.1.4.RELEASE</version>
  43. <scope>compile</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  48. </dependency>
  49. </dependencies>
  50. <build>
  51. <finalName>bg-cloud-user</finalName>
  52. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  53. <plugins>
  54. <plugin>
  55. <artifactId>maven-clean-plugin</artifactId>
  56. <version>3.1.0</version>
  57. </plugin>
  58. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  59. <plugin>
  60. <artifactId>maven-resources-plugin</artifactId>
  61. <version>3.0.2</version>
  62. </plugin>
  63. <plugin>
  64. <artifactId>maven-compiler-plugin</artifactId>
  65. <version>3.8.0</version>
  66. </plugin>
  67. <plugin>
  68. <artifactId>maven-surefire-plugin</artifactId>
  69. <version>2.22.1</version>
  70. </plugin>
  71. <plugin>
  72. <artifactId>maven-war-plugin</artifactId>
  73. <version>3.2.2</version>
  74. </plugin>
  75. <plugin>
  76. <artifactId>maven-install-plugin</artifactId>
  77. <version>2.5.2</version>
  78. </plugin>
  79. <plugin>
  80. <artifactId>maven-deploy-plugin</artifactId>
  81. <version>2.8.2</version>
  82. </plugin>
  83. </plugins>
  84. </pluginManagement>
  85. </build>
  86. </project>

3、 接着创建一个启动类

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

4、 编写配置文件application.yml

  1. eureka:
  2. prefer-ip-address: true
  3. instance:
  4. appname: eureka-server
  5. hostname: localhost
  6. client:
  7. registry-fetch-interval-seconds: 10
  8. fetchRegistry: true
  9. serviceUrl:
  10. defaultZone: http://localhost:9000/eureka
  11. server:
  12. port: 8000
  13. spring:
  14. application:
  15. name: user-server

5、我们创建一个rest服务

  1. /**
  2. * @ClassName IndexController
  3. * @Description 描述此类用途
  4. * @Author lizhijun
  5. * @Date 2020/6/11 12:30
  6. * @Version V1.0
  7. **/
  8. @RestController
  9. @RequestMapping("/index")
  10. public class IndexController {
  11. @RequestMapping("/test")
  12. public String test(){
  13. System.out.println("经过了");
  14. retun "hello test 8000";
  15. }
  16. }

6、启动,去注册中心查看变化,我们把这个项目复制一份将端口改为8001在注册一遍,这样就有连个user-server微服务了,方便下面我们做负载均衡的实验。

查看原文请进:http://www.sucai66.com/article/detail/20200617/20.html

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

闽ICP备14008679号