上文已經寫了如何去搭建註冊中心,僅有註冊中心是遠遠不夠的,因此咱們須要註冊到註冊中心並提供服務的節點,這裏稱爲註冊服務提供者html
閱讀上文,併成功搭建註冊中心,環境無需改變java
這裏咱們須要新建一個maven項目,項目名稱以前沒有起好,這裏就參考一下,個人是SpringCloudDemo,不要在乎這些細節!git
修改pom文件,參考以下:github
注意:請看好這些jar包的版本號,文末我會貼出以前我搭建的兩個比較簡單的demo的github路徑web
<?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> <!--用於監控項目,提供項目中的狀態信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--junit測試--> <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>
雖然版本號不一樣於EurekaServer註冊中心項目,可是經實踐是能夠正常使用的,請放心spring
新建一個啓動類(每一個springboot項目中都有)apache
1 package com.hellxz.springcloudhelloworld; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 /** 8 * @Author : Hellxz 9 * @Description: EurekaClient 10 * @Date : 2018/4/13 16:57 11 */ 12 @EnableDiscoveryClient 13 @SpringBootApplication 14 public class SpringCloudDemoApplication { 15 16 public static void main(String[] args) { 17 SpringApplication.run(SpringCloudDemoApplication.class, args); 18 } 19 }
新建一個controller類,留做以後測試springboot
1 package com.hellxz.springcloudhelloworld; 2 3 import org.apache.log4j.Logger; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.cloud.client.ServiceInstance; 6 import org.springframework.cloud.client.discovery.DiscoveryClient; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RestController; 10 11 /** 12 * @Author : Hellxz 13 * @Description: 服務提供者 14 * @Date : 2018/4/12 11:36 15 */ 16 @RestController 17 public class SpringbootController { 18 19 @Autowired 20 private DiscoveryClient client; //注入發現客戶端 21 22 private final Logger logger = Logger.getLogger(SpringbootController.class); 23 24 @RequestMapping(value = "/hello", method = RequestMethod.GET) 25 public String hello(){ 26 //獲取服務實例,做用爲以後console顯示效果 27 ServiceInstance serviceInstance = client.getLocalServiceInstance(); 28 logger.info("/hello host:"+serviceInstance.getHost()+" service_id:" +serviceInstance.getServiceId()); 29 return "hello"; 30 } 31 }
在src/resources文件夾下建立application.yml 此次使用yaml進行配置,若是想嘗試properties文件方式,請參考上文,此處配置的提供服務地址請參考註冊中心的配置app
server: port: 8080 spring: application: name: hello-service eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/
好了,咱們將這個項目跑在8080端口,並能夠去註冊中心註冊服務了maven
先啓動註冊中心的項目,待其啓動完畢以後,在來啓動本項目。
輸入註冊中心的url查看:localhost:1111
訪問剛纔配置的controller路徑: http://localhost:8080/hello
如右圖所示,註冊成功。
此時咱們就能夠使用這個項目進行提供服務了
示例demo:
https://github.com/HellxZ/EurekaServer
https://github.com/HellxZ/EurekaClient