工程名: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); } }