不废话,直接上代码,
Nacos搭建的微服务,可以看Spring Cloud 配置 Nacos(一)-CSDN博客
一,pom文件
spring-cloud-starter-alibaba-nacos-discovery 和 spring-cloud-starter-openfeign
都是基于spring-cloud-alibaba-dependencies
版本为:2022.0.0.0
<!-- mybatis--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.4.1</version></dependency><!-- pojo持久化使用 --><dependency><groupId>jakarta.persistence</groupId><artifactId>jakarta.persistence-api</artifactId><version>3.1.0</version></dependency><!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.2.0</version></dependency><!-- druid数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.20</version></dependency><!-- 引入alibaba-nacos-discovery依赖,发现服务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- 引入alibaba-nacos-config依赖,可以从Nacos配置中心获得配置信息 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>
二,修改Nacos的配置文件
配置了druid
spring:datasource:username: rootpassword: rooturl: jdbc:mysql://localhost:3306/test1?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceinitialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truefilters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500name: hello world!mybatis-plus:configuration:map-underscore-to-camel-case: false
三,配置model,dao,controller
演示就不创建service了。
model
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {@TableId(value = "id",type = IdType.AUTO)private Integer id;@TableField(value = "name")private String name;@TableField(value = "schoolId")private Integer schoolId;
}
dao
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kintech2.test.model.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserDao extends BaseMapper<User> {
}
controller
import com.alibaba.nacos.api.model.v2.Result;
import com.kintech2.test.mapper.UserDao;
import com.kintech2.test.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("api/test")
@RefreshScope
public class ApiTestController {@Autowiredprivate UserDao userDao;@RequestMapping(value = "test2", method = RequestMethod.GET)@ResponseBodypublic Result test2(Integer id){User user=userDao.selectById(id);return Result.success(user.getName());}
}
四,配置Druid的监控
由于最新版本druid,还使用的javax.servlet,此处无法匹配jakarta.servlet。
所以目前要等druid更新,因此注释此配置类
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import jakarta.servlet.Filter;
import jakarta.servlet.Servlet;/*** 新版本Druid的监控还使用javax,无法匹配jakarta*/
@Configuration
public class DruidConfig {@Bean@ConditionalOnMissingBeanpublic ServletRegistrationBean<Servlet> druidServlet() {ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<Servlet>(new StatViewServlet(), "/druid/*");//白名单:servletRegistrationBean.addInitParameter("allow", "127.0.0.1");//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
// servletRegistrationBean.addInitParameter("deny", "192.168.1.119");//登录查看信息的账号密码, 用于登录Druid监控后台servletRegistrationBean.addInitParameter("loginUsername", "admin");servletRegistrationBean.addInitParameter("loginPassword", "admin");//是否能够重置数据.servletRegistrationBean.addInitParameter("resetEnable", "true");return servletRegistrationBean;}/*** 注册Filter信息, 监控拦截器** @return*/@Bean@ConditionalOnMissingBeanpublic FilterRegistrationBean<Filter> filterRegistrationBean() {FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<Filter>();filterRegistrationBean.setFilter(new WebStatFilter());//添加过滤规则.filterRegistrationBean.addUrlPatterns("/*");//添加不需要忽略的格式信息.filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}
}