当前位置:   article > 正文

springcloud 最新版本上手教程1及坑——(Eureka)服务注册和发现_spring-cloud-dependencies 最新版

spring-cloud-dependencies 最新版

1.建个maven工程

2.建2个module,一个做service,一个做client



next 

  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. <groupId>com.zn</groupId>
  6. <artifactId>eurekaserver</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>eurekaserver</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.1.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RC1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34. <dependencyManagement>
  35. <dependencies>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-dependencies</artifactId>
  39. <version>${spring-cloud.version}</version>
  40. <type>pom</type>
  41. <scope>import</scope>
  42. </dependency>
  43. </dependencies>
  44. </dependencyManagement>
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. <repositories>
  54. <repository>
  55. <id>spring-milestones</id>
  56. <name>Spring Milestones</name>
  57. <url>https://repo.spring.io/milestone</url>
  58. <snapshots>
  59. <enabled>false</enabled>
  60. </snapshots>
  61. </repository>
  62. </repositories>
  63. </project>

建完后的pom。用的最新的springcloud的F版本,sb是2.0

新版本是

<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

网上很多旧版本是没有netflix的

3.sb入口加1个注解

  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class EurekaserverApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaserverApplication.class, args);
  6. }
  7. }
很简单,
@EnableEurekaServer表示是个server

4.appication.yml配置文件

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://localhost:8761/eureka/
registerWithEureka,
fetchRegistry都是false,表示这货是不是个client

5.启动工程,打开

http://localhost:8761 

这样就成功了


6.再做一个client,再建个module,pom如下:

  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. <groupId>com.zn</groupId>
  6. <artifactId>server-hi</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>server-hi</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.1.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RC1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-config</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. </dependencies>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-dependencies</artifactId>
  43. <version>${spring-cloud.version}</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. <repositories>
  58. <repository>
  59. <id>spring-milestones</id>
  60. <name>Spring Milestones</name>
  61. <url>https://repo.spring.io/milestone</url>
  62. <snapshots>
  63. <enabled>false</enabled>
  64. </snapshots>
  65. </repository>
  66. </repositories>
  67. </project>
  1. @SpringBootApplication
  2. @RestController
  3. @EnableDiscoveryClient
  4. public class ServerHiApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ServerHiApplication.class, args);
  7. }
  8. @Value("${server.port}")
  9. String port;
  10. @RequestMapping("/hi")
  11. public String home(@RequestParam String name) {
  12. return "hi "+name+",i am from port:" +port;
  13. }
  14. }
  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8762
  7. spring:
  8. application:
  9. name: service-hi

service-hi就是你注册的id

打开

http://localhost:8762/hi?name=a



注册完成


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

闽ICP备14008679号