## idea2017+springboot+mysql + eureka + feign 项目
实现 eureka-consumer 调用 eureka-provide ,返回user 信息
一、搭建 eureka-server 服务注册器
idea 创建springboot
然后下一步,直到finish;
创建好后的目录:
pom.xml 中的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml 配置:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761
spring:
application:
name: eureka-server
启动类加上:@EnableEurekaServer 注解 启动Eureka
package com.my_eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MyEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(MyEurekaServerApplication.class, args);
}
}
2、然后启动服务器,访问:http://localhost:8761
现在只有 eureka-server, 没有注册的服务器,等后面启动 eureka-client 后就有了
二、搭建 eureke-client ,这里分为 eureka-provide 和 eureka-consumer
由于需要在 eureka-provide 和 eureka-consumer 实现通信,相互调用,所以会使用feign来实现两系统的通信
1、先搭建 eureka-provide (即 my_springboot_project) :服务提供者
然后点击next,直到finish ;
目录:
pom.xml 相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--缺少此jar包,导致@Mapper注解无效-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--实现分页查询插件-->
<!--这是传统ssm项目引入的,这里最好使用springboot版的-->
<!--<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!--操作excel的jar-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
<!-- Hikari 连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--jsp支持-->
<!-- springboot 的spring-boot-starter-web 依赖中有tomcat的依赖,但是没有tomcat-embed-jasper 的依赖,所以没有这个就会下载jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<scope>compile</scope>--> <!--这里必须使用compile ,或者不要这个标签,默认也是compile,否则还是会下载 jsp -->
</dependency>
<!--这是idea启动项目需要的jar,不然访问jsp 会报错:
org.apache.jasper.JasperException: The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the jar files deployed with this application-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
application.yml 配置:
#本项目使用的端口配置
server:
port: 8762
#注册服务器地址
eureka:
client:
service-url:
default-zone: http://localhost:8761
#spring 相关配置
spring:
application:
name: my-springboot-project
mvc:
view:
suffix: .jsp
prefix: /jsp/
#数据源配置、Hikari 连接池 配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: 自己的数据库 url
username: 数据库名称
password: 数据库密码
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 5
maximum-pool-size: 15
auto-commit: true
idle-timeout: 30000
pool-name: DatebookHikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
#mybatis 相关配置
mybatis:
mapper-locations : classpath:mapper/*.xml
# pagehelper 配置
#logging.level.com.example.demo.dao=DEBUG
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
pageSizeZero: true
启动类:
package com.my_springboot_project;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
//扫描包
@ComponentScan(value = "com.my_springboot_project")
//扫描 dao
@MapperScan("com.my_springboot_project.dao")
@EnableEurekaClient
@EnableFeignClients
public class MySpringbootProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootProjectApplication.class, args);
}
}
UserManagerFeignController 类:提供给eureka-consumer 调用的controller
package com.my_springboot_project.controller.feign_controllers;
import com.my_springboot_project.service.user_manager_services.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("userManagerFeignController")
public class UserManagerFeignController {
@Autowired
private UserManagerService userManagerService;
@RequestMapping("/getUserAll")
public List<Map<String,Object>> getUserAll(@RequestParam Map<String,Object> map){
List<Map<String,Object>> list = userManagerService.userSelect(map);
return list;
}
}
service 层和 dao层 以及resource 下的 mapper 自己写;
启动项目:
1)、打开项目设置,选中 Modules,添加 web,如下图:
2)、项目启动配置(这个是关键,没有这一步上面的全都没用)
点击右边Maven Projects选项,找到spring-boot:run ,选中后点击右键创建启动项,这样就可以不需要每次找到这边儿启动了
然后就可以启动项目了,启动后 eureka-server 页面:
2、搭建 eureka-consumer :
idea 创建springboot 就不展示了
目录:
pom.xml 相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--jsp支持-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
application.yml 配置:
spring:
application:
name: eureka-consumer
mvc:
view:
suffix: .jsp
prefix: /WEB-INF/
static-path-pattern: /static/**
server:
port: 8763
eureka:
client:
service-url:
default-zone: http://localhost:8761
启动类:
package com.tangdong.eurekaconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@ComponentScan("com.tangdong.eurekaconsumer")
public class EurekaconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaconsumerApplication.class, args);
}
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
feign 是基于接口调用的,所有调用方需要编写接口:
package com.tangdong.eurekaconsumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
@Component
@FeignClient(value = "my-springboot-project")//使用的服务器名称,名称不能带下划线,不然会报错:java.lang.IllegalStateException: Service id not legal hostname (my_springboot_project)
public interface UserService {
@RequestMapping("/userManagerFeignController/getUserAll")//对应提供服务的服务器的controller 中的方法
List<Map<String,Object>> getUserAll(@RequestParam("map") Map<String,Object> map);
}
eureka-consumer 访问的controller:
package com.tangdong.eurekaconsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/myController")
public class MyController {
@Autowired
private UserService userService;
@RequestMapping("/getUsers")
public String getUsers(){
Map<String,Object> paramMap = new HashMap<>();
List<Map<String,Object>> userList = userService.getUserAll(paramMap);//这里就是 提供者返回的数据
System.out.println(userList.get(0).get("user_name"));
return null;
}
}
启动设置跟 my-springboot-project 一样,启动后访问:
http://localhost:8763/myController/getUsers
后台就会输出获取数据的第一条user 的名称