JAVA语言之Spring-cloud 注册服务提供者搭建方法
龚超 2018-06-19 来源 : 阅读 1020 评论 0

摘要:本文主要向大家介绍了JAVA语言的Spring-cloud 注册服务提供者搭建方法,通过具体的实例让大家了解,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言的Spring-cloud 注册服务提供者搭建方法,通过具体的实例让大家了解,希望对大家学习JAVA语言有所帮助。

前提

阅读上文,并成功搭建注册中心,环境无需改变

项目搭建

这里我们需要新建一个maven项目,项目名称之前没有起好,这里就参考一下,我的是SpringCloudDemo,不要在意这些细节!

修改pom文件,参考如下:

注意:请看好这些jar包的版本号,文末我会贴出之前我搭建的两个比较简单的demo的github路径


<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.hellxz
SpringCloudDemo
0.0.1-SNAPSHOT
jar
SpringCloudDemo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Camden.SR3
pom
import
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-actuator
junit
junit
4.8.2
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8





复制代码

虽然版本号不同于EurekaServer注册中心项目,但是经实践是可以正常使用的,请放心

新建一个启动类(每个springboot项目中都有)


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);
}
}


复制代码

新建一个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: 服务提供者
* @Date : 2018/4/12 11:36
*/
@RestController
public class SpringbootController {
@Autowired
private DiscoveryClient client; //注入发现客户端
private final Logger logger = Logger.getLogger(SpringbootController.class);
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(){
//获取服务实例,作用为之后console显示效果
ServiceInstance serviceInstance = client.getLocalServiceInstance();
logger.info("/hello host:"+serviceInstance.getHost()+" service_id:" +serviceInstance.getServiceId());
return "hello";
}
}

复制代码

在src/resources文件夹下创建application.yml 这次使用yaml进行配置,如果想尝试properties文件方式,请参考上文,此处配置的提供服务地址请参考注册中心的配置


server:
port: 8080
spring:
application:
name: hello-service
eureka:
client:
serviceUrl:
defaultZone:
//localhost:1111/eureka/


复制代码

好了,我们将这个项目跑在8080端口,并可以去注册中心注册服务了

先启动注册中心的项目,待其启动完毕之后,在来启动本项目。

测试

输入注册中心的url查看:localhost:1111


访问刚才配置的controller路径: //localhost:8080/hello

如右图所示,注册成功。

此时我们就可以使用这个项目进行提供服务了

希望这篇文章可以帮助到你,总之同学们,IT资讯尽在职坐标。

本文由 @职坐标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论
本文作者 联系TA

擅长针对企业软件开发的产品设计及开发的细节与流程设计课程内容。座右铭:大道至简!

  • 370
    文章
  • 45109
    人气
  • 83%
    受欢迎度

已有24人表明态度,83%喜欢该老师!

进入TA的空间
求职秘籍 直通车
  • 资料领取 资料领取 资料领取
  • 答疑解惑 答疑解惑 答疑解惑
  • 技术交流 技术交流 技术交流
  • 职业测评 职业测评 职业测评
  • 面试技巧 面试技巧 面试技巧
  • 高薪秘笈 高薪秘笈 高薪秘笈
TA的其他文章 更多>>
WEB前端必须会的基本知识题目
经验技巧 93% 的用户喜欢
Java语言中四种遍历List的方法总结(推荐)
经验技巧 91% 的用户喜欢
Java语言之SHA-256加密的两种实现方法详解
经验技巧 75% 的用户喜欢
java语言实现把两个有序数组合并到一个数组的实例
经验技巧 75% 的用户喜欢
通过Java语言代码来创建view的方法
经验技巧 80% 的用户喜欢
其他海同师资 更多>>
吕益平
吕益平 联系TA
熟悉企业软件开发的产品设计及开发
孔庆琦
孔庆琦 联系TA
对MVC模式和三层架构有深入的研究
戴懿颢​
戴懿颢​ 联系TA
20年+嵌入式开发经验,精多语言 / 云 / 安全 / 数据库
郭自琦
郭自琦 联系TA
16年物联网经验,涉多项目,多校授课,出版书籍并研发IT教程
余承民
余承民 联系TA
8年开发+5年教学经验,指导数千名学员高薪就业
经验技巧30天热搜词 更多>>

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved