spring cloud服务提供与调用示例
本文创建方式采用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服务提供与调用示例的更多相关文章
- 三、spring cloud 服务提供与调用
如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用. 案例中有三个角色:服务注册中心.服务提供者.服务消费者,eureka单机版启动既可,流程是首先启动注册中心,服务 ...
- spring-cloud:利用eureka实现服务提供与调用示例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springCl ...
- spring cloud服务间调用feign
参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...
- Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】
Spring Cloud(三):服务提供与调用 Eureka[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 上一篇文章我们介绍了 Eureka 服务 ...
- Spring Cloud 服务端注册与客户端调用
Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...
- spring cloud 服务A调用服务B自定义token消失,记录
后端:spring cloud 前端:vue 场景:前端ajax请求,包装自定义请求头token到后台做验证,首先调用A服务,A服务通过Feign调用B服务发现自定义token没有传到B服务去; 原因 ...
- 搭建SpringCloud-Eureka 注册中心以及服务提供与调用 快速了解 SpringCloud-Eureka
原文地址: 搭建SpringCloud-Eureka 注册中心以及服务提供与调用 纸上得来终觉浅,绝知此事要躬行啊~果然看着很easy,自己搞起来就是各种坑~各位看官,容我慢慢道来~ 关于spr ...
- Spring Cloud 服务网关Zuul
Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...
- Spring Cloud服务注册中心交付至kubernetes
前言 服务发现原则: 各个微服务在启动时,会将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息 服务消费者可以从服务发现组件中查询到服务提供者的网络地址,并使用该地址来远程调用服务 ...
随机推荐
- Spark程序数据结构优化
场景: 1.scala中的对象:对象头是16个字节(包含指向对象的指针等源数据信息),如果对象中只有一个int的属性,则会占用20个字节,也就是说对象的源数据占用了大部分的空间,所以在封装数据的时候尽 ...
- C++ Standards Support in GCC - GCC 对 C++ 标准的支持
C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...
- VS 2015连接SQL server数据库方法
vs新建一个Windows窗口应用程序,界面布局如下: Form1.cs中代码如下: using System; using System.Collections.Generic; using Sys ...
- logstash启动失败的问题追查
在实验中logstash是作为日志过滤器的作用,日志收集使用的则是filebeat组件.redis作为缓存器,logstash从redis中拉取数据进行过滤并传给elasticsearch组件. 但是 ...
- IO流(字节流,字符流,缓冲流)
一:IO流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流 这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图: 二:字符字节 ...
- 在VUE应用中配置ESLint(代码检查)
eslint配置方式 注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里 配置文件:使用一个js文件,JSON或者YAML文件来给整个目录和它的子目录指定配置信息.这些配置可以写在一个文 ...
- python 在windows下的 虚拟环境
解决 python 环境问题 windows 下安装 pip install virtualenv virtualenv的基本使用 1.1 创建虚拟环境 virtualenv venv 为环境指定Py ...
- swift面向协议编程
https://www.technotification.com/2018/08/protocol-oriented-programming-swift.html https://www.toptal ...
- Android集成讯飞语音、百度语音、阿里语音识别
项目实践:https://blog.csdn.net/Jsagacity/article/details/80094164 demo下载地址:https://fir.im/jy28 demo源码:ht ...
- ROS教程4 ROS自定义srv类型及使用
创建srv文件 在上一节单独为自定义的消息和服务的包 test_msgs 里面 创建 srv文件夹 进入创建 testsrv.srv 文件 ,内容为: (srv文件和msg文件类似,唯一不同的是它包含 ...