工程名:microservicecloud-eureka-7001mysql
<dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册本身。Eureka不响本身注册
fetch-registry: false #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务
service-url:
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ (单机)
#设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址(单机)。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer7001.class, args); } }
工程名:microservicecloud-provider-dept-8001spring
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
eureka:
client: # 客户端注册进eureka内
service-url:
defaultZone: http://localhost:7001/eureka/
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.yufeng.springcloud.entities # 全部Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
spring: application: name: microservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://192.168.172.20:3306/cloudDB01 # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库链接池的最小维持链接数
initial-size: 5 # 初始化链接数
max-total: 5 # 最大链接数
max-wait-millis: 200 # 等待链接获取的最大超时时间
eureka:
client: # 客户端注册进eureka内
service-url:
defaultZone: http://localhost:7001/eureka/
@SpringBootApplication @EnableEurekaClient public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); } }
对于注册到Eureka里面的微服务,能够经过服务发现来得到该服务的信息。sql
@RestController public class DeptController { @Autowired //@Resource(name = "deptServiceImpl")
private DeptService service; @Autowired private DiscoveryClient discoveryClient; @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); } @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET) public Object discovery() { List<String> list = discoveryClient.getServices(); //发现eureka中的微服务列表 System.out.println("eureka中全部的微服务的name列表" + list); //从eureka中获取指定name的微服务的信息(yml文件中配置的 spring.application.name) List<ServiceInstance> instances = discoveryClient.getInstances(list.get(0)); for(ServiceInstance instance : instances) { System.out.println(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri()); } return this.discoveryClient; } }
@SpringBootApplication @EnableEurekaClient //本服务启动后自动注册到eureka中
@EnableDiscoveryClient //服务的发现, 暴露出来
public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); } }