springcloud入门案例
首先搭建好eureka服务,教程:https://www.cnblogs.com/Y-wee/p/14129138.html
1、公共模块
新建一个普通项目作为公共模块项目,编写一些公共代码,实现代码复用,案例项目名为:common
- 编写实体类,作为公共代码
package com.yl.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private String username;
private String password;
}
2、服务提供者
新建项目继承父工程,作为服务提供者,案例项目名:server
2.1、修改pom文件
- 继承父工程
<parent>
<groupId>com.yl</groupId>
<artifactId>springcloue_parent_02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--公共模块-->
<dependency>
<groupId>com.yl</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--配置eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置web的starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2、控制器
package com.yl.controller;
import com.yl.bean.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServerController {
@RequestMapping("/getUser")
public User getUser(){
User user=new User("yl01","1111");
return user;
}
}
2.3、全局配置文件
spring:
application:
name: server
server:
port: 8801
#eureka的基本信息
eureka:
instance:
hostname: 127.0.0.1
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
#它本身是一个普通的服务,需要在eureka-server注册
register-with-eureka: true
#需要获取注册信息
fetch-registry: true
2.4、启动类添加@EnableEurekaClient注解
package com.yl;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ServerApplication {
public static void main(String[] args) {
// SpringApplication.run(ServerApplication.class, args);
SpringApplication springApplication=new SpringApplication(ServerApplication.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
}
}
2.5、测试服务提供者是否搭建成功
- 启动eureka服务
- 启动服务提供者
- 浏览器访问http://localhost:7001
- 查看是否有SERVER服务
3、服务消费者
新建项目继承父工程,作为服务消费者,案例项目名:client
3.1、修改pom文件
- 继承父工程
<parent>
<groupId>com.yl</groupId>
<artifactId>springcloue_parent_02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--公共模块-->
<dependency>
<groupId>com.yl</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--配置eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--支持feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--支持hystrix组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3.2、业务层接口,使用feign实现
package com.yl.service;
import com.yl.bean.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "SERVER")//调用的服务名
public interface IUserService {
@RequestMapping("/getUser")//调用的服务地址
User getUser();
}
3.3、控制器
package com.yl.controller;
import com.yl.bean.User;
import com.yl.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/getUser")
public User getUser(){
return userService.getUser();
}
}
3.4、全局配置文件
spring:
application:
name: client
server:
port: 8080
#eureka的基本信息
eureka:
instance:
hostname: 127.0.0.1
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
#它本身是一个普通的服务,需要在eureka-server注册
register-with-eureka: true
#需要获取注册信息
fetch-registry: true
3.5、启动类添加@EnableEurekaClient注解和@EnableFeignClients注解
package com.yl;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ClientApplication {
public static void main(String[] args) {
// SpringApplication.run(ClientApplication.class, args);
SpringApplication springApplication=new SpringApplication(ClientApplication.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
}
}
3.6、测试服务消费者是否搭建成功
- 启动eureka服务
- 启动服务提供者
- 启动服务消费者
- 浏览器访问:http://localhost:8080/getUser
4、业务层使用RestTemplate实现
如果不使用feign,使用RestTemplate实现,则需要编写一个配置类
@Configuration
public class RestTemplateConfig {
//配置RestTempleate成为bean
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
业务层接口不使用注解实现,使用实现类去实现
@Service
public class IUserServiceImpl implements IUserService {
@Autowired
RestTemplate restTemplate;
@Override
public User getUser() {
String url="http://SERVICE/getUser";//通过http restful api实现远程调用服务
User user=restTemplate.getForObject(url,User.class);
return user;
}
}