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
- <?xml version= "1.0" encoding= "UTF-8"?>
- <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.hellxz</groupId>
- <artifactId>SpringCloudDemo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>SpringCloudDemo</name>
- <description>demo Project for Spring boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- <relativePath/> <!--lookup parent from repository to
- </parent>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Camden.SR3</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!--for monitoring projects, providing status information in projects--
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <!--junit Test-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </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)
- Package Com.hellxz.springcloudhelloworld;
- Import org.springframework.boot.SpringApplication;
- Import org.springframework.boot.autoconfigure.SpringBootApplication;
- Import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- /**
- * @Author: HELLXZ
- * @Description: Eurekaclient
- * @Date: 2018/4/13 16:57
- */
- @EnableDiscoveryClient
- @SpringBootApplication
- public class Springclouddemoapplication {
- public static void Main (string[] args) {
- Springapplication.run (Springclouddemoapplication.class, args);
- }
- }
Copy Code
Create a new controller class and leave it for testing later
- Package Com.hellxz.springcloudhelloworld;
- Import Org.apache.log4j.Logger;
- Import org.springframework.beans.factory.annotation.Autowired;
- Import org.springframework.cloud.client.ServiceInstance;
- Import org.springframework.cloud.client.discovery.DiscoveryClient;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestMethod;
- Import Org.springframework.web.bind.annotation.RestController;
- /**
- * @Author: HELLXZ
- * @Description: Service Provider
- * @Date: 2018/4/12 11:36
- */
- @RestController
- public class Springbootcontroller {
- @Autowired
- Private Discoveryclient client; Inject Discovery Client
- Private final Logger Logger = Logger.getlogger (Springbootcontroller.class);
- @RequestMapping (value = "/hello", method = Requestmethod.get)
- Public String Hello () {
- Gets the service instance, which functions as the console display effect after
- Serviceinstance serviceinstance = Client.getlocalserviceinstance ();
- Logger.info ("/hello Host:" +serviceinstance.gethost () + "service_id:" +serviceinstance.getserviceid ());
- return "Hello";
- }
- }
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
- Server
- port:8080
- Spring
- Application:
- Name:hello-service
- Eureka
- Client
- Serviceurl:
- Defaultzone:
- 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