本文共 9272 字,大约阅读时间需要 30 分钟。
org.springframework.cloud spring-cloud-starter-eureka-server
完整pom文件如下:
4.0.0 com.atguigu.springcloud microservicecloud 0.0.1-SNAPSHOT microservicecloud-eureka-7001 org.springframework.cloud spring-cloud-starter-eureka-server org.springframework springloaded org.springframework.boot spring-boot-devtools
(2)yaml配置文件
server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://${ eureka.instance.hostname}:${ server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)
(3)主启动类
使用注解@EnableEurekaServer开启Eureka服务@SpringBootApplication@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来public class EurekaServer7001_App{ public static void main(String[] args){ SpringApplication.run(EurekaServer7001_App.class, args); }}
org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-config
完整pom文件:
4.0.0 com.atguigu.springcloud microservicecloud 0.0.1-SNAPSHOT microservicecloud-provider-dept-8001 com.atguigu.springcloud microservicecloud-api ${project.version} org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-config junit junit mysql mysql-connector-java com.alibaba druid ch.qos.logback logback-core org.mybatis.spring.boot mybatis-spring-boot-starter org.springframework.boot spring-boot-starter-jetty org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework springloaded org.springframework.boot spring-boot-devtools
(2)yaml配置文件
server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.atguigu.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://localhost:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: microservicecloud-dept8001 #在Eureka注册中心显示的服务路径名 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
(3)主启动类
@SpringBootApplication@EnableEurekaClient //本服务启动后会自动注册进eureka服务中public class DeptProvider8001_App{ public static void main(String[] args){ SpringApplication.run(DeptProvider8001_App.class, args); }}
(4)controller层代码
@RestControllerpublic class DeptController{ @Autowired private DeptService service; @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); }}}
(5)Eureka的info信息显示
① pom文件 添加监听 actuator监控信息org.springframework.boot spring-boot-starter-actuator
② 在父工程的pom文件中添加build
父工程名 src/main/resources true org.apache.maven.plugins maven-resources-plugin $
③ yaml配置文件
eureka: instance: instance-id: microservicecloud-dept8001 #在Eureka注册中心显示的服务路径名 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-config
完整pom文件:
4.0.0 com.atguigu.springcloud microservicecloud 0.0.1-SNAPSHOT microservicecloud-consumer-dept-80 部门微服务消费者 com.atguigu.springcloud microservicecloud-api ${project.version} org.springframework.cloud spring-cloud-starter-ribbon org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web org.springframework springloaded org.springframework.boot spring-boot-devtools
(2)yaml配置文件
server: port: 80
(3)主启动类
@SpringBootApplication@EnableEurekaClient //本服务启动后会自动注册进eureka服务中public class DeptConsumer80_App{ public static void main(String[] args){ SpringApplication.run(DeptConsumer80_App.class, args); }}
(4)controller层代码
@RestControllerpublic class DeptController_Consumer{ private static final String REST_URL_PREFIX = "http://localhost:8001";//consumer访问provider的功能 /** * 使用 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap, * ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。 */ @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/consumer/dept/add") public boolean add(Dept dept) { return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class); }}
server: port: 7001 eureka: instance: hostname: eureka7001.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。 defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server: port: 8001spring: application: name: microservicecloud-dept eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #访问路径可以显示IP地址 info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
转载地址:http://skojz.baihongyu.com/