7.我的springCloud 代码工程例子:https://git.oschina.net/andrexu2015/mindSpringCloudMicroDemo,欢迎下载。
### 1. spingBoot简单介绍
package com.calm.b;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("com.calm.*")
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
@ImportResource(value = {"applicationContext.xml"})
@EnableAspectJAutoProxy
public class BizApplication {
/**
* 程序的启动入口
*
* @param args
*/
public static void main(String[] args) {
// SpringApplication.run(BizApplication.class, args);
new SpringApplicationBuilder(BizApplication.class).web(true).run(args);
}
}
构建spingboot工程,只需要在所有package目录最根层建立一个main方法class,配上@Configuration,@ComponentScan,@EnableAutoConfiguration注解就能够启动springBoot程序,@SpringBootApplication是前三个注解默认配置的。
1. @Configuration 代表该component是一个spring容器定义的配置bean。
2. @ComponentScan 代表spring容器需要扫表java组件包路径范围。默认是当前class所有在包位置。
3. @EnableAutoConfiguration 启动自动获取配置的开关,exclude是指可以在排除需要的configuration bean。
#service discovery url
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
@RequestMapping(value = "/monitor")
public void getDishSetmealGroup(HttpServletRequest request, HttpServletResponse response)
throws ServletRequestBindingException {
InstanceInfo instanceInfo = discoveryClient.getNextServerFromEureka("cloudSimpleService", false);
System.out.println(instanceInfo.getHealthCheckUrl());
serviceUrl();
}
public String serviceUrl() {
// discoveryClient.geti
List list = discoveryClient.getInstancesById("my-THINK:cloudSimpleService:8082");
if (list != null && list.size() > 0) {
System.out.println(list.get(0).getHomePageUrl());
}
Application application = discoveryClient.getApplication("cloudSimpleService");
list = application.getInstances();
if (list != null && list.size() > 0) {
System.out.println(list.get(0).getHealthCheckUrl());
}
return null;
}
<instanceId>my-THINK:cloudSimpleService:8082instanceId>
<hostName>my-THINKhostName>
<app>CLOUDSIMPLESERVICEapp>
<ipAddr>192.168.7.197ipAddr>
<status>UPstatus>
<overriddenstatus>UNKNOWNoverriddenstatus>
<port enabled="true">8082port>
<securePort enabled="false">443securePort>
<countryId>1countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwnname>
dataCenterInfo>
7.参考如下http://www.tuicool.com/articles/JBbU3mY ,描述EurekaClient的注册过程。
参考spring 官方文档
参考spring 官方文档
@Configuration
public class BeanConfiguration {
@LoadBalanced
@Bean
// @Order()
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@Service("usserService")
public class UserService {
@Resource
private RestTemplate restTemplate;
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
final String SERVICE_NAME = "cloudSimpleService";
// @HystrixCommand(fallbackMethod = "fallbackSearchAll")
public List searchAll() {
logger.info("====================================");
List users=null;
try{
users=restTemplate.getForObject("http://" + SERVICE_NAME + "/user", List.class);
}catch(Exception e){
e.printStackTrace();
}
logger.info("====================================");
return users;
}
public List searchAllTwo() {
logger.info("====================================");
List users=null;
try{
users=restTemplate.postForObject("http://" + SERVICE_NAME + "/user",null, List.class);
//restTemplate.exchange(url, method, requestEntity, responseType)
//restTemplate.delete(url);
}catch(Exception e){
e.printStackTrace();
}
logger.info("====================================");
return users;
}
主要通spring.application.name=cloudSimpleService 名字+url来访问具体的服务。
dev –代表自己的环境
– apps:customized – 所有appInstance配置
– cloudSimpleService –测试的服务应用
— —该app自己的属性定义
#App Config Dashboard (ACD) dump created on :Wed Nov 02 15:09:00 CST 2016
/configurations/mind/dev/apps=foo=bar
/configurations/mind/dev/apps:customized=foo=bar
/configurations/mind/dev/apps:customized/customized=age=22222222222
/configurations/mind/dev/apps:customized/customized=foo=bar
/configurations/mind/dev/cloudSimpleService=foo=bar
/configurations/mind/dev/cloudSimpleService/customized=foo=bar
/configurations/mind/dev/cloudSimpleService/customized=password=1wswddewwpassword
/configurations/mind/dev/cloudSimpleService/customized=userName=user12345andrex
/configurations/mind/dev/cloudSimpleService/mysqldb=foo=bar
/configurations/mind/dev/tradeAnalyzer=andrexu2=11
/configurations/mind/dev/tradeAnalyzer=foo=bar
/configurations/mind/dev/tradeAnalyzer=testUserNode=12433
#service name
spring.application.name=cloudSimpleService
spring.profiles.active=dev,datasource,mail,customized
# zookeeper所有集群address
spring.cloud.zookeeper.connectString=testzk1.shishike.com:2181,testzk2.shishike.com:2181,testzk3.shishike.com:2181
#是否开启zookeeper使用
spring.cloud.zookeeper.config.enable=true
#zokkeeper配置的根目录
spring.cloud.zookeeper.config.root=/configurations/mind/dev
spring.cloud.zookeeper.config.defaultContext=apps
spring.cloud.zookeeper.config.profileSeparator=:
@Component
@ConfigurationProperties(prefix="customized")
public class CustomizedProperties {
//@NotNull
private String userName;
//@NotNull
private String password;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}