1.整体的运行环境:
eclipse 4.6.2 | sts 3.9.3 | jdk 1.8 | tomacat 7.0.75 | springboot 2.0.0 | dubbo 2.5.3 | zkclient 0.1| zookeeper 3.4.10 |
2. Zookeeper 的搭建
关于 Zookeeper 的介绍和搭建,博主写了另外一篇博文,比较简短,详情请看:http://blog.csdn.net/u013142781/article/details/50395650
项目结构
pom.xml不需要更改
实体类Emp.java 代码如下:
package com.springboot.dao;
import java.io.Serializable;
public class Emp implements Serializable{ //最重要的是实现这个序列化,不然页面访问会报错
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer gender;
private Integer deptId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
}
接口 DemoService.java 代码如下:
package com.springboot.service;
import java.util.List;
import com.springboot.dao.Emp;
public interface DemoService {
/**
* 根据id取emp对象
* @param id
* @return
*/
public Emp findEmp(int id);
/**
* 根据id 删除emp对象
*/
public void delEmp(int id);
/**
* 根据id修改emp对象的值
*/
public void modEmp(Emp emp);
/**
* 添加对象
*/
public void addEmp(Emp emp);
/**
* 查找全部对象
*/
public List findEmps();
}
3.2 ===provider服务提供者
首先是pom.xml文件(最容易出错的是jar包的版本不兼容)
4.0.0
com.springboot
Provide
0.0.1-SNAPSHOT
jar
Provide
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
com.springboot
Api-1
0.0.1-SNAPSHOT
com.alibaba
dubbo
2.5.3
org.springframework
spring
com.github.sgroschupf
zkclient
0.1
org.apache.zookeeper
zookeeper
3.4.10
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
junit
junit
3.8.1
test
提供者:mapper.java
package com.springboot.mapper;
import java.util.List;
import com.springboot.dao.Emp;
public interface EmpMapper {
//mybatis直接使用注解的方式
/* @Select("select * from emp where id = #{id}")
Emp selectById(@Param("id")Integer id); */
/**
* 根据id取emp对象
* @param id
* @return
*/
public Emp findEmp(int id);
/**
* 根据id 删除emp对象
*/
public void delEmp(int id);
/**
* 根据id修改emp对象的值
*/
public void modEmp(Emp emp);
/**
* 添加对象
*/
public void addEmp(Emp emp);
/**
* 查找全部对象
*/
public List findEmps();
}
提供者:DemoServiceImpl.java
package com.springboot.sevice.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.dao.Emp;
import com.springboot.mapper.EmpMapper;
import com.springboot.service.DemoService;
@Service("demoService") //查找接口
public class DemoServiceImpl implements DemoService{
@Autowired
private EmpMapper empMapper;
/**
* 根据id取emp对象
*/
@Override
public Emp findEmp(int id) {
Emp emp=empMapper.findEmp(id);
System.out.println(emp.getName());
return emp;
}
/**
* 根据id 删除emp对象
*/
@Override
public void delEmp(int id) {
empMapper.delEmp(id);
System.out.println("删除");
}
/**
* 根据id修改emp对象的值
*/
@Override
public void modEmp(Emp emp) {
empMapper.modEmp(emp);
System.out.println("修改");
}
/**
* 添加对象
*/
@Override
public void addEmp(Emp emp) {
empMapper.addEmp(emp);
System.out.println("添加");
}
/**
* 查找全部对象
*/
@Override
public List findEmps() {
List lists =empMapper.findEmps();
System.out.println("全查");
return lists;
}
}
提供者:启动类App.java(启动类所在位置不要改变,必须包含所有其他包,启动才有效)
package com.springboot;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@MapperScan("com.springboot.mapper")
@ImportResource(locations={"provide.xml"})
@SpringBootApplication
public class App {
/* @Autowired
private Environment env; */
private static volatile boolean running = true;
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return new com.alibaba.druid.pool.DruidDataSource();
}
//提供SqlSeesion
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml")); //最重要的是读取sql语句的xml文件
return sqlSessionFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "服务提供者注册成功" );
}
}
提供者:EmpMapper.xml (sql语句)
//mapper接口所在的类
id, name, gender, deptid
delete from emp where id= #{id}
update emp set name=#{name},gender =#{gender},deptid=#{deptId} where id=#{id}
insert into emp(name,gender,deptId) values(#{name},#{gender},#{deptId})
提供者:application.properties
spring.datasource.url:jdbc:mysql://localhost:3306/company
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
dubbo.registry.center=127.0.0.1:2181
server.port=8181
提供者:provide.xml
//有多少接口就要暴露多少,不然消费者调用不到
3.3===consume服务消费者
首先是pom.xml
4.0.0
com.springboot
Consume-1
0.0.1-SNAPSHOT
jar
Consume-1
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
com.springboot
Api-1
0.0.1-SNAPSHOT
com.alibaba
dubbo
2.5.3
org.springframework
spring
com.github.sgroschupf
zkclient
0.1
org.apache.zookeeper
zookeeper
3.4.10
org.slf4j
slf4j-log4j12
org.springframework.boot
spring-boot-starter-web
junit
junit
3.8.1
test
消费者:DemoController.java
package com.springboot.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor;
import com.springboot.dao.Emp;
import com.springboot.service.DemoService;
@RestController //RestController 相当于restbody+controller,所以默认返回的是json数据
@RequestMapping("/user")
public class DemoController {
@Autowired
private DemoService demoService;
Map map = new HashMap<>(); //返回的字符串接收不到 所以改成了map集合
/**
* 根据id取emp对象
* @param id
* @return
*/
@RequestMapping("/getEmp.do")
public Emp getEmp(int id){
Emp emp = demoService.findEmp(id);
return emp;
}
/**
* 查找全部对象
*/
@RequestMapping("/findEmps.do")
public List findEmpss(){
List emps = demoService.findEmps();
System.out.println("全查");
return emps;
}
/**
* 根据id 删除emp对象
*/
@RequestMapping("/delById.do")
public String delEmpById(){
demoService.delEmp(3);
return "success";
}
/**
* 根据id修改emp对象的值
*/
@RequestMapping("/modById.do")
public String modById(Emp emp){
/* emp.setId(2);
emp.setName("dfjjk");
emp.setGender(3);
emp.setDeptId(5);*/
demoService.modEmp(emp);
return "success";
}
/**
* 添加对象
*/
@RequestMapping("/addById.do")
public Map addById(Emp emp){
/* emp.setName("大家风范");
emp.setGender(2);
emp.setDeptId(5);*/
demoService.addEmp(emp);
map.put("fanhui", "success");
return map;
}
}
为了解决跨域问题CorsConfig.java
package com.springboot.util;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class CorsConfigs extends WebMvcConfigurerAdapter{
/**
* 解决前端站点(主要为JavaScript发起的Ajax请求)访问的跨域问题
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") //允许所有前端站点调用
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT")
.maxAge(1728000);
}
}
消费者:启动类App.java
package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@ImportResource("classpath:consume.xml")
@SpringBootApplication
public class App {
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "消费者调用服务成功!" );
}
}