Spring-cloud registration service provider construction method

The above has written how to build a registration center, only the registration center is not enough, so we need to register to the registration center and provide the service node, which is called the registration service provider here

premise

Read the above, and successfully build the registration center, the environment does not need to change

Project construction

Here we need to create a new maven project. The project name has not been set up before. Here is a reference. Mine is SpringCloudDemo, don't care about these details!

Modify the pom file, refer to the following:

Note: Please look at the version numbers of these jar packages. At the end of the article, I will post the github paths of the two simpler demos I built before.

  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. <!--Used to monitor the project and provide status information in the project-->
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-actuator</artifactId>
  47. </dependency>
  48. <!--junit test-->
  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>
copy code

Although the version number is different from the EurekaServer registry project, it can be used normally after practice, please rest assured

Create a new startup class (available in every springboot project)

  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. }
copy code

Create a new controller class and leave it for later testing

  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: service provider
  12. * @Date : 2018/4/12 11:36
  13. */
  14. @RestController
  15. public class SpringbootController {
  16. @Autowired
  17. private DiscoveryClient client; //Inject discovery client
  18. private final Logger logger = Logger.getLogger(SpringbootController.class);
  19. @RequestMapping(value = "/hello", method = RequestMethod.GET)
  20. public String hello(){
  21. //Get the service instance, which is used to display the effect on the console later
  22. ServiceInstance serviceInstance = client.getLocalServiceInstance();
  23. logger.info("/hello host:"+serviceInstance.getHost()+" service_id:" +serviceInstance.getServiceId());
  24. return "hello";
  25. }
  26. }
copy code

Create application.yml in the src/resources folder. This time, use yaml for configuration. If you want to try the properties file method, please refer to the above.

  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/
copy code

Well, we will run this project on port 8080, and we can go to the registry to register the service

Start the project of the registration center first, and then start the project after it is started.

test

Enter the url of the registry to view: localhost:1111

Access the controller path just configured: http://localhost:8080/hello

As shown on the right, the registration is successful.

At this point we can use this project to provide services

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/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325369599&siteId=291194637