SpringCloud
eureka
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
pom.xml
<dependencies>
<!--eureka服务器端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 9008
eureka:
client:
register-with-eureka: false # 是否将该服务注册到eureka服务端
fetch-registry: false # 是否从eureka服务端获取其他服务实例
service-url: # eureka的注册地址
defaultZone: http://127.0.0.1:${server.port}/eureka
FriendApplication(启动器)
@SpringBootApplication
@EnableEurekaClient // 开启Eureka客户端
@EnableDiscoveryClient // 开启微服务发现
@EnableFeignClients // 开启微服务远程调用
public class FriendApplication {
public static void main(String[] args) {
SpringApplication.run(FriendApplication.class, args);
}
@Bean
public IdWorker idWorker(){
return new IdWorker();
}
}
FriendService
package com.ktc.friend.service;
import com.ktc.friend.client.UserClient;
import com.ktc.friend.dao.NofriendDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ktc.friend.dao.FriendDao;
import com.ktc.friend.pojo.Friend;
/**
* @Description tb_friend 服务层
* @author admin
* @date 2023-07-24 18:20:25
*/
@Service
public class FriendService {
@Autowired
private FriendDao friendDao;
@Autowired
private NofriendDao noFriendDao;
//@Qualifier("ktc-user")因为同时具有远程服务和服务降级,会出现报红
@Autowired
private UserClient userClient;
/**
* 加关注/取消关注
* 1,3
*
* @param userid
* @param friendid
* @return
*/
@Transactional
public Boolean like(String userid, String friendid) {
// 我是来加关注的还是取消关注的啊?
Integer count = friendDao.countByUseridAndFriendid(userid, friendid);
if (count > 0) {
// 我关注过了他,我是来取消关注的
friendDao.deleteByUseridAndFriendid(userid, friendid);
// 这个人有没有喜欢我
if (friendDao.countByUseridAndFriendid(friendid, userid) > 0) {
// 我是喜欢他的,他也喜欢我,我们两个的isLike为1,只不过我喜欢他的这条记录被删除了,但是他喜欢我的那条记录的isLike改为0
friendDao.updateIslike(friendid, userid, "0");
}
userClient.updateFollowCount(userid,-1);
userClient.updateFansCount(friendid,-1);
return false;
} else {
// 我没有关注过他,我是来关注的
Friend friend = new Friend();
friend.setUserid(userid);
friend.setFriendid(friendid);
friend.setIslike("0");
if (friendDao.countByUseridAndFriendid(friendid, userid) > 0) {
friend.setIslike("1");
friendDao.updateIslike(friendid, userid, "1");
}
friendDao.save(friend);
// 我的关注数+1,对方的粉丝数+1
userClient.updateFollowCount(userid,1);
userClient.updateFansCount(friendid,1);
return true;
}
}
UserClient(远程服务)
package com.ktc.friend.client;
import com.ktc.friend.client.impl.UserClientImpl;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author lscl
* @version 1.0
* @intro:
*/
@FeignClient(value = "ktc-user",fallback = UserClientImpl.class)//fallback开启服务降级
public interface UserClient {
/**
* 更新粉丝数
* @param userid
* @param count
* @return
*/
@RequestMapping(value = "/user/updateFansCount/{userid}/{count}", method = RequestMethod.PUT)
public Result updateFansCount(@PathVariable("userid") String userid, @PathVariable("count") Integer count) ;
/**
* 更新关注数成功
* @param userid
* @param count
* @return
*/
@RequestMapping(value = "/user/updateFollowCount/{userid}/{count}", method = RequestMethod.PUT)
public Result updateFollowCount(@PathVariable("userid") String userid, @PathVariable("count") Integer count);
}
hystrix(熔断器)
pom.xml
<!--引入feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
feign:
hystrix: # 开启熔断器
enabled: true
command: # 请求超时降级
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2000 # 修改超时时间为2s,默认为1s
UserClientImpl(服务降级)
package com.ktc.friend.client.impl;
import com.ktc.friend.client.UserClient;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author lscl
* @version 1.0
* @intro:
*/
@Component
public class UserClientImpl implements UserClient {
/**
* 更新粉丝数
* 服务降级方法
* @param userid
* @param count
* @return
*/
@Override
public Result updateFansCount(@PathVariable("userid") String userid, @PathVariable("count") Integer count){
System.out.println("访问降级了!");
return new Result(false, StatusCode.ERROR, "访问降级了!");
}
/**
* 更新关注数成功
* 服务降级方法
* @param userid
* @param count
* @return
*/
@Override
public Result updateFollowCount(@PathVariable("userid") String userid, @PathVariable("count") Integer count){
System.out.println("访问降级了!");
return new Result(false, StatusCode.ERROR, "访问降级了!");
}
}