标签:unit 本机 str 国内 ota clean 作用 打印 serve
取名为eureka-server
,并添加如下Dependencies:
application.properties
#配置端口
server.port=8761
#spring 的应用名=一般是模块名
spring.application.name=eureka-server
#当前模块是否注册为eureka的客户端
# --->因为当前应用应该是服务端
# --->所以选false
eureka.client.register-with-eureka=false
#既然不是客户端,那么是否
# 在Server Center 注册?
# --->不要,因为当前应用本来就是server
eureka.client.fetch-registry=false
#------------------------------------------
#eureka.客户端.服务url.默认区域,${server.port} 动态获取port-->即第1行的8761
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka
@EnableEurekaServer
//启用Eureka服务器
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
本机访问http://localhost:8761/
有画面则说明启动成功
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept implements Serializable {
private Integer deptno;
private String dname;
private String loc;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private java.sql.Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
}
clean
然后再install
打包到本地仓库如上图,有就行了...
Spring Initializr
工程<dependency>
<groupId>cn.kgc</groupId>
<artifactId>employee-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--修改版本号-->
<version>5.1.38</version>
</dependency>
DeptMapper.java
@Mapper
public interface DeptMapper {
@Select("select * from dept")
List<Map<String,Object>>selAllDeptData();
}
DeptService.java
public interface DeptService {
List<Map<String,Object>> optionData();
}
DeptServiceImpl.java
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Map<String, Object>> optionData() {
return deptMapper.selAllDeptData();
}
}
CenterController.java
@RestController
public class CenterController {
@Autowired
private DeptService deptService;
@GetMapping("/optionData.do")
public List<Map<String, Object>> optionData() {
return deptService.optionData();
}
}
application.properties
#服务端口号
server.port=8762
#应用名
spring.application.name=provider
#eureka客户端服务url默认区域
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#数据源驱动类名
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#数据源url,www.52cc.monster为我的服务器,但是可能在国外,time out 恶心死我了 还是用回localhost或者国内阿里的
spring.datasource.url=jdbc:mysql://www.52cc.monster:3306/kh75
#数据源用户名
spring.datasource.username=root
#数据源密码
spring.datasource.password=****
#后期会写mapper.xml,这里先注释
#mybatis.mapper-locations=classpath:mapper/*.xml
#给实体类起别名,同样这里先注释
#mybatis.type-aliases-package=cn.kgc.vo
ProviderOneApplication.java
//启用eureka客户端
@EnableEurekaClient
//Mapper扫描,对应的mapper包
@MapperScan("cn.kgc.mapper")
@SpringBootApplication
public class ProviderOneApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderOneApplication.class, args);
}
}
Spring Initializr
工程<dependency>
<groupId>cn.kgc</groupId>
<artifactId>employee-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
CenterFeign.java
/**
* 是consumer调用provider(需要指定provider的名字)
* 请求的清单列表:规定调用地址、参数、返回值
*/
@FeignClient(name = "provider")
public interface CenterFeign {
@GetMapping("/optionData.do")
public List<Map<String, Object>> optionData();
}
CenterController
CenterController.java
@RestController
public class CenterController {
@Autowired
private CenterFeign centerFeign;
@GetMapping("/optionData-consumer.do")
public List<Map<String, Object>> optionData() {
return centerFeign.optionData();
}
}
application.properties
#端口号
server.port=8764
#应用名
spring.application.name=consumer
#eureka客户端服务url默认区域
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#下线名称.ribbon.NF加载平衡规则类名,这里先注释
#provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
ConsumerApplication.java
//开启Feign客户端
@EnableFeignClients
//开启eureka客户端
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
已查出数据....
EmpMapper.java
public interface EmpMapper {
@Select(
"<script>" +
" select d.dname,d.loc,e.* from emp e,dept d WHERE e.deptno=d.deptno" +
" <if test='deptno!=-1 and deptno!=null'>" +
" and e.deptno=#{deptno}" +
" </if>" +
" <if test='empno!=null'>" +
" and e.empno=#{empno}" +
" </if>" +
"</script>" )
List<Map<String,Object>> selEmpData(Emp emp);
@Insert("insert into emp(empno,ename,sal,job,deptno) values(#{empno},#{ename},#{sal},#{job},#{deptno})")
Integer insert(Emp emp);
@Update("update emp set ename=#{ename},sal=#{sal},job=#{job},deptno=#{deptno} where empno=#{empno}")
Integer updEmp(Emp emp);
@Delete("delete from emp where empno=#{empno}")
Integer delEmpByPrimaryKey(Integer empno);
}
? EmpService.java
public interface EmpService {
List<Map<String,Object>> showEmpData(Emp emp);
Integer add(Emp emp);
Integer edit(Emp emp);
Integer del(Integer empno);
}
EmpServiceImpl.java
@Service
@Transactional
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List<Map<String, Object>> showEmpData(Emp emp) {
return empMapper.selEmpData(emp);
}
@Override
public Integer add(Emp emp) {
return empMapper.insert(emp);
}
@Override
public Integer edit(Emp emp) {
return empMapper.updEmp(emp);
}
@Override
public Integer del(Integer empno) {
return empMapper.delEmpByPrimaryKey(empno);
}
}
更新中心控制类
CenterController.java
@RestController
public class CenterController {
@Autowired
private DeptService deptService;
@Autowired
private EmpService empService;
@GetMapping("/optionData.do")
public List<Map<String, Object>> optionData() {
return deptService.optionData();
}
@PostMapping("/showEmpData.do")
//Feign:不支持对象传参,所以要用@RequestBody
public List<Map<String, Object>> showEmpData(@RequestBody Emp emp) {
return empService.showEmpData(emp);
}
@PostMapping("/add.do")
public Integer add(@RequestBody Emp emp) {
return empService.add(emp);
}
@PostMapping("/edit.do")
public Integer edit(@RequestBody Emp emp) {
return empService.edit(emp);
}
@GetMapping("/del.do")
public Integer del(@RequestParam("empno") Integer empno) {
return empService.del(empno);
}
}
更新feign类
@FeignClient(name = "provider")
public interface CenterFeign {
@GetMapping("/optionData.do")
public List<Map<String, Object>> optionData();
@PostMapping("/showEmpData.do")
//Feign:不支持对象传参,所以要用@RequestBody
public List<Map<String, Object>> showEmpData(@RequestBody Emp emp);
@PostMapping("/add.do")
public Integer add(@RequestBody Emp emp);
@PostMapping("/edit.do")
public Integer edit(@RequestBody Emp emp);
@GetMapping("/del.do")
public Integer del(@RequestParam("empno") Integer empno);
}
更新consumer的controller
@RestController
public class CenterController{
@Autowired
private CenterFeign centerFeign;
@GetMapping("/optionData-consumer.do")
public List<Map<String, Object>> optionData() {
return centerFeign.optionData();
}
@PostMapping("/showEmpData-consumer.do")
public List<Map<String, Object>> showEmpData(@RequestBody Emp emp) {
return centerFeign.showEmpData(emp);
}
@PostMapping("/add-consumer.do")
public Integer add(@RequestBody Emp emp) {
return centerFeign.add(emp);
}
@PostMapping("/edit-consumer.do")
public Integer edit(@RequestBody Emp emp) {
return centerFeign.edit(emp);
}
@GetMapping("/del-consumer.do")
public Integer del(@RequestParam("empno") Integer empno) {
return centerFeign.del(empno);
}
}
再用postman测试,都没有问题
注意,这里测试的都是http://localhost:8764/XXX-consumer.do
的url
XXX为consumer的controller中的Mapping映射
步骤和3一样,就名改成two
<dependency>
<groupId>cn.kgc</groupId>
<artifactId>employee-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--修改版本号-->
<version>5.1.38</version>
</dependency>
//启用eureka客户端
@EnableEurekaClient
//Mapper扫描,对应的mapper包
@MapperScan("cn.kgc.mapper")
@SpringBootApplication
public class ProviderTwoApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderTwoApplication.class, args);
}
}
application.properties
,也直接扒拉,把端口号改一改#服务端口号
server.port=8763
#应用名
spring.application.name=provider
#eureka客户端服务url默认区域
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#数据源驱动类名
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#数据源url
spring.datasource.url=jdbc:mysql:///kh75
#数据源用户名
spring.datasource.username=root
#数据源密码
spring.datasource.password=admin
#后期会写mapper.xml,这里先注释
#mybatis.mapper-locations=classpath:mapper/*.xml
#给实体类起别名,同样这里先注释
#mybatis.type-aliases-package=cn.kgc.vo
application.properties
#端口号
server.port=8764
#应用名
spring.application.name=consumer
#eureka客户端服务url默认区域
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#下线名称.ribbon.NF加载平衡规则类名,默认是轮询,现在改为随机,RandomRule:随机选择一个服务节点
provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
/showEmpData.do
中加打印 @PostMapping("/showEmpData.do")
//Feign:不支持对象传参,所以要用@RequestBody
public List<Map<String, Object>> showEmpData(@RequestBody Emp emp) {
System.out.println("provider-one>>>>"+emp);
return empService.showEmpData(emp);
}
/showEmpData.do
中加打印 @PostMapping("/showEmpData.do")
//Feign:不支持对象传参,所以要用@RequestBody
public List<Map<String, Object>> showEmpData(@RequestBody Emp emp) {
System.out.println("provide-two>>>>"+emp);
return empService.showEmpData(emp);
}
/showEmpData-consumer.do
测10次看后台打印是不是随机的one:有6个
two:有4个
所以负载均衡策略是随机的
那现在把上面的注释
#下线名称.ribbon.NF加载平衡规则类名,这里先注释provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
还是测10次,结果:
one:5次
two:5次
为了防止巧合,再测10次一共20次,结果:
one:10次
two:10次
所以,以下这句话起作用了
#下线名称.ribbon.NF加载平衡规则类名,这里先注释provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
employee-common
eureka-server
employee-common
provider-one
provider-two
consumer
Ribbon提供了多钟负载均衡策略:
1 WeightedResponseTimeRule:根据响应时间分配一个weight(权重,大小的概念),响应时间和weight成反比
2.RoundRobinRule:轮询选择服务节点(默认的负载均衡策略)
3.RandomRule:随机选择一个服务节点
4.ZoneAvoidanceRule:综合考虑服务节点所在区域的性能和服务节点的可用性来选择服务节点
5.RetryRule:重试机制.在一个配置时间段内,当选择服节点不成功时会一直尝试重新选择
6.BestAvailableRule:选择一个并发请求最小的服务器节点.
7.AvailabilityFilteringRule:过滤掉因为一直连接失败而被标记为circuit tripped的服务节点和那些高并发的服务节点(active connections超过配置值)
SpringCloud+Eureka+Feign+Ribbon的简化搭建流程和CRUD练习
标签:unit 本机 str 国内 ota clean 作用 打印 serve
原文地址:https://www.cnblogs.com/jsccc520/p/12075547.html