springcloud服务提供端-02

springcloud入门详细搭建微服务环境-01

搭建springcloud服务提供端的搭建过程:这是在《springcloud入门详细搭建微服务环境-01 》的基础上进行的。

1.先导入springcloud服务提供端的依赖jar包



        <!-- springBoot支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot 测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- Eureka 客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- spring cloud 核心,自动配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

2.建一个springcloud服务提供端的入口类

package cn.mesmile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @Created with IDEA
 * @author: Super Zheng
 * @Description: java类作用描述
 * @Date:2019/1/6
 * @Time:11:34
 *
 * @EnableEurekaClient 表示这是一个Eureka的客户端
 */
@SpringBootApplication
@EnableEurekaClient
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class);
    }
}

3.写一个controller类用于提供服务

package cn.mesmile.controller;

import cn.mesmile.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Created with IDEA
 * @author: Super Zheng
 * @Description: java类作用描述
 * @Date:2019/1/6
 * @Time:11:40
 */
@RestController
@RequestMapping("/provider")
public class UserController {

    @RequestMapping("/user/{id}")
    public User getUser(@PathVariable(name = "id") Long id){
        return new User(id,"test");
    }
}

4.写springcloud服务提供端的配置文件

注意:这里的配置文件名 为 application.yml 

server:
  port: 8001
eureka:
  client:
    service-url:
     #defaultZone: http://localhost:7001/eureka #告诉服务提供者要把服务注册到哪里
     defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7002.com:7002/eureka #集群环境下的配置
  instance:
    prefer-ip-address: true #显示客户端ip
spring:
  application:
    name: USER-PROVIDER #这是服务提供者在注册中心的别名,别名不要用下划线

5.启动注册中心入口类后,启动服务提供端入口类后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值