虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Java编程 > Spring-cloud 注册服务提供者搭建方法

Spring-cloud 注册服务提供者搭建方法
类别:Java编程   作者:码皇   来源:互联网   点击:

本篇文章主要介绍了Spring-cloud 注册服务提供者搭建方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

上文已经写了如何去搭建注册中心,仅有注册中心是远远不够的,所以我们需要注册到注册中心并提供服务的节点,这里称为注册服务提供者

前提

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

项目搭建

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

修改pom文件,参考如下:

注意:请看好这些jar包的版本号,文末我会贴出之前我搭建的两个比较简单的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> <!--用于监控项目,提供项目中的状态信息--> <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注册中心项目,但是经实践是可以正常使用的,请放心

新建一个启动类(每个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@SpringBootApplicationpublic 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 */@RestControllerpublic 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: 8080spring: application: name: hello-serviceeureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/

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

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

测试

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

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

 

如右图所示,注册成功。

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

示例demo:

https://github.com/HellxZ/EurekaServer

https://github.com/HellxZ/EurekaClient

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • Spring Cloud实现提供API给客户端的方法详解
相关热词搜索: spring cloud 提供者 spring cloud服务搭建