Spring-cloud How to build a registered service provider

Source: Internet
Author: User

The above has been written on how to build a registry, only the registry is not enough, so we need to register to the registry and provide services node, this is referred to as a registered service provider

Premise

Read the above and successfully build a registration center without changing the environment

Project Building

Here we need to create a new MAVEN project, the project name is not good, here is the reference, my is springclouddemo, do not care about these details!

To modify the Pom file, refer to the following:

Note: Please look at the version number of these jar packages and I will post the GitHub path of the two simpler demos I built

  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 to
  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. <!--for monitoring projects, providing status information in projects--
  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 differs from the Eurekaserver registry project, it can be used in practice, please rest assured

Create a new startup class (available in each 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 testing later

  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. Gets the service instance, which functions as the console display effect after
  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 under the Src/resources folder this time using Yaml to configure, if you want to try the properties file way, please refer to the above, the service address configured here, please refer to the registry configuration

    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're going to run this project on port 8080, and we can go to the registration center and sign up for the service.

Start the project in the registry before starting the project.

Test

Enter the URL of the registry view: localhost:1111

Access the controller path you just configured: Http://localhost:8080/hello

As shown in the image on the right, 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/

Spring-cloud How to build a registered service provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.