本文创建方式采用intellij IDEA  创建项目

1.创建基于Eureka的注册中心。

在打开项目中右键,选择new 选择moudle

然后下一步

输入要创建的项目的信息

选择web下面的web,选择cloud  discovery下面的 Eureka server  下一步,创建项目

然后同步pom.xml文件内容、

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>
</repository>
</repositories> </project>

在启动文件中选择

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication { public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
} }

配置文件

server:
port: 8000
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:8000/eureka/
spring:
application:
name: spring-cloud-eureka

  创建完,我们去运行下,运行正常后,我们去访问localhost:8000, 到下面的界面,这样我们Eureka 注册中心就创建成功,

下面我们去创建server端,和client;

server端呢创建中与Eureka选择不同的在于cloud  discovery中,这里需要选择cloud Discovery

然后创建完,去同步对应的pom.xml文件

在启动类编写如下

@SpringBootApplication
@EnableDiscoveryClient
public class ServeroneApplication { public static void main(String[] args) {
SpringApplication.run(ServeroneApplication.class, args);
} }
配置文件
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
spring:
application:
name: spring-serverserver

我们需要编写一个提供服务的接口

@RestController
public class HelloController { @RequestMapping("/hello")
public String indesx(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}

这样我们就可以实现我们的server端

然后我们去创建client端

client端多需要在server上创建多一个Feign

那么我们在启动类,如下

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }

配置文件

server:
port: 8002
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
spring:
application:
name: spring-client

那么我们去写调用server的服务

@FeignClient(name= "spring-serverserver")
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}

实现接口

@RestController
public class ConsumerController { @Autowired
HelloRemote lloRemote; @RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return lloRemote.hello(name);
}
}

这样我们就实现了服务的注册与调用。

那么我们去启动服务进行测试,服务注册成功,我们去启动服务调用端

服务调用端成功,

那么我们去测试下,我们先去测试看单独访问服务是否正常

输入http://localhost:8001/hello?name=liwanlei

显示

那么我们看下另外一个调用这个服务的服务

http://localhost:8002/hello/name

那么我们看下返回

这样我们调试成功。

户端已经成功的通过feign调用了远程服务,并且将结果返回到了浏览器。

spring cloud服务提供与调用示例的更多相关文章

  1. 三、spring cloud 服务提供与调用

    如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用. 案例中有三个角色:服务注册中心.服务提供者.服务消费者,eureka单机版启动既可,流程是首先启动注册中心,服务 ...

  2. spring-cloud:利用eureka实现服务提供与调用示例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springCl ...

  3. spring cloud服务间调用feign

    参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...

  4. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

  5. Spring Cloud 服务端注册与客户端调用

    Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...

  6. spring cloud 服务A调用服务B自定义token消失,记录

    后端:spring cloud 前端:vue 场景:前端ajax请求,包装自定义请求头token到后台做验证,首先调用A服务,A服务通过Feign调用B服务发现自定义token没有传到B服务去; 原因 ...

  7. 搭建SpringCloud-Eureka 注册中心以及服务提供与调用 快速了解 SpringCloud-Eureka

    原文地址:  搭建SpringCloud-Eureka 注册中心以及服务提供与调用   纸上得来终觉浅,绝知此事要躬行啊~果然看着很easy,自己搞起来就是各种坑~各位看官,容我慢慢道来~ 关于spr ...

  8. Spring Cloud 服务网关Zuul

    Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...

  9. Spring Cloud服务注册中心交付至kubernetes

    前言 服务发现原则: 各个微服务在启动时,会将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息 服务消费者可以从服务发现组件中查询到服务提供者的网络地址,并使用该地址来远程调用服务 ...

随机推荐

  1. maven五:查找jar包坐标,选择jar包版本

    查找jar包坐标 以spring core的jar包为例,访问http://www.mvnrepository.com/    在最上方中间,输入spring core,点击Search. 搜索结果第 ...

  2. [20170705]diff比较执行结果的内容.txt

    [20170705]diff比较执行结果的内容.txt --//有时候需要比较2个命令输出的结果进行比较,比较笨的方法如下,例子: $  lsnrctl status LISTENER_SCAN2 & ...

  3. EntityFramework Code-First 简易教程(七)-------领域类配置之Fluent API

    Fluent API配置: 前面我们已经了解到使用DataAnotations特性来覆写Code-First默认约定,现在我们来学习Fluent API. Fluent API是另一种配置领域类的方法 ...

  4. php解决前后端验证字符串长度不一致

    前端代码 function getStrleng(str){ var myLen =0; for(var i=0;i<str.length;i++){ if(str.charCodeAt(i)& ...

  5. Linux 小知识翻译 - 「克隆」

    最近比较流行的Linux发行版,得是连新闻都报道的,刚刚发布新版的「CentOS」了. 「CentOS」一般被称为Red Hat EnterpriseLinux的克隆版本,这是什么意思呢? Linux ...

  6. Static简介

    1.static称为静态修饰符,它可以修饰类中得成员.被static修饰的成员被称为静态成员,也成为类成员,而不用static修饰的成员称为实例成员. 2.当 Voluem volu1 = new V ...

  7. 推荐5款简洁美观的Hexo主题

    2018-11-17 17:15:46 原文地址:http://www.izhongxia.com 以下是 <hexo 主题列表> 中挑选出来一些比较简洁美观的主题(存在个人主观意识,请勿 ...

  8. Opaque data type--不透明类型

    Opaque:对使用者来说,类型结构和机制明晰即为transparent,否则为Opaque In computer science, an opaque data type is a data ty ...

  9. Git解决冲突(本地共享仓库简单实践)

    1:可以使用git init --bare初始化一个本地共享仓库. 2:假设有A,B两个人进行合作开发,此时A,B可以使用git clone 共享仓库路径进行克隆.此时A,B的室友仓库代码是一致的. ...

  10. Semaphore实现的生产者消费者程序

    Semaphore:Semaphores are often used to restrict the number of threads than can access some (physical ...