Spring-cloud 注冊服務(wù)提供者搭建方法
上文已經(jīng)寫了如何去搭建注冊中心,僅有注冊中心是遠(yuǎn)遠(yuǎn)不夠的,所以我們需要注冊到注冊中心并提供服務(wù)的節(jié)點(diǎn),這里稱為注冊服務(wù)提供者
前提
閱讀上文,并成功搭建注冊中心,環(huán)境無需改變
項(xiàng)目搭建
這里我們需要新建一個(gè)maven項(xiàng)目,項(xiàng)目名稱之前沒有起好,這里就參考一下,我的是SpringCloudDemo,不要在意這些細(xì)節(jié)!
修改pom文件,參考如下:
注意:請看好這些jar包的版本號(hào),文末我會(huì)貼出之前我搭建的兩個(gè)比較簡單的demo的github路徑
<?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>
<!--用于監(jiān)控項(xiàng)目,提供項(xiàng)目中的狀態(tài)信息-->
<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>
雖然版本號(hào)不同于EurekaServer注冊中心項(xiàng)目,但是經(jīng)實(shí)踐是可以正常使用的,請放心
新建一個(gè)啟動(dòng)類(每個(gè)springboot項(xiàng)目中都有)
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);
}
}
新建一個(gè)controller類,留作之后測試
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: 服務(wù)提供者
* @Date : 2018/4/12 11:36
*/
@RestController
public class SpringbootController {
@Autowired
private DiscoveryClient client; //注入發(fā)現(xiàn)客戶端
private final Logger logger = Logger.getLogger(SpringbootController.class);
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(){
//獲取服務(wù)實(shí)例,作用為之后console顯示效果
ServiceInstance serviceInstance = client.getLocalServiceInstance();
logger.info("/hello host:"+serviceInstance.getHost()+" service_id:" +serviceInstance.getServiceId());
return "hello";
}
}
在src/resources文件夾下創(chuàng)建application.yml 這次使用yaml進(jìn)行配置,如果想嘗試properties文件方式,請參考上文,此處配置的提供服務(wù)地址請參考注冊中心的配置
server:
port: 8080
spring:
application:
name: hello-service
eureka:
client:
serviceUrl:
defaultZone:
http://localhost:1111/eureka/
好了,我們將這個(gè)項(xiàng)目跑在8080端口,并可以去注冊中心注冊服務(wù)了
先啟動(dòng)注冊中心的項(xiàng)目,待其啟動(dòng)完畢之后,在來啟動(dòng)本項(xiàng)目。
測試
輸入注冊中心的url查看:localhost:1111

訪問剛才配置的controller路徑: http://localhost:8080/hello

如右圖所示,注冊成功。
此時(shí)我們就可以使用這個(gè)項(xiàng)目進(jìn)行提供服務(wù)了
示例demo:
https://github.com/HellxZ/EurekaServer
https://github.com/HellxZ/EurekaClient
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
最大子數(shù)組和Java實(shí)現(xiàn)代碼示例
這篇文章主要介紹了最大子數(shù)組和Java實(shí)現(xiàn)的相關(guān)資料,文中介紹了兩種方法來解決尋找具有最大和的連續(xù)子數(shù)組的問題,第一種方法是動(dòng)態(tài)規(guī)劃,第二種方法是分治法,需要的朋友可以參考下2024-11-11
Java實(shí)現(xiàn)從字符串中找出數(shù)字字符串的方法小結(jié)
這篇文章主要介紹了Java實(shí)現(xiàn)從字符串中找出數(shù)字字符串的方法,結(jié)合實(shí)例形式總結(jié)分析了Java查找數(shù)字字符串的常用技巧,需要的朋友可以參考下2016-03-03
SpringBoot請求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹
這篇文章主要介紹了SpringBoot請求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
Mybatis實(shí)現(xiàn)批量操作8種小結(jié)
本文對Mybatis的五種批處理方式進(jìn)行了性能測試,包括批量新增和批量修改,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
Java使用String類格式化當(dāng)前日期實(shí)現(xiàn)代碼
這篇文章主要介紹了Java使用String類格式化當(dāng)前日期實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02

