SpringCloud服务提供者

Posted cppdy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud服务提供者相关的知识,希望对你有一定的参考价值。

服务提供者就是提供一个服务暴露出来给别人调用,在springcloud中需要注册服务到服务中心

搭建服务提供者项目(ProduceDemo)

1、创建pom.xml

<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.cppdy</groupId>
    <artifactId>ProduceDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath />
    </parent>
    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

2、创建application.yml配置文件

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9000/eureka/
      #注册到哪个服务中心上
server:
  port: 9001
  #当前服务的端口
spring:
  application:
    name: cppdy-hello
    #服务的名字

3、创建测试类(HelloController)

package com.cppdy.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${server.port}")
    private String port;
    
    @RequestMapping("hello")
    public String hello(String name) {
        return "Hello"+name+",From port:"+port;
    }
}

4、创建启动类(ProduceApp) 

package com.cppdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ProduceApp {

    public static void main(String[] args) {
        SpringApplication.run(ProduceApp.class, args);
    }

}

5、先启动EurekaDemo(注册中心项目),再启动ProduceDemo(服务提供者项目),访问http://localhost:9000/,看到服务的名字表示注册成功

技术分享图片

以上是关于SpringCloud服务提供者的主要内容,如果未能解决你的问题,请参考以下文章

springCloud 之 Eureka服务治理机制及代码运行

springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段

SpringCloud-创建服务消费者-Ribbon方式(附代码下载)

SpringCloud用Zookeeper做服务注册与发现中心代码实现

SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段

《SpringCloud超级入门》使用Eureka编写服务提供者《十》