SpringCloud学习---注册中心、服务注册、服务调用

参考:http://www.ityouknow.com/spring-cloud.html

先把第一个项目跑起来。完成服务注册到注册中心,消费者从注册中心进行服务的调用。

首先,创建一个简单的spingboot工程。推荐一个project,三个moduel。因为后续测试中,注册中心、服务提供者、消费者都是分开的。可参考我的项目:

注册中心

使用Eureka作为注册中心。Eureka的读音:[juˈri:kə],有点拗口,不过这个读音还挺好听的。^_^

添加Eureka依赖

据说eureka的更新有点跟不上springcloud的更新节奏,所以,添加依赖时,注意下version。目前eureka最好版本就是1.4.5,springboot都2.0.3了。

首先,springboot的包是不可少的

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>  

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

 

添加配置

可以在application.propertis或者applicaiton.yml中进行配置。这里使用yml配置:

server:
  port: 9999
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false   #表示不将自己注册到注册中心去
    fetch-registry: false #这里设置为true的话,启动就会报警
    service-url:  #注册中心的地址
       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

添加注解

在启动类中添加@EnableEurekaServer注解

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

启动

启动项目后,在浏览器中输入:http://localhost:9999/

即可看到注册中心页面

扫描二维码关注公众号,回复: 2554245 查看本文章

此时看到注册中心页面,即表示Eureka注册中心搭建成功。那么目前没有任何服务注册,所以Applicaion栏下为No instance available

服务注册

此时,我们需要创建服务并注册到Eureka注册中心去,使用provider moduel。

provider端的依赖同上。

创建controller

@RestController
public class DemoController {
    @Value("${server.port}")
    String port;

    @RequestMapping("say")
    public String say(){
        return "hello world";
    }

    @RequestMapping("hello")
    public String hello(@RequestParam String name){
        return "hello "+name+",port is "+port;
    }   

}

同正常的controller创建一样,没什么区别。

配置文件

同样,在application.yml中进行配置:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9999/eureka/ #需要将服务注册到那个注册中心(地址)去
server:
  port: 8762  #自身服务端口
spring:
  application:
    name: demo-provider #需要指定应用名,后续服务调用就是根据服务名进行调用

启动类注解

需要为启动类添加一个注解:@EnableEurekaClient,表明是一个eureka的服务客户端。

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

启动

最后,启动项目。在注册中心页面,我们可以看到该服务已经注册到该注册中心。

即服务已注册成功,红字可以先不用管。

直接在浏览器上调用该服务:

即服务的发布也没问题了。

服务消费

不同于Dubbo消费者与提供者基于sokect的通信,springcloud的服务都是rest风格的服务,即在浏览器上就可直接访问了。

创建远程接口

添加@FeignClient注解,指定需要调的那个服务的应用名,方法必须和该应用服务的服务保持一致。

@FeignClient(name = "demo-provider")
public interface ClientService {

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

    @RequestMapping("/say")
    String say();

}

controller

编写一个Controller,以便能够在页面上进行访问。

@RestController
public class ClientController {
    @Resource
    ClientService clientService;

    @RequestMapping("index")
    public String index(){
        String jack = clientService.say();
        return jack;
    }
    @RequestMapping("test/{name}")
    public String test(@PathVariable String name){
        String hello = clientService.hello(name);
        return hello;
    }
}

添加配置

spring.application.name=demo-consumer
server.port=8888
eureka.client.service-url.defaultZone=http://localhost:9999/eureka/

启动类注解

需要添加两个注解:

@EnableDiscoveryClient:启用服务注册与发现

@EnableFeignClients:启用feign进行远程调用

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

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

启动

启动后,该消费者也注册到注册中心了。

查看服务是否调用成功,在浏览器中输入:

http://localhost:8888/test/zhangsan   

http://localhost:8888/index

即消费者调用服务成功!

猜你喜欢

转载自blog.csdn.net/abreaking2012/article/details/81363718