Spring-cloud 注册服务提供者搭建方法

上文已经写了如何去搭建注册中心,仅有注册中心是远远不够的,所以我们需要注册到注册中心并提供服务的节点,这里称为注册服务提供者

前提

阅读上文,并成功搭建注册中心,环境无需改变

项目搭建

这里我们需要新建一个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文件方式,请参考上文,此处配置的提供服务地址请参考注册中心的配置

扫描二维码关注公众号,回复: 120429 查看本文章
  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/

猜你喜欢

转载自www.cnblogs.com/ljhseocom/p/8999529.html