服务提供与调用
2017-05-17 09:33
405 查看
服务提供
我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务
1、pom包配置
创建一个springboot项目,pom.xml中添加如下配置:<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、配置文件
application.properties配置如下:spring.application.name=spring-cloud-producer server.port=9000 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
参数在上一篇都已经解释过,这里不多说。
3、启动类
启动类中添加@EnableDiscoveryClient注解
@SpringBootApplication @EnableDiscoveryClient public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } }
4、controller
提供hello服务@RestController public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge"; } }
添加
@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。
到此服务提供者配置就完成了。
服务调用
1、pom包配置
和服务提供者一致<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、配置文件
application.properties配置如下:spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3、启动类
启动类添加@EnableDiscoveryClient和
@EnableFeignClients注解。
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
@EnableDiscoveryClient:启用服务注册与发现
@EnableFeignClients:启用feign进行远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
4、feign调用实现
@FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }
name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
5、web层调用远程服务
将HelloRemote注入到controller层,像普通方法一样去调用即可。@RestController public class ConsumerController { @Autowired HelloRemote HelloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return HelloRemote.hello(name); } }
到此,最简单的一个服务注册与调用的例子就完成了。
测试
简单调用
依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目先输入:
http://localhost:9000/hello?name=neo检查spring-cloud-producer服务是否正常
返回:
hello neo,this is first messge
说明spring-cloud-producer正常启动,提供的服务也正常。
浏览器中输入:
http://localhost:9001/hello/neo
返回:
hello neo,this is first messge
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
负载均衡
以上面spring-cloud-producer为例子修改,将其中的controller改动如下:@RestController public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is producer 2 send first messge"; } }
在配置文件中改动端口:
spring.application.name=spring-cloud-producer server.port=9003 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
打包启动后,在eureka就会发现两个服务提供者,如下图:
然后在浏览器再次输入:
http://localhost:9001/hello/neo进行测试:
第一次返回结果:
hello neo,this is first messge
第二次返回结果:
hello neo,this is producer 2 send first messge
不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。
相关文章推荐
- 对硬编码WCF服务的封装(提供服务和客户端调用的封装,调用样例....)
- C# 调用clicktell提供的SMS服务
- IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,
- 基于JDK6的JAX-WX为客户端提供XML与JSON格式数据服务,以及客户端采用AXIS调用案例
- WebService学习总结(四)——调用第三方提供的webService服务
- 代码示例:调用SPS提供的remoting服务,在线把Office文档转换成html文档
- WebService-调用第三方提供的webService服务
- SQL Server 2008安装时出现不能在控件上调用 Invoke 或 BeginInvoke错误的方法,为sql server代理服务提供的凭据无效。若要继续操作
- Android的框架层是如何调用C++提供的服务的
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
- ASP.NET MVC 扩展之 JsonpResult ,用来提供轻量级跨域调用服务
- ASP.NET MVC 扩展之 JsonpResult ,用来提供轻量级跨域调用服务
- [原]需要的是提供给别人服务调用,而非每次都要写函数实现
- httpInvoker提供远程服务调用总结(三)
- VC++6.0调用Web Services(可以是c#,java,vc++.net等提供的服务都可以)!
- 对硬编码WCF服务的封装(提供服务和客户端调用的封装,调用样例....)
- httpInvoker提供远程服务调用总结(二)
- 找到多个与名为“HOME”的控制器匹配的类型。如果为此请求(“{CONTROLLER}/{ACTION}/{ID}”)提供服务的路由在搜索匹配此请求的控制器时没有指定命名空间,则会发生此情况。如果是这样,请通过调用含有“NAMESPACES”参数的“MAPROUTE”方法的重载来注册此路由。
- 假定某系统提供硬件的访管指令(例如形式:“svc n”),为了实现系统调用,系统设计者应做哪些工作?用户又如如何请求操作系统服务?