微服务考试:

1.考试电脑idea改4个设置:

code、font、compact middle packages、general

1.更换阿里源:
https://start.aliyun.com


2.所用依赖:

maven依赖下载网址:
https://mvnrepository.com/

//用到的依赖

//1.server
idea直接勾选自动生成依赖(web、eureka-server)

//2.provider
    2.1 公共模块
    2.2 log4j:
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    2.3 pageHelper:
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>
其余在创建项目时勾选(web、jdbc、mysql、mybatis、eureka-client)

//3.consumer
    3.1  公共模块
    3.2  log4j:
    3.3  openFeign:
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>${spring-boot.version}</version>
        </dependency> 
    3.4  hystrix:
         <dependency>
           <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
其余在创建项目时勾选(web、eureka-client)    


3.springboot和springcloud版本对应关系:
2.0.1.RELEASE------Finchley.SR3

4.格式化日期:
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private Date updateDate;

Date date = new Date();
Timestamp timeStamp = new Timestamp(date.getTime());
house.setPublishDate(date);
5.mybatis配置:
=============configuration==============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
    <mappers>
        <mapper resource="mapper/houseMapper.xml"/>
    </mappers>
</configuration>


====================mapper================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.susu.contact_provider.mapper.ContactMapper">
    <!--查询所有联系人信息-->
    <select id="getContactMsg" parameterType="com.susu.pojo.Contact" resultType="com.susu.pojo.Contact">
        select *
        from contact
        <where>
            <if test="cname != null and cname != '' ">
                cname like concat('%' #{cname} '%')
            </if>
        </where>
    </select>
    <!--新增联系人信息-->
    <insert id="addContactMsg" parameterType="com.susu.pojo.Contact">
        insert into contact (id, cname, mobile, qq, email, address)
        values (#{id}, #{cname}, #{mobile}, #{qq}, #{email}, #{address})
    </insert>
    <!--删除联系人信息-->
    <delete id="delContactMsg" parameterType="com.susu.pojo.Contact">
        delete
        from contact
        where id = #{id}
    </delete>
</mapper>

6.application.yml配置:

=================server==============
server:
  port: 7777
spring:
  application:
    name: eureka-contact-server
  security:
    user:
      name: root
      password: 123456
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7777/eureka/
    fetch-registry: false
    register-with-eureka: false


===============provider================
server:
  port: 8080

spring:
  application:
    name: contactProvider
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/contact?characterEncoding=utf-8
    username: root
    password: 123456

mybatis:
  config-location: classpath:mybatis_config.xml
  type-aliases-package: com.susu.pojo

eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:7777/eureka/

===============consumer====================
server:
  port: 8083

spring:
  application:
    name: contactConsumer

eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:7777/eureka/

feign:
  client:
    config:
      feignName:
        connectTimeout: 10000
        readTimeout: 10000

#feign:
#  hystrix:
#    enabled: false

userProvider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule  #负载均衡配置(轮询)










7.用hystrix时要加的代码:

package com.susu.contact_consumer.service;

import com.susu.contact_consumer.exception.FeignFallbackFactory;
import com.susu.service.ContactService;
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name = "contactProvider",fallbackFactory = FeignFallbackFactory.class)
public interface ContactServiceConsumer extends ContactService {

}

=========================================================================

package com.susu.contact_consumer.exception;


import com.alibaba.fastjson.JSON;
import com.susu.contact_consumer.service.ContactServiceConsumer;
import com.susu.pojo.Contact;
import com.susu.pojo.Result;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * 友好提示类
 */
@Component
public class FeignFallbackFactory implements FallbackFactory<ContactServiceConsumer> {

    @Override
    public ContactServiceConsumer create(Throwable throwable) {

        return new ContactServiceConsumer() {
            @Override
            public String getContactMsg(int pageNum, int pageSize, String cname) {
                Result<?> result = new Result();
                result.setCode("500");
                result.setMsg("sorry,访问出问题了!");
                return JSON.toJSONString(result);
            }

            @Override
            public String addContactMsg(Contact contact) {
                return null;
            }

            @Override
            public String delContactMsg(int id) {
                return null;
            }
        };

    }
}


8.返回result到前台时的数据结构:

{"code":"200","msg":"获取房屋信息成功!","data":{"pageNum":0,"pageSize":0,"totalPage":2,"houseList":[{"houseId":12,"houseDesc":"11111","typeId":2,"monthlyRent":1200,"publishDate":"2022-04-01 20:55:30","typeName":"小户型"},{"houseId":11,"houseDesc":"11111","typeId":2,"monthlyRent":1200,"publishDate":"2022-04-01 20:55:30","typeName":"小户型"},{"houseId":4,"houseDesc":"desc4","typeId":2,"monthlyRent":800,"publishDate":"2011-10-09 16:00:00","typeName":"小户型"}]}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值