1. 程式人生 > >Spring-cloud 註冊服務提供者搭建方法

Spring-cloud 註冊服務提供者搭建方法

demo server -c esc boot ica 新建 spring .class

上文已經寫了如何去搭建註冊中心,僅有註冊中心是遠遠不夠的,所以我們需要註冊到註冊中心並提供服務的節點,這裏稱為註冊服務提供者

前提

閱讀上文,並成功搭建註冊中心,環境無需改變

項目搭建

這裏我們需要新建一個maven項目,項目名稱之前沒有起好,這裏就參考一下,我的是SpringCloudDemo,不要在意這些細節!

修改pom文件,參考如下:

註意:請看好這些jar包的版本號,文末我會貼出之前我搭建的兩個比較簡單的demo的github路徑

  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.hellxz</groupId>
  6. <artifactId>SpringCloudDemo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>SpringCloudDemo</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>1.5.9.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-dependencies</artifactId>
  22. <version>Camden.SR3</version>
  23. <type>pom</type>
  24. <scope>import</scope>
  25. </dependency>
  26. </dependencies>
  27. </dependencyManagement>
  28. <properties>
  29. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  30. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  31. <java.version>1.8</java.version>
  32. </properties>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <!--用於監控項目,提供項目中的狀態信息-->
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-actuator</artifactId>
  47. </dependency>
  48. <!--junit測試-->
  49. <dependency>
  50. <groupId>junit</groupId>
  51. <artifactId>junit</artifactId>
  52. <version>4.8.2</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-starter-eureka</artifactId>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.springframework.cloud</groupId>
  60. <artifactId>spring-cloud-config-server</artifactId>
  61. </dependency>
  62. </dependencies>
  63. <build>
  64. <plugins>
  65. <plugin>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-maven-plugin</artifactId>
  68. </plugin>
  69. <plugin>
  70. <groupId>org.apache.maven.plugins</groupId>
  71. <artifactId>maven-compiler-plugin</artifactId>
  72. <configuration>
  73. <source>1.8</source>
  74. <target>1.8</target>
  75. </configuration>
  76. </plugin>
  77. </plugins>
  78. </build>
  79. </project>
復制代碼

雖然版本號不同於EurekaServer註冊中心項目,但是經實踐是可以正常使用的,請放心

新建一個啟動類(每個springboot項目中都有)

  1. package com.hellxz.springcloudhelloworld;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. /**
  6. * @Author : Hellxz
  7. * @Description: EurekaClient
  8. * @Date : 2018/4/13 16:57
  9. */
  10. @EnableDiscoveryClient
  11. @SpringBootApplication
  12. public class SpringCloudDemoApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(SpringCloudDemoApplication.class, args);
  15. }
  16. }
復制代碼

新建一個controller類,留作之後測試

  1. package com.hellxz.springcloudhelloworld;
  2. import org.apache.log4j.Logger;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.cloud.client.ServiceInstance;
  5. import org.springframework.cloud.client.discovery.DiscoveryClient;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RestController;
  9. /**
  10. * @Author : Hellxz
  11. * @Description: 服務提供者
  12. * @Date : 2018/4/12 11:36
  13. */
  14. @RestController
  15. public class SpringbootController {
  16. @Autowired
  17. private DiscoveryClient client; //註入發現客戶端
  18. private final Logger logger = Logger.getLogger(SpringbootController.class);
  19. @RequestMapping(value = "/hello", method = RequestMethod.GET)
  20. public String hello(){
  21. //獲取服務實例,作用為之後console顯示效果
  22. ServiceInstance serviceInstance = client.getLocalServiceInstance();
  23. logger.info("/hello host:"+serviceInstance.getHost()+" service_id:" +serviceInstance.getServiceId());
  24. return "hello";
  25. }
  26. }
復制代碼

在src/resources文件夾下創建application.yml 這次使用yaml進行配置,如果想嘗試properties文件方式,請參考上文,此處配置的提供服務地址請參考註冊中心的配置

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: hello-service
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone:
  10. http://localhost:1111/eureka/
復制代碼

好了,我們將這個項目跑在8080端口,並可以去註冊中心註冊服務了

先啟動註冊中心的項目,待其啟動完畢之後,在來啟動本項目。

測試

輸入註冊中心的url查看:localhost:1111

技術分享圖片

訪問剛才配置的controller路徑: http://localhost:8080/hello

技術分享圖片

技術分享圖片

如右圖所示,註冊成功。

此時我們就可以使用這個項目進行提供服務了

http://www.ljhseo.com/
http://www.xyrjkf.net/
http://www.xyrjkf.cn/
http://www.xyrjkf.com.cn/
http://www.zjdygsi.cn/
http://www.zjdaiyun.cn/
http://www.jsdygsi.cn/
http://www.xyrjkf.top/
http://www.xyrjkf.com/
http://www.daiyunzj.cn/
http://ljhseo.com/
http://xyrjkf.net/
http://xyrjkf.cn/
http://xyrjkf.com.cn/
http://zjdygsi.cn/
http://zjdaiyun.cn/
http://jsdygsi.cn/
http://xyrjkf.top/
http://xyrjkf.com/
http://daiyunzj.cn/

Spring-cloud 註冊服務提供者搭建方法