0.项目结构
api和consumer模块没有变化(参考之前的项目)
7001-7003是注册的三个eureka服务,正常应该是在三台电脑上进行操作,这里只是进行一个模拟
![](https://i-blog.csdnimg.cn/blog_migrate/1c5e88a1ae733fd11a075e8f086af5b0.png)
1.Eureka模块
由于模块相似,这里只写一个模块
![](https://i-blog.csdnimg.cn/blog_migrate/161aa7043916a626d992884ec58a51f5.png)
1.1 pom
<dependencies>
<!--eureka包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
1.2 配置文件
application.yml
server:
port: 7001
#eureka配置
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #表示是否注册自己
fetch-registry: false #表示自己是注册中心
service-url: #监控页面
defualtZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
#设置与其他两个eureka关联 如果是其他的电脑前面的localhost也要改变
1.3 启动类
@SpringBootApplication
@EnableEurekaServer//服务端启动类,可以接受别人注册进来
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
2.Provider模块
![](https://i-blog.csdnimg.cn/blog_migrate/7ae5da28f3b089b8a9dec0903a24b78d.png)
2.1 主启动类添加注解
@SpringBootApplication
@EnableEurekaClient//在服务启动后自动注册到eureka中
@EnableDiscoveryClient//服务发现
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
2.2 controller类可以获得一些微服务的信息
//提供restful服务
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient discoveryClient;//获取一些具体微服务
@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> queryAll(Dept dept) {
return deptService.queryAll();
}
//注册进来的微服务 获取一些信息
@GetMapping("/dept/discovery")
public Object discovery() {
//获得微服务列表的清单
List<String> strings = discoveryClient.getServices();
System.out.println(strings);
//得到具体的微服务的信息
List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");//通过eureka里显示的名字来取
for (ServiceInstance instance : instances) {
System.out.println(instance.getHost() + " " + instance.getPort() + " " + instance.getUri() + " " + instance.getInstanceId());
}
return this.discoveryClient;
}
}
3.3 配置文件application.yml 注册eureka,集群设置,以及更改一些信息
server:
port: 8001
#mybatis的配置
mybatis:
type-aliases-package: com.wu.springcloud.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
#spring的配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db01?useSSL=true&useUnicode=true&characterEncoding=utf-8
username: root
password: root
#注册eureka,服务注册到哪里
eureka:
client:
service-url:
#集群发布
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
instance:
instance-id: provider-dept-8001 #修改描述信息
info:
app.name: xiaowu-springcloud
company.name: yuzhibo.com