💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
* 打开`microservice-provider-user`项目,并在`pom.xml`中新增以下依赖: ~~~xml <!-- 新增依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ~~~ * 修改`App.java`: ~~~java package com.shiyanlou; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import java.math.BigDecimal; import java.util.stream.Stream; @SpringBootApplication @EnableDiscoveryClient public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean ApplicationRunner init(UserRepository repository){ return args -> { User user1 = new User(1L, "account1", "张三", 20, new BigDecimal(100.00)); User user2 = new User(2L, "account2", "李四", 28, new BigDecimal(180.00)); User user3 = new User(3L, "account3", "王五", 32, new BigDecimal(280.00)); Stream.of(user1, user2, user3) .forEach(repository::save); }; } } ~~~ * 新增 Eureka Client 相关配置: ~~~yaml # 新增配置 eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ instance: prefer-ip-address: true ~~~ 简单介绍一下以上配置: 1. eureka.client.serviceUrl.defaultZone:表示 Eureka Server 的交互地址,此前已经在 Eureka Server 中配置该 defaultZone。 2. eureka.instance.prefer-ip-address:表示将自身服务器的 IP 地址注册到 Eureka Server 中。如果不配置或者设置为 false,则将会将服务器的 hostname 注册到 Eureka Server 中。 * 重启服务 如果此前正在运行该服务,请先停止该服务器。 ~~~bash mvn spring-boot:run ~~~ 当出现一下日志则表示服务已经启动完成: ~~~txt 2019-03-15 20:52:11.887 INFO 15232 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8000 (http) with context path '' 2019-03-15 20:52:11.888 INFO 15232 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8000 2019-03-15 20:52:11.892 INFO 15232 --- [ main] .c.m.MicroserviceProviderUserApplication : Started MicroserviceProviderUserApplication in 12.96 seconds (JVM running for 13.407) 2019-03-15 21:51:09.615 INFO 29321 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_MICROSERVICE-PROVIDER-USER/terransforce:microservice-provider-user:8000 - registration status: 204 ~~~