SpringCloud2.0 -- client

pom依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

在启动类上面加入注解

@SpringBootApplication
@EnableDiscoveryClient
public class ExampleClientApplication {

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

}

@EnableDiscoveryClient 含义为开启一个客户端

提供服务接口

@RestController
public class HelloCloudController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String helloCloud(){
        return "hello SpringCloud !";
    }
}

客户端一般就是提供各种各样的服务接口,这里我们简单的写了一个返回字符串信息的接口,通过get请求能够访问它。

将客户端注册到eureka

配置文件中添加:

server:
  port: 8771
eureka:
  instance:
    hostname: localhost
    #instance-id
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

defaultZone 指的就是注册的地址,如果有多个节点,则把每个节点的配置加入到defaultZone,以逗号隔开。
同时instance指的是本机的host,instance-id是一个有意思的配置,可以改变你的实例在eureka上显示的地址。

最后

客户端没有太多复杂的配置,开箱即用还是强大啊…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值