项目2---十次方项目开发---后台--springcloud之初入江湖---07

ip攻击是运维的解决的。

quartz:默认是多线程的,job是要处理的定时任务。job对象默认是单例的。

没执行完毕则停掉。

cron表达式:http://cron.qqe2.com/

七位,周和日不能同时出现。

quartz:https://www.cnblogs.com/notably/p/10607839.html

JWT和单点登陆:一个servlet是一个session。

--------------------------------------------------------------------------反馈----------------------------------------------------------------------------------------

eureka和feign。

springboot和springcloud的版本对应关系:

----------------------------------------------------------02--------------------03----------------------------------------------------------------------------

Eureka:服务发现。

包括两部分:Eureka Server和Eureka Client

----------------------------------------------------04-------------------------------------------------------------------------------------------------------

第一步:在父工程锁定版本。

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

者这个的意思式任意一个jar包扔到这里面版本就锁死了。

第二步:配置文件

server:
   port: 6868
eureka:
   client:
     register-with-eureka: false
     fetchRegistry: false
     service-url:
           defaultZone: http://127.0.0.1:${server.port}/eureak/

第三步:启动类

第四步:访问localhost:6868/

-------------------------------------------------05----------------------------------------------------------服务器的搭建-------------------------------

article客户端的搭建:

第一步:

 <dependency>
		  <groupId>org.springframework.cloud</groupId>
		  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	  </dependency>

第二步:写配置文件

eureka:
   client:
     service-url:
           defaultZone: http://127.0.0.1:6868/eureka/

第三步:启动类启动

第四步:

可以跨域访问。

-------------

注册一个user微服务:

注册base的微服务:

-----------------------------------------------------------------------------06-----------------------------------------------------------------------------------

服务之间的调用:Feign。

目的:在qa里面调用base。

第一步:注册qa到服务中心

第二步:在qa里面导入feign

  <dependency>
		  <groupId>org.springframework.cloud</groupId>
		  <artifactId>spring-cloud-starter-openfeign</artifactId>
	  </dependency>

第三步:

注意那个名字就是微服务的名字。

调用这个方法:这个是base的。

注意一点:

以前1和2对上就可以了,用feign是不可以的。接口不看后面。

第四步:服务发现

第六步:测试。

这个是一个接口,压根没有实现类,我们去调用别人的。

	@GetMapping("/label/{labelId}")
	public Result findByLabelId(@PathVariable String labelId){
		Result result = baseClient.findById(labelId);
		return result;
	}

第七部:测试报错。

解决办法:https://www.cnblogs.com/jhwy/p/9772303.html

-----------------------------------------------------------------------------07-----------------------------------------------------------------------------------

负载均衡:springcloud给你做了。

-----------------------------------------------------------------------------08-----------------------------------------------------------------------------------

交友微服务的开发:

第一步:

建表语句:

-- auto-generated definition
create table tb_nofriend
(
  userid   varchar(20) not null
  comment '用户ID',
  friendid varchar(20) not null
  comment '好友ID',
  primary key (userid, friendid)
);

第二步:

点击

相互喜欢是islike。

取消喜欢的话几步操作?

1.删除好友

2.加非好友

3.不管是用户id还是friendid送都是来自于tb_user。

a喜欢b就是 a多关注了一个人b多了一个粉丝。

原来就没有关系直接不喜欢。

原本是好友删掉了。a关注数-1,b的粉丝数-1.

第二步的逻辑具体还要看下文档。

-----------------------------------------------------------------------------09-----------------------------------------------------------------------------------

第三步:搭建friend模块。

写yml文件。

server:
  port: 9010
spring:
  application:
    name: tensquare-friend
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.244.136:3306/tensquare_friend?serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456
  jpa:
    show-sql: true
    database: mysql
eureka:
  client:
    service-url:
      defaultZone: http://localhost:6868/eureka/
    fetchRegistry: true
  instance:
    prefer-ip-address: true #可以跨域访问,不加的话只能本地访问
jwt:
  config:
    key: itcast
    ttl: 3600000

写启动类:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FriendApplication {
    public static void main(String[] args) {

        SpringApplication.run(FriendApplication.class,args);
    }

    @Bean
    public JwtUtil jwtUtil(){
        return new JwtUtil();
    }
}

拿过滤器的一套东西。

拿这个用户。

-----------------------------------------------------------------------------10-----------------------------------------------------------------------------------

业务编写:

1.添加好友:

联合主键:

1表示联合主键,其实1没有起作用,2起作用了。

写一系列方法:

-----------------------------------------------------------------------------11-----------------------------------------------------------------------------------

添加好友测试:略。

-----------------------------------------------------------------------------12-----------------------------------------------------------------------------------

添加非好友,原来就不是好友添加非好友。

拉黑:在个人中心的好友列表。

-----------------------------------------------------------------------------13-----------------------------------------------------------------------------------

用到feign了:粉丝和关注的更新。

第一步:user模块更新粉丝数。

/**
	 * 更新好友粉丝数和用户关注数
	 * @param userid
	 * @param friendid
	 */
	@PutMapping("/{userid}/{friendid}/{num}")
	public void updateFanscountAndFollowcount(@PathVariable("userid") String userid,@PathVariable("friendid") String friendid,@PathVariable("num") int num){

		userService.updateFanscountAndFollowcount(userid,friendid,num);
	}
@Transactional
	public void updateFanscountAndFollowcount( String userid, String friendid,int num) {

		userDao.updateFanscount(num,friendid);
		userDao.updateFollowcount(num,userid);
	}

这个代码看下官方的代码。

-----------------------------------------------------------------------------14-----------------------------------------------------------------------------------

微服务friend想用它。

-----------------------------------------------------------------------------15-----------------------------------------------------------------------------------

删除好友:

@DeleteMapping("/{friendid}")
    public Result deleteFriend(@PathVariable("friendid")String friendid){
        //验证是否登录,并且拿到当前登录的用户的id
        Claims claims=(Claims)request.getAttribute("claims_user");
        if(claims==null ){
            //当前用户没有user角色
            return new Result(false,StatusCode.LOGINERROR,"权限不足");
        }
        //得到当前登录用户的id
        String userId = claims.getId();
        friendService.deleteFriend(userId,friendid);
        userClient.updateFanscountAndFollowcount(userId,friendid,-1);
        return new Result(true,StatusCode.OK,"删除成功");
    }

-----------------------------------------------------------------------------16-----------------------------------------------------------------------------------

feign配置负载均衡:http://blog.itpub.net/31558358/viewspace-2565058/

发布了308 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_28764557/article/details/104247186
今日推荐