前提
便于理解,我修改了本地域名=》这里!!!
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
学习Rest实例之提供者
1、导入依赖
<!-- 实体类 + Web--><dependency><groupId>com.jyl</groupId><artifactId>springcloud-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>
<!-- 热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka --><!-- eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></dependency>
<!-- 解决 eureka Status actuator/info 完善监控信息--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
2、application.yml 用户级配置文件
配置端口号、mybatis配置、spring配置、eureka配置、监控信息配置
server:port: 8001
# mybatis 配置mybatis:type-aliases-package: com.jyl.springcloud.pojoconfig-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mapper/*.xml# spring 的配置spring:application:name: springcloud-provider-deptdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: org.gjt.mm.mysql.Driverurl: jdbc:mysql://localhost:3306/db01?useUnicode=true&useSSL=false&characterEncoding=utf-8username: rootpassword: rootdruid:test-while-idle: false # 关闭空闲检测eureka:client:service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");# 端口号 7001 7002 7003 ,实际工作中是3台不同的电脑域名或IPdefaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7002.com:7003/eureka/instance:instance-id: springcloud-provider-dept8001 # 修改eureka 上 Status默认描述信息# 导入 actuator
info:app.name: jiangyl-springcloud8001company.name: jiang.yl.com
3、Controller层
通过DiscoveryClient的API从注册中心获取其他已经注册的服务的信息
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;// 可以获取服务列表 @Autowiredprivate DiscoveryClient client;@PostMapping("/dept/add")public boolean addDept(Dept dept){return deptService.addDept(dept);}@GetMapping("/dept/get/{id}")public Dept get(@PathVariable("id") Long id){return deptService.queryById(id);}@GetMapping("/dept/list")public List<Dept> qeuryAll(){return deptService.queryAll();}// 还需要在启动类中添加 @EnableDiscoveryClient 服务发现 用于团队协助@GetMapping("/dept/discovery")//注册进来的微服务 , 获取一些信息public Object discovery(){// 获取微服务列表的清单List<String> services = client.getServices();System.out.println("discovery->services = " + services);// Eureka 中Application iDList<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");for (ServiceInstance instance : instances) {System.out.println(instance.getHost()+"\t"+instance.getUri()+"\t"+instance.getPort()+"\t"+instance.getServiceId());}return this.client;}
}
4、service层
public interface DeptService {public boolean addDept(Dept dept);public Dept queryById(Long id);public List<Dept> queryAll();}
5、启动类
@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient // 自动在服务启动后自动注册到Eureka中
@EnableDiscoveryClient // 服务发现
public class SpringBootProviderApplication_8001 {public static void main(String[] args) {SpringApplication.run(SpringBootProviderApplication_8001.class,args);}
}
测试结果展示
前提是先启动eureka集群
测试1
访问eureka服务 http://localhost:7001/
可以查看到注册的提供者信息名称为SPRINGCLOUD-PROVIDER-DEPT
测试2
访问服务提供者接口 http://localhost:8001/dept/list
测试3
访问http://localhost:8001/dept/discovery
控制台输出
discovery->services = [springcloud-provider-dept]
[本机用户名] http://[本机用户名]:8001 8001 SPRINGCLOUD-PROVIDER-DEPT
discovery->services = [springcloud-provider-dept]
[本机用户名] http://[本机用户名]:8001 8001 SPRINGCLOUD-PROVIDER-DEPT
搭建eureka服务==>传送
搭建springcloud的提供者–>传送