org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
1.0.2.RELEASE
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.cloud
spring-cloud-netflix
1.2.5.RELEASE
pom
import
server:
port: 9997
spring:
application:
#服务名称,CLIENT端用来享受服务
name: USERSERVER
cloud:
zookeeper:
#ZK_ADDRESS
connect-string: host219:2181,host223:2181,host224:2181
discovery:
register: true
#服务地址
instance-host: 10.150.27.29
#服务端口
instance-port: ${server.port}
enabled: true
@SpringBootApplication
@RestController
//服务注册到ZK
@EnableDiscoveryClient
public class ZkServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZkServerApplication.class, args);
}
}
public class Bean {
private String name;
private String likes;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLikes() {
return likes;
}
public void setLikes(String likes) {
this.likes = likes;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
@RestController
public class UserService {
// PathVariable
@RequestMapping("/user/{id}")
public Bean getUserInfo(@PathVariable("id") String id) {
Bean bean = new Bean();
bean.setLikes("Orange");
bean.setName("lili");
bean.setId(id);
return bean;
}
// Path
@RequestMapping
public Bean getUserList(@RequestParam(required = true, value = "id") String id) {
Bean bean = new Bean();
bean.setLikes("Banana");
bean.setName("Mr Wang");
bean.setId(id);
return bean;
}
}
Server端总结:每启动一个Server端实例SpringCloud向ZK以"USERSERVER"为服务唯一标识在ZK "/services"节点下以该服务标识为路径注册服务,该特性由"@EnableDiscoveryClient" 注解及application.yml中相关配置进行驱动.
Client端实现
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.cloud
spring-cloud-starter-zookeeper-discovery
1.0.2.RELEASE
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-devtools
true
org.springframework
spring-jdbc
oracle
ojdbc14
10.2.0.1.0
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-jdbc
server:
port: 9990
spring:
application:
name: USERCLIENT
cloud:
zookeeper:
enabled: true
connect-string: host219:2181,host223:2181,host224:2181
discovery:
root: /services
register: true
datasource:
url: jdbc:oracle:thin:@host233:1521/orcl
username: root
password: root
driver-class-name : oracle.jdbc.driver.OracleDriver
@SpringBootApplication
// 启用REST 客户端
@EnableFeignClients
// 启用客户端负载均衡
@RibbonClient(name = "my", configuration = RibbonConfig.class)
// 启用服务发现
@EnableDiscoveryClient
// 启用断路器
@EnableHystrix
@ImportResource(value = "spring_mvc.xml")
@ComponentScan("cn.com.xiaofen")
public class ZkClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZkClientApplication.class, args);
}
/* Spring JDBC */
@Bean
public JdbcTemplate primaryJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/* TOMCAT DataSourcePool */
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
@RestController
public class UserController {
@Autowired
private UserService us;
@RequestMapping("/user/{id}")
public String getUserInfo(@PathVariable("id") String id) {
return us.getUserInfo(id);
}
@RequestMapping("/user")
public String getUSerList(@RequestParam("id") String id) {
return us.getUSerList(id);
}
}
@FeignClient("USERSERVER")
public interface UserService {
@RequestMapping("/user/{id}")
public String getUserInfo(@PathVariable("id") String id);
@RequestMapping("/user")
public String getUSerList(@RequestParam("id") String id);
}
@Controller
public class ByonetController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/byonetListFreeMaker")
public String byonetListFreeMaker(Map model) {
String sql = "select kkid,kkmc,x,y from t_itgs_tgsinfo";
BeanPropertyRowMapper rowMapper = new BeanPropertyRowMapper(Byonet.class);
List byonets=jdbcTemplate.query(sql, rowMapper);
model.put("byonets", byonets);
return "byonetList";
}
}
FreeMaker模版,路径src/main/resources/remplates/byonetList.ftl
卡点列表
Our latest product:
KKID
KKMC
X
Y
<#list byonets as byonet>
${byonet.kkid}
${byonet.kkmc}
${ (byonet.x)! }
${ (byonet.y)!}
#list>
controller
@RestController
public class ByonetControllerRest {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/byonetList")
public List byonetList() {
String sql = "select kkid,kkmc,x,y from t_itgs_tgsinfo";
BeanPropertyRowMapper rowMapper = new BeanPropertyRowMapper(Byonet.class);
return jdbcTemplate.query(sql, rowMapper);
}
@RequestMapping("/byonetList/{kkid}")
public List byonetListById(@PathVariable(required=true,name="kkid")String kkid) {
String sql = "select kkid,kkmc,x,y from t_itgs_tgsinfo where kkid=?";
BeanPropertyRowMapper rowMapper = new BeanPropertyRowMapper(Byonet.class);
return jdbcTemplate.query(sql, rowMapper,kkid);
}
}
Insert title here
RPC测试
info_1
info_2
FreeMaker测试
/byonetListFreeMaker
/homePage
AngularJS 测试