微服务 : 使用eclipse 来搭建一个简单的微服务

下载地址:https://download.csdn.net/download/qq_18430613/10642372

1.先看看整体项目的一个架构,parent 项目父依赖包,用于管理我们的依赖,provider 服务提供方,consumer 消费者。

微服务 : 使用eclipse 来搭建一个简单的微服务_第1张图片

2.创建parent 项目

1)创建无骨架的maven pom项目

微服务 : 使用eclipse 来搭建一个简单的微服务_第2张图片

2)修改pom包 如下步骤:

https://projects.spring.io/spring-cloud/ 修改maven parent


        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
        
    


        


            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.SR1
                pom
                import
            

        
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                1.3.2
            

        
    


        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                

            

            
                org.springframework.boot
                spring-boot-maven-plugin
            

        
    


        ../provider
    

3.添加provider 服务提供项目

右键选择》new 》spring-start-project

微服务 : 使用eclipse 来搭建一个简单的微服务_第3张图片

微服务 : 使用eclipse 来搭建一个简单的微服务_第4张图片

修改provider服务提供方pom文件如下:

微服务 : 使用eclipse 来搭建一个简单的微服务_第5张图片

修改application.properties文件为yml 文件(红色部分为大小写转换)

微服务 : 使用eclipse 来搭建一个简单的微服务_第6张图片

编写代码部分:

微服务 : 使用eclipse 来搭建一个简单的微服务_第7张图片

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public UserInfo getUserInfo(Integer userId) {
		return userMapper.getUserById(userId);
	}

}


@Mapper
public interface UserMapper {
	
	@Select("SELECT user_id,user_name,user_birth FROM tb_user WHERE user_id = #{userId}")
	public UserInfo getUserById(@Param(value = "userId") Integer userId);

}


public class UserInfo implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Integer userId;
	private String userName;
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date userBirth;
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Date getUserBirth() {
		return userBirth;
	}
	public void setUserBirth(Date userBirth) {
		this.userBirth = userBirth;
	}
	
}

服务提供方到此完成 可以启动访问:http://localhost:8080/book/books

4.添加消费consumer项目,直接复制provide修改相关文件如下

微服务 : 使用eclipse 来搭建一个简单的微服务_第8张图片

provider-consumer pom文件修改

微服务 : 使用eclipse 来搭建一个简单的微服务_第9张图片

添加代码部分:

@SpringBootApplication
@RestController
public class ProviderApplication {
	
	@Value("${spring.application.name}")
	private String applicationName;
	
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

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

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping("/warp/get/{userId}")
	public UserInfo getUser(@PathVariable Integer userId) {
		return restTemplate.getForObject("http://localhost:8099/user/get/"+userId, UserInfo.class);
	}
}

public class UserInfo implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Integer userId;
	private String userName;
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date userBirth;
	
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public Date getUserBirth() {
		return userBirth;
	}
	public void setUserBirth(Date userBirth) {
		this.userBirth = userBirth;
	}
	
}

//application.yml文件
server:
  port: 8100

spring:
  application:
    name: provider-consumer

测试:http://localhost:8100/user/warp/get/1

 

你可能感兴趣的:(springcloud)