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.
- <?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 -->
- </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>
- <!--Used to monitor the project and provide status information in the project-->
- <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>
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)
- 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);
- }
- }
Create a new controller class and leave it for later testing
- 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(){
- //Get the service instance, which is used to display the effect on the console later
- ServiceInstance serviceInstance = client.getLocalServiceInstance();
- logger.info("/hello host:"+serviceInstance.getHost()+" service_id:" +serviceInstance.getServiceId());
- return "hello";
- }
- }
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.
- server:
- port: 8080
- spring:
- application:
- name: hello-service
- eureka:
- client:
- serviceUrl:
- defaultZone:
- http://localhost:1111/eureka/
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/