SpringCloub项目的搭建

一、需求:idea springboot+mybatis 并创建三个项目 :注册中心、服务方、消费方

1、首先 创建注册中心 ,并在 pom.xml添加依赖(此项目无业务逻辑、页面显示,可以当成一个中转站)

<!--spring cloud center pom-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
<!--spring cloud center management plugin-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、application.properties中添加配置

#服务注册中心
spring.application.name=server-register-center
server.port=1111

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

3、注册中心 接口处添加注释@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class ServiceRegisterApplication {

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

二、1、创建 服务方,并在pom.xml中添加依赖(此项目有业务逻辑,无界面显示)

    <!--服务提供方的依赖包-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、application.properties中添加配置

#spring cloud配置
spring.application.name=simple-service-provider
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

3、服务方接口处加入注解@EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class SimpleServerProviderApplication {

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

4、然后就是服务方逻辑的实现 (服务方只需提供接口,以便消费方调用)

@RestController
    public class ComputerController {

        @RequestMapping("/plus/{a}/{b}")
        public Integer plus(@PathVariable Integer a, @PathVariable Integer b){
        return a+b;

        }
}

三、1、创建消费方 ,并在pom.xml中添加依赖 (和服务方的依赖相同)

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、添加配置

spring.application.name=simple-service-consumer
server.port=3333
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

3、消费方接口处添加注释@EnableDiscoveryClient,以及实例化 RestTemplate

@SpringBootApplication
@EnableDiscoveryClient
public class SimpleServiceConsumerApplication {

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

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

4、消费方的Controller需要写一些代码,以便调用服务方准备好的接口( 传值推荐采用restful风格)

@RestController
public class MyConsumerController {


    @Autowired
    private LoadBalancerClient lbc;

    @Autowired
    private RestTemplate template;


    @RequestMapping("/comsumer/{a}/{b}")
    public String computePlus(@PathVariable Integer a, @PathVariable Integer b){
        //发现服务
        ServiceInstance choose = lbc.choose("simple-service-provider");
        String url = "http://" + choose.getHost() + ":" + choose.getPort() +     "/plus/"+a+"/"+b;

        Integer result = template.getForObject(url, Integer.class);

        return a+"+"+b+"="+result;

    }

5、然后三个项目同时启动,SpringCloub项目搭建成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值