IDEA搭建springcloud微服务

1:创建空的MAVEN项目

然后删除src目录

二:搭建注册中心

①在CloudDemo项目上新建Module

②选择Spring Initializr,点击Next

目录结构

③在启动类添加注解表明是注册中心

@EnableEurekaServer 

④打开application.properties文件进行编辑

#注册中心端口号
server.port=1010
#注册中心IP地址
eureka.instance.hostname=localhost
#服务注册中心不注册自己
eureka.client.register-with-eureka=false
#注册中心不负责检索服务
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

⑤启动项目

⑥访问 http://localhost:1010/

    注册中心完成

三:服务提供者

①新建Module

②添加注解

package com.example.ribbonclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
@EnableDiscoveryClient
public class RibbonClientApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

③新建包controller

④新建java文件HelloRibbonControler

@RestController
public class HelloRibbonController {

    @RequestMapping("/hello")
    public String hello(String name){
        return name+":hello world";
    }
}

⑤配置application.properties文件

server.port=1011
eureka.instance.hostname=localhost
spring.application.name=ribbon-client
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:1010/eureka/

⑥启动项目

⑦访问http://localhost:1010/

http://localhost:1011/hello?name=aa

⑧也可以新建service,controller调用service

@Service
public class HelloRibbonService {
    public String sayHello(String name){
        return name+" service:hello world";
    }
}

访问结果

四:服务消费者

①新建项目ribbon-consumer

②需要加上RestTemplate老调用服务

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {


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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

③建controller和service层

service代码如下

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String getHello(String name){
        return restTemplate.getForObject("http://ribbon-client/hello?name=zhaoyangguang",String.class);
    }
}

controller代码如下

@RestController
public class HelloController {


    @Autowired
    HelloService service;
    @RequestMapping("/hello")
    public String hello(String name){
        return  service.getHello(name);
    }
}

 

④application.properties配置信息

server.port=1012
spring.application.name=ribbon-consumer
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:1010/eureka/

⑤访问http://localhost:1012/hello

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值