搭建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 #这是服务提供者在注册中心的别名,别名不要用下划线