SpringCloud 服务注册,服务消费

### 1、通过Springboot建立注册中心,核心是注解@EnableEurekaServer,其他基本上就是Springboot初始化出来的东西;这个注解生命了这个工程就是一个服务的注册中心。

这里写图片描述

package com.adesk.springclouddome;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.PropertySource;

@PropertySource( "classpath:application-ser.properties")
@EnableEurekaServer
@SpringBootApplication
public class SpringclouddomeApplication {

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

还有一个重点就是配置文件,就是下面几行就行

server.port=7171
#注册中心默认端口就是8761,也可通过下面的方式定义其他端口
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
eureka.client.registerWithEureka=false
eureka.cli

到此服务中心建立完毕。浏览器打开localhost:7171会看到网页就说明对了。

2、注册服务,新建一个工程和上面的步骤一样

application.classshao做改变,把上面的注解换成@EnableEurekaClient,这就生命该工程是一个服务,将会注册到注册中心。

@SpringBootApplication
@EnableEurekaClient
public class ClientdomeApplication {

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

配置文件改动如下

#服务的端口号
server.port=7172
#该服务的名称,后续调用服务要用到
spring.application.name=compute-service
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:7171/eureka/

这个工程下面可以定义一些服务(也就是一些方法了)就是你平时web项目工程
controller的一个方法如下

@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;
    @RequestMapping("/test")
    public  String  search(){
        return searchService.find();

    }
}
这样就说明这个服务中可以通过
http://localhost:7172/test
调用成功
3、建立服务的消费方,新建一个工程,跟上面一样

如下:

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class ClientdApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientdApplication.class, args);
    }
}
@EnableEurekaClient说明该工程也是一个服务注册方也可以为别人提供服务。
@EnableFeignClients通过扫描@FeignClient注解的类进行服务调用。

本文通过Feign调用服务,只需要在定义一个接口如下

@FeignClient("compute-service")
@Service
public interface ConsumerService {
    @RequestMapping("/test")
    public String search();
}
"compute-service"代表服务的名称,就是上面配置文件的spring.application.name
@RequestMapping("/test")
这里相当与http://compute-service/test的调用等价于http://localhost:7172/test  
所以这里实现第二个工程的调用。

配置文件如下

server.port=7173
spring.application.name=compute-service2
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:7171/eureka/

现在重启刷新注册中心会出现两个服务
分别为compute-service和compute-service2
在服务调用方定义了一个controller具体如下:

@RestController
@RequestMapping("/config")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    @RequestMapping("/test")
    public void consumer(){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>.....success!");
        String result=consumerService.search();
        System.out.println(result);
    }
}
这里通过ConsumerService调用了compute-service的方法。就这样当你浏览器输入
http://localhost:7173/config/test
的时候会打印
于http://localhost:7172/test
接口的内容。说明成功。
#####完整的代码请移步到
[源码地址](https://github.com/AdesKing/SpringCloudDome)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值