SpringCloud----服务的提供与调用

以上一篇文章中的注册中心Eureka工程为基础,今天写一个服务提供和一个服务调用的具体例子。

服务的提供

  1. 新建一个springboot项目,然后在pom文件中加入以下依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.3.6.RELEASE</version>
</dependency>
  1. 接着我们在application.properties文件里配置如下:
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  1. 在启动类新增@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudProducerApplication.class, args);
    }
}
  1. 编写controller
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(@RequestParam String name){
        return "hello "+name+",this is first message by producer";
    }
}

添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务

注册中心
至此,服务的提供者配置完成。

服务的调用

  1. 新建一个springboot项目,然后在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>
  1. 配置application.properties文件
spring.application.name=spring-cloud-consumer
server.port=9100
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

feign.hystrix.enabled=true
  1. 启动类添加@EnableDiscoveryClient和@EnableFeignClients注解。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudConsumeApplication {

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

@EnableDiscoveryClient :启用服务注册与发现
@EnableFeignClients:启用feign进行远程调用

  1. feign调用的实现
    编写HelloRemote接口
@FeignClient(name = "spring-cloud-producer", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @RequestMapping("/hello")
    String hello(@RequestParam("name") String name);
}

name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

  1. controller层调用远程服务
@RestController
public class HelloController {

    @Autowired
    private 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=fyf

返回:hello fyf,this is first messge by producer

说明spring-cloud-producer正常启动,提供的服务也正常。

浏览器中输入:http://localhost:9100/hello/fyf

返回:hello fyf,this is first messge by producer

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

负载均衡

  1. 复制一个生产者项目为spring-cloud-producer
  2. 修改项目配置文件的端口为9001
  3. 修改controller文件的hello方法
@RestController
public class HelloController {

    @RequestMapping
    public String index(@RequestParam String name){
        return "hello "+name+",this is first message by producer2";
    }
}
  1. 启动服务,注册中心会出现两个生产者服务
    注册中心
    再在浏览器中进行测试,输入地址:http://localhost:9100/hello/fyf
    浏览器第一次返回:hello fyf,this is first messge by producer
    浏览器第二次返回: hello fyf,this is first messge by producer2
    由此可见,达到了负载均衡的效果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值