The above article has already written how to build a registration center. It is far from enough to have a registration center, so we need to register in the registration center and provide services. This is called the registration service provider.
premise
Read the above and successfully build a registration center without changing the environment
Project construction
Here we need to create a new maven project. The project name has not been raised before. Let me refer to it here. Mine is SpringCloudDemo, don’t care about these details!
To 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 simple 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> </dependency> </dependency> </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> </dependency> </dependency> <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 Registration Center project, it can be used normally through practice. Please rest assured
Create a new startup class (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@SpringBootApplicationpublic 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 */@RestControllerpublic 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, and the function is to display the effect of the console 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. Please refer to the configuration of the registration center for the service address configured here.
server: port: 8080spring: application: name: hello-serviceeureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/
OK, we will run this project on port 8080 and can go to the registration center to register for services.
Start the project in the registration center first, and after it is started, start the project.
test
Enter the url of the registration center to view: localhost:1111
Access the controller path you just configured: http://localhost:8080/hello
As shown in the picture on the right, the registration is successful.
At this time, we can use this project to provide services
Example demo:
https://github.com/HellxZ/EurekaServer
https://github.com/HellxZ/EurekaClient
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.